EntityReferenceEntityFormatter.php in Entity reference 8
File
lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceEntityFormatter.php
View source
<?php
namespace Drupal\entityreference\Plugin\field\formatter;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
use Drupal\entityreference\EntityReferenceRecursiveRenderingException;
use Drupal\entityreference\Plugin\field\formatter\EntityReferenceFormatterBase;
class EntityReferenceEntityFormatter extends EntityReferenceFormatterBase {
public function settingsForm(array $form, array &$form_state) {
$entity_info = entity_get_info($this->field['settings']['target_type']);
$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'];
}
}
$elements['view_mode'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => t('View mode'),
'#default_value' => $this
->getSetting('view_mode'),
'#required' => TRUE,
);
$elements['links'] = array(
'#type' => 'checkbox',
'#title' => t('Show links'),
'#default_value' => $this
->getSetting('links'),
);
return $elements;
}
public function settingsSummary() {
$summary = array();
$entity_info = entity_get_info($this->field['settings']['target_type']);
$view_mode = $this
->getSetting('view_mode');
$summary[] = t('Rendered as @mode', array(
'@mode' => isset($entity_info['view modes'][$view_mode]['label']) ? $entity_info['view modes'][$view_mode]['label'] : $view_mode,
));
$summary[] = $this
->getSetting('links') ? t('Display links') : t('Do not display links');
return implode('<br />', $summary);
}
public function viewElements(EntityInterface $entity, $langcode, array $items) {
parent::viewElements($entity, $langcode, $items);
$instance = $this->instance;
$field = $this->field;
$view_mode = $this
->getSetting('view_mode');
$links = $this
->getSetting('links');
$target_type = $field['settings']['target_type'];
$elements = array();
foreach ($items as $delta => $item) {
static $depth = 0;
$depth++;
if ($depth > 20) {
throw new EntityReferenceRecursiveRenderingException(format_string('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', array(
'@entity_type' => $entity_type,
'@entity_id' => $item['target_id'],
)));
}
$entity = clone $item['entity'];
unset($entity->content);
$elements[$delta] = entity_view($entity, $view_mode, $langcode);
if (empty($links) && isset($result[$delta][$target_type][$item['target_id']]['links'])) {
$elements[$delta][$target_type][$item['target_id']]['links']['#access'] = FALSE;
}
$depth = 0;
}
return $elements;
}
}