Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
Alex Pott
committed
* Contains \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter.
Dries Buytaert
committed
*/
Alex Pott
committed
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
Dries Buytaert
committed
use Drupal\Core\Field\FieldDefinitionInterface;
Alex Pott
committed
use Drupal\Core\Field\FieldItemListInterface;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Alex Pott
committed
use Drupal\Core\Language\LanguageInterface;
Alex Pott
committed
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
Dries Buytaert
committed
/**
* Plugin implementation of the 'entity reference rendered entity' formatter.
*
Alex Pott
committed
* @FieldFormatter(
Dries Buytaert
committed
* id = "entity_reference_entity_view",
* label = @Translation("Rendered entity"),
* description = @Translation("Display the referenced entities rendered by entity_view()."),
* field_types = {
* "entity_reference"
* }
* )
*/
Alex Pott
committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class EntityReferenceEntityFormatter extends EntityReferenceFormatterBase implements ContainerFactoryPluginInterface {
/**
* The logger factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;
/**
* Constructs a StringFormatter instance.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings settings.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param LoggerChannelFactoryInterface $logger_factory
* The logger factory.
*/
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;
}
/**
* {@inheritdoc}
*/
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')
);
}
Dries Buytaert
committed
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'view_mode' => 'default',
'link' => FALSE,
) + parent::defaultSettings();
}
Dries Buytaert
committed
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
Dries Buytaert
committed
public function settingsForm(array $form, FormStateInterface $form_state) {
Dries Buytaert
committed
$elements['view_mode'] = array(
'#type' => 'select',
'#options' => \Drupal::entityManager()->getViewModeOptions($this->getFieldSetting('target_type')),
Dries Buytaert
committed
'#title' => t('View mode'),
'#default_value' => $this->getSetting('view_mode'),
'#required' => TRUE,
);
return $elements;
}
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function settingsSummary() {
$summary = array();
$view_modes = \Drupal::entityManager()->getViewModeOptions($this->getFieldSetting('target_type'));
Dries Buytaert
committed
$view_mode = $this->getSetting('view_mode');
$summary[] = t('Rendered as @mode', array('@mode' => isset($view_modes[$view_mode]) ? $view_modes[$view_mode] : $view_mode));
Dries Buytaert
committed
Alex Pott
committed
return $summary;
Dries Buytaert
committed
}
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
Alex Pott
committed
public function viewElements(FieldItemListInterface $items, $langcode) {
Dries Buytaert
committed
$view_mode = $this->getSetting('view_mode');
$elements = array();
Alex Pott
committed
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
Dries Buytaert
committed
// Protect ourselves from recursive rendering.
static $depth = 0;
$depth++;
if ($depth > 20) {
Alex Pott
committed
$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;
Dries Buytaert
committed
}
if ($entity->id()) {
$elements[$delta] = entity_view($entity, $view_mode, $entity->language()->getId());
Dries Buytaert
committed
// Add a resource attribute to set the mapping property's value to the
// entity's url. Since we don't know what the markup of the entity will
// be, we shouldn't rely on it for structured data such as RDFa.
if (!empty($items[$delta]->_attributes)) {
$items[$delta]->_attributes += array('resource' => $entity->url());
}
Dries Buytaert
committed
}
else {
// This is an "auto_create" item.
$elements[$delta] = array('#markup' => $entity->label());
Dries Buytaert
committed
}
$depth = 0;
}
return $elements;
}
Angie Byron
committed
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
// This formatter is only available for entity types that have a view
// builder.
$target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
return \Drupal::entityManager()->getDefinition($target_type)->hasViewBuilderClass();
}