You are here

function ccl_contextual_links_view_alter in Custom Contextual Links 8

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

Implements hook_contextual_links_view_alter().

TODO:

  • Make sure all links are work in a multilanguage setup.

File

./ccl.module, line 106
Main CCL module file.

Code

function ccl_contextual_links_view_alter(&$element, $items) {

  // Get the destination parameter.
  $dest = drupal_get_destination();

  // Check if we have a node link to process.
  if (isset($element['#element']['#node']->nid)) {
    $node = $element['#element']['#node'];
    $node_cache = ccl_cache_get('ccl_nodes');

    // Global elements.
    foreach ($node_cache['global'] as $id => $link) {
      if ($processed_link = _ccl_prepare_link($link, $dest, $node)) {
        $element['#links']['ccl-global-node-' . $id] = $processed_link;
      }
    }

    // Content Type.
    if (in_array($node->type, array_keys($node_cache['ct']))) {
      foreach ($node_cache['ct'][$node->type] as $id => $link) {
        if ($processed_link = _ccl_prepare_link($link, $dest, $node)) {
          $element['#links']['ccl-ct-' . $id] = $processed_link;
        }
      }
    }

    // Individual nodes.
    if (in_array($node->nid, array_keys($node_cache['ids']))) {
      foreach ($node_cache['ids'][$node->nid] as $id => $link) {
        if ($processed_link = _ccl_prepare_link($link, $dest, $node)) {
          $element['#links']['ccl-node-' . $id] = $processed_link;
        }
      }
    }
  }

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