Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldFormatter / EntityReferenceLabelFormatter.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\Core\Form\FormStateInterface;
9
10 /**
11  * Plugin implementation of the 'entity reference label' formatter.
12  *
13  * @FieldFormatter(
14  *   id = "entity_reference_label",
15  *   label = @Translation("Label"),
16  *   description = @Translation("Display the label of the referenced entities."),
17  *   field_types = {
18  *     "entity_reference"
19  *   }
20  * )
21  */
22 class EntityReferenceLabelFormatter extends EntityReferenceFormatterBase {
23
24   /**
25    * {@inheritdoc}
26    */
27   public static function defaultSettings() {
28     return [
29       'link' => TRUE,
30     ] + parent::defaultSettings();
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function settingsForm(array $form, FormStateInterface $form_state) {
37     $elements['link'] = [
38       '#title' => t('Link label to the referenced entity'),
39       '#type' => 'checkbox',
40       '#default_value' => $this->getSetting('link'),
41     ];
42
43     return $elements;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function settingsSummary() {
50     $summary = [];
51     $summary[] = $this->getSetting('link') ? t('Link to the referenced entity') : t('No link');
52     return $summary;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function viewElements(FieldItemListInterface $items, $langcode) {
59     $elements = [];
60     $output_as_link = $this->getSetting('link');
61
62     foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
63       $label = $entity->label();
64       // If the link is to be displayed and the entity has a uri, display a
65       // link.
66       if ($output_as_link && !$entity->isNew()) {
67         try {
68           $uri = $entity->urlInfo();
69         }
70         catch (UndefinedLinkTemplateException $e) {
71           // This exception is thrown by \Drupal\Core\Entity\Entity::urlInfo()
72           // and it means that the entity type doesn't have a link template nor
73           // a valid "uri_callback", so don't bother trying to output a link for
74           // the rest of the referenced entities.
75           $output_as_link = FALSE;
76         }
77       }
78
79       if ($output_as_link && isset($uri) && !$entity->isNew()) {
80         $elements[$delta] = [
81           '#type' => 'link',
82           '#title' => $label,
83           '#url' => $uri,
84           '#options' => $uri->getOptions(),
85         ];
86
87         if (!empty($items[$delta]->_attributes)) {
88           $elements[$delta]['#options'] += ['attributes' => []];
89           $elements[$delta]['#options']['attributes'] += $items[$delta]->_attributes;
90           // Unset field item attributes since they have been included in the
91           // formatter output and shouldn't be rendered in the field template.
92           unset($items[$delta]->_attributes);
93         }
94       }
95       else {
96         $elements[$delta] = ['#plain_text' => $label];
97       }
98       $elements[$delta]['#cache']['tags'] = $entity->getCacheTags();
99     }
100
101     return $elements;
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   protected function checkAccess(EntityInterface $entity) {
108     return $entity->access('view label', NULL, TRUE);
109   }
110
111 }