You are here

LayoutParagraphsFormatter.php in Layout Paragraphs 2.0.x

Same filename and directory in other branches
  1. 1.0.x src/Plugin/Field/FieldFormatter/LayoutParagraphsFormatter.php

File

src/Plugin/Field/FieldFormatter/LayoutParagraphsFormatter.php
View source
<?php

namespace Drupal\layout_paragraphs\Plugin\Field\FieldFormatter;

use Drupal\entity_reference_revisions\Plugin\Field\FieldFormatter\EntityReferenceRevisionsEntityFormatter;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\layout_paragraphs\LayoutParagraphsComponent;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\Core\Cache\CacheableMetadata;

/**
 * Layout Paragraphs field formatter.
 *
 * @FieldFormatter(
 *   id = "layout_paragraphs",
 *   label = @Translation("Layout Paragraphs"),
 *   description = @Translation("Renders paragraphs with layout."),
 *   field_types = {
 *     "entity_reference_revisions"
 *   }
 * )
 */
class LayoutParagraphsFormatter extends EntityReferenceRevisionsEntityFormatter implements ContainerFactoryPluginInterface {

  /**
   * Returns the referenced entities for display.
   *
   * See \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::getEntitiesToView().
   *
   * @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items
   *   The item list.
   * @param string $langcode
   *   The language code of the referenced entities to display.
   *
   * @return \Drupal\Core\Entity\EntityInterface[]
   *   The array of referenced entities to display, keyed by delta.
   *
   * @see ::prepareView()
   */
  protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
    $entities = [];
    foreach ($items as $delta => $item) {

      // Ignore items where no entity could be loaded in prepareView() and
      // items that are not root level components.
      if (!empty($item->_loaded) && LayoutParagraphsComponent::isRootComponent($item->entity)) {
        $entity = $item->entity;
        $access = $this
          ->checkAccess($entity);

        // Add the access result's cacheability, ::view() needs it.
        $item->_accessCacheability = CacheableMetadata::createFromObject($access);
        if ($access
          ->isAllowed()) {

          // Set the entity in the correct language for display.
          if ($entity instanceof TranslatableInterface) {
            $entity = \Drupal::service('entity.repository')
              ->getTranslationFromContext($entity, $langcode);
          }

          // Add the referring item, in case the formatter needs it.
          $entity->_referringItem = $items[$delta];
          $entities[$delta] = $entity;
        }
      }
    }
    return $entities;
  }

}

Classes

Namesort descending Description
LayoutParagraphsFormatter Layout Paragraphs field formatter.