You are here

function entityqueue_views_pre_render in Entityqueue 8

Implements hook_views_pre_render().

Add contexual links to views before rendering.

File

./entityqueue.module, line 37
Allows users to collect entities in arbitrarily ordered lists.

Code

function entityqueue_views_pre_render(ViewExecutable $view) {

  // Do not add contextual link on view preview.
  if (\Drupal::moduleHandler()
    ->moduleExists('views_ui') && views_ui_contextual_links_suppress()) {
    return;
  }

  // Allow to disable the contextual links.
  if (!$view->display_handler
    ->getOption('show_admin_links')) {
    return;
  }

  // Get view display relationships.
  $relationships = $view->relationship;
  foreach ($relationships as $relationship) {
    if ($relationship->field == 'entityqueue_relationship') {
      $referenced_subqueues = (array) $relationship->options['limit_queue'];

      // Contextual links can handle only one set of links coming from a module,
      // so we'll have to settle for the first referenced queue.
      if (!empty($referenced_subqueues) && ($subqueue = EntitySubqueue::load(reset($referenced_subqueues)))) {
        $route_parameters = [
          'entity_queue' => $subqueue
            ->getQueue()
            ->id(),
          'entity_subqueue' => $subqueue
            ->id(),
        ];
        $view->element['#contextual_links']['entityqueue'] = [
          'route_parameters' => $route_parameters,
        ];
      }
    }
  }
}