You are here

ref_field_content.module in (Entity)Reference Field Synchronization 7

File

ref_field_content/ref_field_content.module
View source
<?php

/**
 * Implements hook_help().
 */
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>';
  }
}

/**
 * Implements hook_field_formatter_info().
 */
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' => '',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
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;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
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;
}

/**
 * Implements hook_field_formatter_prepare_view().
 */
function ref_field_content_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
  $target_ids = array();

  // Collect every possible entity attached to any of the entities.
  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);

    // Iterate through the fieldable entities again to attach the loaded data.
    foreach ($entities as $id => $entity) {
      $rekey = FALSE;
      foreach ($items[$id] as $delta => $item) {

        // Check whether the referenced entity could be loaded.
        if (isset($target_entities[$item['eid']])) {

          // Replace the instance value with the term data.
          $items[$id][$delta]['entity'] = $target_entities[$item['eid']];
        }
        else {
          unset($items[$id][$delta]);
          $rekey = TRUE;
        }
      }
      if ($rekey) {

        // Rekey the items array.
        $items[$id] = array_values($items[$id]);
      }
    }
  }
}

/**
 * Implements hook_field_formatter_view().
 */
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) {

        // Protect ourselves from recursive rendering.
        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;
}

/**
 * Exception thrown when the entity view renderer goes into a potentially infinite loop.
 */
class RefFieldContentRecursiveRenderingException extends Exception {

}