123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace Drupal\materio_samples\Plugin\Field\FieldFormatter;
- use Drupal\Core\Field\FormatterBase;
- use Drupal\Core\Field\FieldItemListInterface;
- use Drupal\taxonomy\Entity\Term;
- /**
- * Plugin implementation of the 'materio_samples_default_formatter' formatter.
- *
- * @FieldFormatter(
- * id = "materio_samples_default_formatter",
- * module = "materio_samples",
- * label = @Translation("Simple list key paired formatter"),
- * field_types = {
- * "materio_samples_field"
- * }
- * )
- */
- class SamplesDefaultFormatter extends FormatterBase {
- /**
- * {@inheritdoc}
- */
- public function viewElements(FieldItemListInterface $items, $langcode) {
- $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
- $elements = [];
- foreach ($items as $delta => $item) {
- // return nothing if target_id is null
- if(!$item->target_id) return;
- $term = Term::load($item->target_id);
- // translate the term
- $term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $language);
- $elements[$delta] = [
- // We create a render array to produce the desired markup,
- // "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
- // See theme_html_tag().
- '#type' => 'html_tag',
- '#tag' => 'span',
- '#value' => $this->t('@target : @location', [
- '@target' => $term->getName(),
- '@location' => $item->location
- ]
- ),
- ];
- }
- return $elements;
- }
- }
|