You are here

public function LayoutParagraphsBuilderFormatter::view in Layout Paragraphs 2.0.x

File

src/Plugin/Field/FieldFormatter/LayoutParagraphsBuilderFormatter.php, line 67

Class

LayoutParagraphsBuilderFormatter
Layout Paragraphs field formatter.

Namespace

Drupal\layout_paragraphs\Plugin\Field\FieldFormatter

Code

public function view(FieldItemListInterface $items, $langcode = NULL) {
  $elements = [
    '#theme' => 'layout_paragraphs_builder_formatter',
    '#root_components' => parent::view($items, $langcode),
  ];
  $entity = $items
    ->getEntity();
  if (!$entity
    ->access('update')) {
    return $elements['#root_components'];
  }

  /** @var \Drupal\Core\Entity\EntityDefintion $definition */
  $definition = $items
    ->getFieldDefinition();
  $layout = new LayoutParagraphsLayout($items, $this
    ->getSettings() + [
    'reference_field_view_mode' => $this->viewMode,
  ]);
  $this->tempstore
    ->set($layout);
  $layout = $this->tempstore
    ->get($layout);
  $elements['#link_text'] = $this
    ->t('Edit @label', [
    '@label' => $definition
      ->label(),
  ]);
  $elements['#link_url'] = Url::fromRoute('layout_paragraphs.builder.formatter', [
    'layout_paragraphs_layout' => $layout
      ->id(),
  ]);
  $elements['#field_label'] = $definition
    ->label();
  $elements['#type'] = 'container';
  $elements['#is_empty'] = count($layout
    ->getRootComponents()) == 0;
  $elements['#attributes'] = [
    'class' => [
      'lpb-formatter',
    ],
    'data-lpb-id' => $layout
      ->id(),
  ];
  $elements['#attached']['library'][] = 'layout_paragraphs/builder';
  $elements['#attached']['library'][] = 'layout_paragraphs/builder_form';
  return $elements;
}