You are here

function menu_attach_block_link in Menu Attach Block 7

Override theme_link().

Render a block inside a link.

@todo Implement a theme function to do the render.

Parameters

array $variables: Array of theme variables as would be passed to theme_link().

Return value

string HTML for a link with an attached block.

1 string reference to 'menu_attach_block_link'
menu_attach_block_theme_registry_alter in ./menu_attach_block.module
Implements hook_theme_registry_alter().

File

./menu_attach_block.module, line 72
Module to enable adding a block to menu item.

Code

function menu_attach_block_link(&$variables) {

  // Don't run all the block logic if we're told its not needed.
  if (isset($variables['menu_attach_block_no_render']) && $variables['menu_attach_block_no_render']) {
    return theme('menu_attach_block_link_default', $variables);
  }
  $block = FALSE;
  $options = $variables['options'];

  // If a block is attached to this menu item, load it.
  if (isset($options['menu_attach_block']) && !empty($options['menu_attach_block']['name'])) {
    $block = menu_attach_block_load_from_key($options['menu_attach_block']['name']);

    // Set default options for menus saved from previous versions of the module.
    // @see http://php.net/manual/en/language.operators.array.php
    $options['menu_attach_block'] += _menu_attach_block_defaults();
  }

  // Render a block if one is attached to this link.
  if ($block) {

    // Render the link with the block content afterwards.
    $link = '';

    // menu_attach_block gives users the option to select '<block>' as the menu
    // path for a link.
    if ($variables['path'] != '<block>' && $variables['path'] != '<droplink>') {

      // Build the link manually instead of using l() to avoid getting caught in
      // theme_link() again.
      $variables['menu_attach_block_no_render'] = TRUE;
      $link = theme('link', $variables);
    }
    else {

      // Remove link.
      $link = '';
    }
    if ($options['menu_attach_block']['use_ajax'] || !$options['menu_attach_block']['no_drop']) {
      $menu_classes = array(
        'menu-attach-block-drop-link',
      );
      if ($options['menu_attach_block']['use_ajax']) {
        $menu_classes[] = 'menu-ajax-enabled';
      }
      if ($variables['path'] == '<droplink>') {

        // Use menu item as drop link, add other class passed in on $variables.
        if (isset($variables['attributes']['class'])) {
          $menu_classes = array_merge($menu_classes, $variables['attributes']['class']);
        }
      }
      else {

        // Add class 'external' to "More" link to apply styles.
        $menu_classes[] = 'external';
      }
      if ($options['menu_attach_block']['on_hover']) {
        $menu_classes[] = 'expand-on-hover';
      }
      else {
        $menu_classes[] = 'expand-on-click';
      }

      // If 'expanded on load' set, add class 'dropped'.
      if (isset($variables['options']['menu_attach_block']['dropped']) && $variables['options']['menu_attach_block']['dropped']) {
        $menu_classes[] = 'dropped';
      }
      $attributes = array(
        'class' => $menu_classes,
        'data-block-id' => $options['menu_attach_block']['name'],
        'id' => $variables['path'] . '-drop-link-' . $variables['options']['menu_attach_block']['mlid'],
      );

      // Pass a space as the fragment parameter to get a <a href='# '> link.
      // @link http://bit.ly/zZXArS @endlink
      $l_options = array(
        'fragment' => ' ',
        'external' => TRUE,
        'attributes' => $attributes,
        'html' => $options['menu_attach_block']['allow_html'],
      );
      if ($variables['path'] == '<droplink>') {

        // Use menu item as drop link.
        $link .= l($variables['text'], '', $l_options);
      }
      else {
        $link .= PHP_EOL . l(t('More'), '', $l_options);
      }
      drupal_add_js(drupal_get_path('module', 'menu_attach_block') . '/menu_attach_block.js');
    }
    $block_output = '';

    // Get the block html if normal rendering is used (not ajax).
    if (!$options['menu_attach_block']['use_ajax']) {
      $block_output = menu_attach_block_block_render($block['module'], $block['delta']);
    }
    else {
      $block_output = '<span class="ajax-progress"><span class="throbber"></span></span>';
    }
    if (!empty($block_output)) {
      $variables = array(
        'orientation' => $options['menu_attach_block']['orientation'],
        'content' => $block_output,
        'block' => $block,
        'link' => $variables,
      );
      $block_output = theme('menu_attach_block_wrapper', $variables);
      return $link . $block_output;
    }
  }

  // Otherwise, pass through to the original theme function (likely theme_link).
  return theme('menu_attach_block_link_default', $variables);
}