Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x

EntityReferenceTaxonomyTermRssFormatter.php

Namespace

Drupal\taxonomy\Plugin\Field\FieldFormatter

File

core/modules/taxonomy/src/Plugin/Field/FieldFormatter/EntityReferenceTaxonomyTermRssFormatter.php

View source
<?php

namespace Drupal\taxonomy\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;

/**
 * Plugin implementation of the 'entity reference taxonomy term RSS' formatter.
 */
class EntityReferenceTaxonomyTermRssFormatter extends EntityReferenceFormatterBase {
    
    /**
     * {@inheritdoc}
     */
    public function viewElements(FieldItemListInterface $items, $langcode) {
        $parent_entity = $items->getEntity();
        $elements = [];
        foreach ($this->getEntitiesToView($items, $langcode) as $entity) {
            $parent_entity->rss_elements[] = [
                'key' => 'category',
                'value' => $entity->label(),
                'attributes' => [
                    'domain' => $entity->id() ? Url::fromRoute('entity.taxonomy_term.canonical', [
                        'taxonomy_term' => $entity->id(),
                    ], [
                        'absolute' => TRUE,
                    ])
                        ->toString() : '',
                ],
            ];
        }
        return $elements;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function isApplicable(FieldDefinitionInterface $field_definition) {
        // This formatter is only available for taxonomy terms.
        return $field_definition->getFieldStorageDefinition()
            ->getSetting('target_type') == 'taxonomy_term';
    }

}

Classes

Title Deprecated Summary
EntityReferenceTaxonomyTermRssFormatter Plugin implementation of the 'entity reference taxonomy term RSS' formatter.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal