3 namespace Drupal\views\Plugin\views\field;
5 use Drupal\Core\Entity\EntityMalformedException;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\views\ResultRow;
10 use Drupal\views\ViewExecutable;
11 use Drupal\views\Plugin\views\display\DisplayPluginBase;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Field handler to display entity label optionally linked to entity page.
17 * @ViewsField("entity_label")
19 class EntityLabel extends FieldPluginBase {
22 * Array of entities that reference to file.
26 protected $loadedReferencers = [];
29 * EntityManager class.
31 * @var \Drupal\Core\Entity\EntityManagerInterface
33 protected $entityManager;
36 * Constructs a EntityLabel object.
38 * @param array $configuration
39 * A configuration array containing information about the plugin instance.
40 * @param string $plugin_id
41 * The plugin_id for the plugin instance.
42 * @param mixed $plugin_definition
43 * The plugin implementation definition.
44 * @param \Drupal\Core\Entity\EntityManagerInterface $manager
45 * EntityManager that is stored internally and used to load nodes.
47 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $manager) {
48 parent::__construct($configuration, $plugin_id, $plugin_definition);
50 $this->entityManager = $manager;
56 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
61 $container->get('entity.manager')
68 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
69 parent::init($view, $display, $options);
70 $this->additional_fields[$this->definition['entity type field']] = $this->definition['entity type field'];
76 protected function defineOptions() {
77 $options = parent::defineOptions();
78 $options['link_to_entity'] = ['default' => FALSE];
85 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
86 $form['link_to_entity'] = [
87 '#title' => $this->t('Link to entity'),
88 '#description' => $this->t('Make entity label a link to entity page.'),
89 '#type' => 'checkbox',
90 '#default_value' => !empty($this->options['link_to_entity']),
92 parent::buildOptionsForm($form, $form_state);
98 public function render(ResultRow $values) {
99 $type = $this->getValue($values, $this->definition['entity type field']);
100 $value = $this->getValue($values);
102 if (empty($this->loadedReferencers[$type][$value])) {
106 /** @var $entity \Drupal\Core\Entity\EntityInterface */
107 $entity = $this->loadedReferencers[$type][$value];
109 if (!empty($this->options['link_to_entity'])) {
111 $this->options['alter']['url'] = $entity->toUrl();
112 $this->options['alter']['make_link'] = TRUE;
114 catch (UndefinedLinkTemplateException $e) {
115 $this->options['alter']['make_link'] = FALSE;
117 catch (EntityMalformedException $e) {
118 $this->options['alter']['make_link'] = FALSE;
122 return $this->sanitizeValue($entity->label());
128 public function preRender(&$values) {
129 parent::preRender($values);
131 $entity_ids_per_type = [];
132 foreach ($values as $value) {
133 if ($type = $this->getValue($value, 'type')) {
134 $entity_ids_per_type[$type][] = $this->getValue($value);
138 foreach ($entity_ids_per_type as $type => $ids) {
139 $this->loadedReferencers[$type] = $this->entityManager->getStorage($type)->loadMultiple($ids);