Version 1
[yaffs-website] / web / core / modules / views / src / Plugin / views / field / EntityLabel.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\field;
4
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;
13
14 /**
15  * Field handler to display entity label optionally linked to entity page.
16  *
17  * @ViewsField("entity_label")
18  */
19 class EntityLabel extends FieldPluginBase {
20
21   /**
22    * Array of entities that reference to file.
23    *
24    * @var array
25    */
26   protected $loadedReferencers = [];
27
28   /**
29    * EntityManager class.
30    *
31    * @var \Drupal\Core\Entity\EntityManagerInterface
32    */
33   protected $entityManager;
34
35   /**
36    * Constructs a EntityLabel object.
37    *
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.
46    */
47   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $manager) {
48     parent::__construct($configuration, $plugin_id, $plugin_definition);
49
50     $this->entityManager = $manager;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
57     return new static(
58       $configuration,
59       $plugin_id,
60       $plugin_definition,
61       $container->get('entity.manager')
62     );
63   }
64
65   /**
66    * {@inheritdoc}
67    */
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'];
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   protected function defineOptions() {
77     $options = parent::defineOptions();
78     $options['link_to_entity'] = ['default' => FALSE];
79     return $options;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
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']),
91     ];
92     parent::buildOptionsForm($form, $form_state);
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function render(ResultRow $values) {
99     $type = $this->getValue($values, $this->definition['entity type field']);
100     $value = $this->getValue($values);
101
102     if (empty($this->loadedReferencers[$type][$value])) {
103       return;
104     }
105
106     /** @var $entity \Drupal\Core\Entity\EntityInterface */
107     $entity = $this->loadedReferencers[$type][$value];
108
109     if (!empty($this->options['link_to_entity'])) {
110       try {
111         $this->options['alter']['url'] = $entity->toUrl();
112         $this->options['alter']['make_link'] = TRUE;
113       }
114       catch (UndefinedLinkTemplateException $e) {
115         $this->options['alter']['make_link'] = FALSE;
116       }
117       catch (EntityMalformedException $e) {
118         $this->options['alter']['make_link'] = FALSE;
119       }
120     }
121
122     return $this->sanitizeValue($entity->label());
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function preRender(&$values) {
129     parent::preRender($values);
130
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);
135       }
136     }
137
138     foreach ($entity_ids_per_type as $type => $ids) {
139       $this->loadedReferencers[$type] = $this->entityManager->getStorage($type)->loadMultiple($ids);
140     }
141   }
142
143 }