You are here

class LayoutParagraphsFormatter in Layout Paragraphs 2.0.x

Same name and namespace in other branches
  1. 1.0.x src/Plugin/Field/FieldFormatter/LayoutParagraphsFormatter.php \Drupal\layout_paragraphs\Plugin\Field\FieldFormatter\LayoutParagraphsFormatter

Layout Paragraphs field formatter.

Plugin annotation


@FieldFormatter(
  id = "layout_paragraphs",
  label = @Translation("Layout Paragraphs"),
  description = @Translation("Renders paragraphs with layout."),
  field_types = {
    "entity_reference_revisions"
  }
)

Hierarchy

Expanded class hierarchy of LayoutParagraphsFormatter

File

src/Plugin/Field/FieldFormatter/LayoutParagraphsFormatter.php, line 24

Namespace

Drupal\layout_paragraphs\Plugin\Field\FieldFormatter
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContainerFactoryPluginInterface::create public static function Creates an instance of the plugin. 120
LayoutParagraphsFormatter::getEntitiesToView protected function Returns the referenced entities for display.