View source
<?php
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityReferenceEntityFormatter extends EntityReferenceFormatterBase implements ContainerFactoryPluginInterface {
protected $loggerFactory;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, LoggerChannelFactoryInterface $logger_factory) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->loggerFactory = $logger_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('logger.factory'));
}
public static function defaultSettings() {
return array(
'view_mode' => 'default',
'link' => FALSE,
) + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['view_mode'] = array(
'#type' => 'select',
'#options' => \Drupal::entityManager()
->getViewModeOptions($this
->getFieldSetting('target_type')),
'#title' => t('View mode'),
'#default_value' => $this
->getSetting('view_mode'),
'#required' => TRUE,
);
return $elements;
}
public function settingsSummary() {
$summary = array();
$view_modes = \Drupal::entityManager()
->getViewModeOptions($this
->getFieldSetting('target_type'));
$view_mode = $this
->getSetting('view_mode');
$summary[] = t('Rendered as @mode', array(
'@mode' => isset($view_modes[$view_mode]) ? $view_modes[$view_mode] : $view_mode,
));
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$view_mode = $this
->getSetting('view_mode');
$elements = array();
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $entity) {
static $depth = 0;
$depth++;
if ($depth > 20) {
$this->loggerFactory
->get('entity')
->error('Recursive rendering detected when rendering entity @entity_type @entity_id. Aborting rendering.', array(
'@entity_type' => $entity
->getEntityTypeId(),
'@entity_id' => $entity
->id(),
));
return $elements;
}
if ($entity
->id()) {
$elements[$delta] = entity_view($entity, $view_mode, $entity
->language()
->getId());
if (!empty($items[$delta]->_attributes)) {
$items[$delta]->_attributes += array(
'resource' => $entity
->url(),
);
}
}
else {
$elements[$delta] = array(
'#markup' => $entity
->label(),
);
}
$depth = 0;
}
return $elements;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$target_type = $field_definition
->getFieldStorageDefinition()
->getSetting('target_type');
return \Drupal::entityManager()
->getDefinition($target_type)
->hasViewBuilderClass();
}
}