You are here

public function ViewsReferenceFieldFormatter::viewElements in Views Reference Field 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/ViewsReferenceFieldFormatter.php \Drupal\viewsreference\Plugin\Field\FieldFormatter\ViewsReferenceFieldFormatter::viewElements()

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/ViewsReferenceFieldFormatter.php, line 70

Class

ViewsReferenceFieldFormatter
Field formatter for Viewsreference Field.

Namespace

Drupal\viewsreference\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    $view_name = $item
      ->getValue()['target_id'];
    $display_id = $item
      ->getValue()['display_id'];
    $data = unserialize($item
      ->getValue()['data'], [
      'allowed_classes' => FALSE,
    ]);
    $view = Views::getView($view_name);

    // Add an extra check because the view could have been deleted.
    if (!is_object($view)) {
      continue;
    }
    $view
      ->setDisplay($display_id);
    $enabled_settings = array_filter($this
      ->getFieldSetting('enabled_settings') ?? []);

    // Add properties to the view so our hook_views_pre_build() implementation
    // can alter the view. This is pretty hacky, but we need this to fix ajax
    // behaviour in views. The hook_views_pre_build() needs to know if the
    // view was part of a viewsreference field or not.
    $view->element['#viewsreference'] = [
      'data' => $data,
      'enabled_settings' => $enabled_settings,
    ];
    $view
      ->preExecute();
    $view
      ->execute($display_id);
    if (!empty($view->result) || !empty($view->empty) || !empty($view->exposed_widgets)) {

      // Show view if there are results or empty behaviour defined or exposed widgets.
      if ($this
        ->getSetting('plugin_types')) {

        // Add a custom template if the title is available.
        $title = $view
          ->getTitle();
        if (!empty($title)) {

          // If the title contains tokens, we need to render the view to
          // populate the rowTokens.
          if (strpos($title, '{{') !== FALSE) {
            $view
              ->render();
            $title = $view
              ->getTitle();
          }
          $elements[$delta]['title'] = [
            '#theme' => 'viewsreference__view_title',
            '#title' => $title,
          ];
        }
      }
      $render_array = $view
        ->buildRenderable($display_id, $view->args, FALSE);

      // The views_add_contextual_links() function needs the following
      // information in the render array in order to attach the contextual
      // links to the view.
      $render_array['#view_id'] = $view->storage
        ->id();
      $render_array['#view_display_show_admin_links'] = $view
        ->getShowAdminLinks();
      $render_array['#view_display_plugin_id'] = $view
        ->getDisplay()
        ->getPluginId();
      views_add_contextual_links($render_array, $render_array['#view_display_plugin_id'], $display_id);
      $elements[$delta]['contents'] = $render_array;
    }
  }
  return $elements;
}