View source
<?php
function ref_field_content_help($path, $arg) {
switch ($path) {
case 'admin/help#ref_field_content':
return '<p>' . t('Reference field Content Formatter provides a formatter for Entity referene Field that will render the referecnced entities.') . '</p>';
}
}
function ref_field_content_field_formatter_info() {
return array(
'ref_field_content' => array(
'label' => t('Entity content'),
'description' => t('Display the content of the referenced entity'),
'field types' => array(
'ref_field',
),
'settings' => array(
'view_mode' => '',
),
),
);
}
function ref_field_content_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$element = array();
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
switch ($display['type']) {
case 'ref_field_content':
$entity_info = entity_get_info($field['settings']['entity']);
$options = array();
if (!empty($entity_info['view modes'])) {
foreach ($entity_info['view modes'] as $view_mode => $view_mode_settings) {
$options[$view_mode] = $view_mode_settings['label'];
}
}
if ($options) {
$element['view_mode'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => t('View mode'),
'#default_value' => $settings['view_mode'],
);
}
break;
}
return $element;
}
function ref_field_content_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
switch ($display['type']) {
case 'ref_field_content':
$entity_info = entity_get_info($field['settings']['entity']);
if ($settings['view_mode']) {
$summary = t('Rendered as @mode', array(
'@mode' => isset($entity_info['view modes'][$settings['view_mode']]['label']) ? $entity_info['view modes'][$settings['view_mode']]['label'] : $settings['view_mode'],
));
}
else {
$summary = t('No view mode has been selected.<br />Please select one by clicking on the configuration button to the right of this message.');
}
break;
}
return $summary;
}
function ref_field_content_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
$target_ids = array();
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
$target_ids[] = $item['eid'];
}
}
if ($target_ids) {
$target_entities = entity_load($field['settings']['entity'], $target_ids);
foreach ($entities as $id => $entity) {
$rekey = FALSE;
foreach ($items[$id] as $delta => $item) {
if (isset($target_entities[$item['eid']])) {
$items[$id][$delta]['entity'] = $target_entities[$item['eid']];
}
else {
unset($items[$id][$delta]);
$rekey = TRUE;
}
}
if ($rekey) {
$items[$id] = array_values($items[$id]);
}
}
}
}
function ref_field_content_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'ref_field_content':
foreach ($items as $delta => $item) {
static $depth = 0;
$depth++;
if ($depth > 20) {
throw new RefFieldContentRecursiveRenderingException(t('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', array(
'@entity_type' => $entity_type,
'@entity_id' => $item['eid'],
)));
}
$e = $item['entity'];
$e_type = $field['settings']['entity'];
$element[$delta] = entity_view($e_type, array(
$item['eid'] => $e,
), $display['settings']['view_mode'], $langcode, FALSE);
$depth = 0;
}
break;
}
return $element;
}
class RefFieldContentRecursiveRenderingException extends Exception {
}