Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\taxonomy\Plugin\field\formatter\EntityReferenceTaxonomyTermRssFormatter.
*/
Alex Pott
committed
namespace Drupal\taxonomy\Plugin\Field\FieldFormatter;
Dries Buytaert
committed
use Drupal\Core\Field\FieldDefinitionInterface;
Alex Pott
committed
use Drupal\Core\Field\FieldItemListInterface;
Alex Pott
committed
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
Dries Buytaert
committed
/**
* Plugin implementation of the 'entity reference taxonomy term RSS' formatter.
*
Alex Pott
committed
* @FieldFormatter(
Dries Buytaert
committed
* id = "entity_reference_rss_category",
* label = @Translation("RSS category"),
* description = @Translation("Display reference to taxonomy term in RSS."),
* field_types = {
* "entity_reference"
* }
* )
*/
class EntityReferenceTaxonomyTermRssFormatter extends EntityReferenceFormatterBase {
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function viewElements(FieldItemListInterface $items) {
Dries Buytaert
committed
$elements = array();
foreach ($this->getEntitiesToView($items) as $delta => $entity) {
Dries Buytaert
committed
$entity->rss_elements[] = array(
'key' => 'category',
'value' => $entity->label(),
Dries Buytaert
committed
'attributes' => array(
'domain' => $entity->id() ? \Drupal::url('entity.taxonomy_term.canonical', ['taxonomy_term' => $entity->id()], array('absolute' => TRUE)) : '',
Dries Buytaert
committed
),
);
}
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';
}