Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / taxonomy / src / Plugin / Field / FieldFormatter / EntityReferenceTaxonomyTermRssFormatter.php
1 <?php
2
3 namespace Drupal\taxonomy\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
8
9 /**
10  * Plugin implementation of the 'entity reference taxonomy term RSS' formatter.
11  *
12  * @FieldFormatter(
13  *   id = "entity_reference_rss_category",
14  *   label = @Translation("RSS category"),
15  *   description = @Translation("Display reference to taxonomy term in RSS."),
16  *   field_types = {
17  *     "entity_reference"
18  *   }
19  * )
20  */
21 class EntityReferenceTaxonomyTermRssFormatter extends EntityReferenceFormatterBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function viewElements(FieldItemListInterface $items, $langcode) {
27     $parent_entity = $items->getEntity();
28     $elements = [];
29
30     foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
31       $parent_entity->rss_elements[] = [
32         'key' => 'category',
33         'value' => $entity->label(),
34         'attributes' => [
35           'domain' => $entity->id() ? \Drupal::url('entity.taxonomy_term.canonical', ['taxonomy_term' => $entity->id()], ['absolute' => TRUE]) : '',
36         ],
37       ];
38     }
39
40     return $elements;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function isApplicable(FieldDefinitionInterface $field_definition) {
47     // This formatter is only available for taxonomy terms.
48     return $field_definition->getFieldStorageDefinition()->getSetting('target_type') == 'taxonomy_term';
49   }
50
51 }