You are here

public function ViewfieldFormatterTitle::viewElements in Viewfield 8.3

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/ViewfieldFormatterTitle.php, line 23

Class

ViewfieldFormatterTitle
Plugin implementation of the 'viewfield_title' formatter.

Namespace

Drupal\viewfield\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    $value = $item
      ->getValue();
    $target_id = $value['target_id'];
    $display_id = $value['display_id'];
    $arguments = $value['arguments'];
    $view = Views::getView($target_id);
    $title = $view
      ->getTitle();
    $display = $view->displayHandlers
      ->get($display_id);
    if (!$display) {
      $elements[$delta] = [
        '#theme' => 'item_list',
        '#items' => [
          $this
            ->t('Missing or broken view/display'),
        ],
      ];
      continue;
    }
    $display_name = $display
      ->pluginTitle();
    $elements[$delta] = [
      '#theme' => 'item_list',
      '#items' => [
        $this
          ->t('<strong>View:</strong> @title (@id)', [
          '@title' => $title,
          '@id' => $target_id,
        ]),
        $this
          ->t('<strong>Display:</strong> @display (@id)', [
          '@display' => $display_name,
          '@id' => $display_id,
        ]),
        $this
          ->t('<strong>Arguments:</strong> @arguments', [
          '@arguments' => $arguments,
        ]),
      ],
    ];
  }
  return $elements;
}