Skip to content

Instantly share code, notes, and snippets.

@litzinger
Last active December 18, 2015 03:29
Show Gist options
  • Save litzinger/5718707 to your computer and use it in GitHub Desktop.
Save litzinger/5718707 to your computer and use it in GitHub Desktop.
Add Api_channel_fields back to the entries parser. Update the replace() methods in the following two core files: system/expressionengine/libraries/channel_entries_parser/components/Custom_field.php system/expressionengine/libraries/channel_entries_parser/components/Custom_field_pair.php
<?php
public function replace($tagdata, EE_Channel_data_parser $obj, $ft_api)
{
$tag = $obj->tag();
$data = $obj->row();
$prefix = $obj->prefix();
$site_id = $data['site_id'];
$cfields = $obj->channel()->cfields;
$rfields = $obj->channel()->rfields;
$rfields = isset($rfields[$site_id]) ? $rfields[$site_id] : array();
$cfields = isset($cfields[$site_id]) ? $cfields[$site_id] : array();
$cfields = array_diff_key($cfields, $rfields);
if (empty($cfields))
{
return $tagdata;
}
$unprefixed_tag = preg_replace('/^'.$prefix.'/', '', $tag);
$field_name = substr($unprefixed_tag.' ', 0, strpos($unprefixed_tag.' ', ' '));
$param_string = substr($unprefixed_tag.' ', strlen($field_name));
$modifier = '';
$modifier_loc = strpos($field_name, ':');
if ($modifier_loc !== FALSE)
{
$modifier = substr($field_name, $modifier_loc + 1);
$field_name = substr($field_name, 0, $modifier_loc);
}
if (isset($cfields[$field_name]))
{
$entry = '';
$field_id = $cfields[$field_name];
if (isset($data['field_id_'.$field_id]) && $data['field_id_'.$field_id] != '')
{
$params = array();
$parse_fnc = ($modifier) ? 'replace_'.$modifier : 'replace_tag';
if ($param_string)
{
$params = ee()->functions->assign_parameters($param_string);
}
$obj = $ft_api->setup_handler($field_id, TRUE);
if ($obj)
{
/*
$_ft_path = $ft_api->ft_paths[$ft_api->field_type];
ee()->load->add_package_path($_ft_path, FALSE);
$obj->_init(array('row' => $data));
$data = $obj->pre_process($data['field_id_'.$field_id]);
if (method_exists($obj, $parse_fnc))
{
$entry = $obj->$parse_fnc($data, $params, FALSE);
}
elseif (method_exists($obj, 'replace_tag_catchall'))
{
$entry = $obj->replace_tag_catchall($data, $params, FALSE, $modifier);
}
*/
ee()->load->library('api');
ee()->api->instantiate('channel_fields');
ee()->api_channel_fields->apply('_init', array(array('row' => $data)));
$data = ee()->api_channel_fields->apply('pre_process', array($data['field_id_'.$field_id]));
if (ee()->api_channel_fields->check_method_exists($parse_fnc))
{
$entry = ee()->api_channel_fields->apply($parse_fnc, array($data, $params, FALSE));
}
elseif (ee()->api_channel_fields->check_method_exists($parse_fnc_catchall))
{
$entry = ee()->api_channel_fields->apply($parse_fnc_catchall, array($data, $params, FALSE, $modifier));
}
}
else
{
// Couldn't find a fieldtype
$entry = ee()->typography->parse_type(
ee()->functions->encode_ee_tags($data['field_id_'.$field_id]),
array(
'text_format' => $data['field_ft_'.$field_id],
'html_format' => $data['channel_html_formatting'],
'auto_links' => $data['channel_auto_link_urls'],
'allow_img_url' => $data['channel_allow_img_urls']
)
);
}
// prevent accidental parsing of other channel variables in custom field data
if (strpos($entry, '{') !== FALSE)
{
$entry = str_replace(
array('{', '}'),
array(unique_marker('channel_bracket_open'), unique_marker('channel_bracket_close')),
$entry
);
}
$tagdata = str_replace(LD.$tag.RD, $entry, $tagdata);
}
$tagdata = str_replace(LD.$tag.RD, '', $tagdata);
}
return $tagdata;
}
<?php
public function replace($tagdata, EE_Channel_data_parser $obj, $pfield_chunks)
{
$data = $obj->row();
$prefix = $obj->prefix();
$site_id = $data['site_id'];
$cfields = $obj->channel()->cfields;
$cfields = isset($cfields[$site_id]) ? $cfields[$site_id] : array();
if (empty($cfields) OR ! isset($pfield_chunks[$site_id]))
{
return $tagdata;
}
$pfield_chunk = $pfield_chunks[$site_id];
$ft_api = ee()->api_channel_fields;
foreach ($pfield_chunk as $tag_name => $chunks)
{
$field_name = preg_replace('/^'.$prefix.'/', '', $tag_name);
$field_name = substr($field_name, strpos($field_name, ' '));
$field_id = $cfields[$field_name];
$obj = $ft_api->setup_handler($field_id, TRUE);
if ($obj)
{
/*
$_ft_path = $ft_api->ft_paths[$ft_api->field_type];
ee()->load->add_package_path($_ft_path, FALSE);
$obj->_init(array('row' => $data));
$pre_processed = $obj->pre_process($data['field_id_'.$field_id]);
*/
ee()->load->library('api');
ee()->api->instantiate('channel_fields');
ee()->api_channel_fields->apply('_init', array(array('row' => $data)));
$pre_processed = ee()->api_channel_fields->apply('pre_process', array($data['field_id_'.$field_id]));
foreach($chunks as $chk_data)
{
list($modifier, $content, $params, $chunk) = $chk_data;
$tpl_chunk = '';
// Set up parse function name based on whether or not
// we have a modifier
$parse_fnc = ($modifier) ? 'replace_'.$modifier : 'replace_tag';
/*
if (method_exists($obj, $parse_fnc))
{
$tpl_chunk = $obj->$parse_fnc($pre_processed, $params, $content);
}
// Go to catchall and include modifier
elseif (method_exists($obj, 'replace_tag_catchall') AND $modifier !== '')
{
$tpl_chunk = $obj->replace_tag_catchall($pre_processed, $params, $content, $modifier);
}
*/
if (ee()->api_channel_fields->check_method_exists($parse_fnc))
{
$tpl_chunk = ee()->api_channel_fields->apply($parse_fnc, array($pre_processed, $params, $content));
}
elseif (ee()->api_channel_fields->check_method_exists('replace_tag_catchall'))
{
$tpl_chunk = ee()->api_channel_fields->apply($parse_fnc_catchall, array($data, $params, $content, $modifier));
}
$tagdata = str_replace($chunk, $tpl_chunk, $tagdata);
}
}
}
return $tagdata;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment