You are here

function ccl_contextual_links_view_alter in Custom Contextual Links 8.2

Same name and namespace in other branches
  1. 8 ccl.module \ccl_contextual_links_view_alter()
  2. 7 ccl.module \ccl_contextual_links_view_alter()

Implements hook_contextual_links_view_alter().

File

./ccl.module, line 13
Contains ccl.module.

Code

function ccl_contextual_links_view_alter(&$element, $items) {
  if (isset($element['#contextual_links']['node'])) {
    $nid = $element['#contextual_links']['node']['route_parameters']['node'];

    /** @var \Drupal\ccl\CclService $cclService */
    $cclService = Drupal::service('ccl.service');
    $node = Node::load($nid);
    $cache = $cclService
      ->getCachedLinks('ccl:nodes');

    // Global links.
    foreach ($cache['globalLinks'] as $id => $link) {
      if ($processedLink = $cclService
        ->prepareLink($link, $node)) {
        $element['#links']['ccl-node-global-' . $id] = $processedLink;
      }
    }

    // Content type specific links.
    $contentType = $node
      ->bundle();
    if ($contentType) {
      foreach ($cache['contentTypeLinks'][$contentType] as $id => $ct_link) {
        if ($processedLink = $cclService
          ->prepareLink($ct_link, $node)) {
          $element['#links']['ccl-node-ct-' . $id] = $processedLink;
        }
      }
    }

    // Individual nodes.
    if (isset($cache['nodeLinks'][$nid])) {
      foreach ($cache['nodeLinks'][$nid] as $id => $link) {
        if ($processedLink = $cclService
          ->prepareLink($link, $node)) {
          $element['#links']['ccl-node-id-' . $id] = $processedLink;
        }
      }
    }
  }

  // Invoke submodules.
  foreach (Drupal::moduleHandler()
    ->getImplementations('ccl_add_link') as $module) {
    $element = Drupal::moduleHandler()
      ->invoke($module, 'ccl_add_link', [
      $element,
    ]);
  }
}