You are here

function ccl_local_actions_menu_local_tasks_alter in Custom Contextual Links 7

Same name and namespace in other branches
  1. 8 ccl_local_actions/ccl_local_actions.module \ccl_local_actions_menu_local_tasks_alter()

Implements hook_menu_local_tasks_alter().

File

ccl_local_actions/ccl_local_actions.module, line 110
Adds the ability to create local tasks with CCL.

Code

function ccl_local_actions_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  global $user;
  $dest = drupal_get_destination();
  $is_node = $root_path == 'node/%' ? TRUE : FALSE;
  $la_cache = ccl_cache_get('ccl_local_actions');

  // First narrow down the list of links that fit the visibility criteria.
  foreach ($la_cache as $id => $link) {

    // Check for user role restriction.
    if (array_filter($link['visibility']['roles']) && !array_intersect($link['visibility']['roles'], array_keys($user->roles))) {
      unset($la_cache[$id]);
      continue;
    }

    // Check for content type restrictions.
    if (array_filter($link['visibility']['types'])) {

      // If this is not a node continue.
      if (!$is_node) {
        unset($la_cache[$id]);
        continue;
      }
      if (!in_array($router_item['page_arguments'][0]->type, $link['visibility']['types'], TRUE)) {
        unset($la_cache[$id]);
        continue;
      }
    }

    // Check for page visibility.
    if ($link['visibility']['path'] == 1 && empty($link['visibility']['pages'])) {
      unset($la_cache[$id]);
      continue;
    }

    // Match path if necessary.
    if ($link['visibility']['pages']) {

      // Convert path to lowercase. This allows comparison of the same path
      // with different case. Ex: /Page, /page, /PAGE.
      $pages = drupal_strtolower($link['visibility']['pages']);
      $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));

      // Compare the lowercase internal and lowercase path alias (if any).
      $page_match = drupal_match_path($path, $pages);
      if ($path != $_GET['q']) {
        $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
      }

      // When $link['visibility']['path'] has a value of 0,
      // the link is displayed on all pages except those
      // listed in $link['visibility']['pages'].
      // When set to 1, it is displayed only on those pages
      // listed in $link['visibility']['pages'].
      $page_match = !($link['visibility']['path'] xor $page_match);
    }
    else {
      $page_match = TRUE;
    }
    if (!$page_match) {
      unset($la_cache[$id]);
    }
  }

  // If we are on a node, we need to send it through for token replacement.
  $node = $is_node ? $router_item['page_arguments'][0] : NULL;
  foreach ($la_cache as $link) {
    if ($processed_link = _ccl_prepare_link($link, $dest, $node)) {

      // As these links are passed through a different theme function
      // we have to adjust the values a bit.
      $processed_link['localized_options'] = array(
        'attributes' => $processed_link['attributes'],
        'query' => isset($processed_link['query']) ? $processed_link['query'] : '',
        'fragment' => isset($processed_link['fragment']) ? $processed_link['fragment'] : '',
      );
      $processed_link['localized_options']['attributes']['title'] = check_plain($processed_link['title']);
      $data['actions']['output'][] = array(
        '#theme' => 'menu_local_action',
        '#link' => $processed_link,
      );
      $data['actions']['count']++;
    }
  }
}