You are here

function template_preprocess_mmenu in Mobile sliding menu 8

Same name and namespace in other branches
  1. 7.3 mmenu.module \template_preprocess_mmenu()
  2. 7 mmenu.module \template_preprocess_mmenu()
  3. 7.2 mmenu.module \template_preprocess_mmenu()

Processes variables for mmenu.tpl.php.

Most themes utilize their own copy of mmenu.tpl.php. The default is located inside "modules/mmenu/tpl/mmenu.tpl.php". Look in there for the full list of variables.

The $variables array contains the following arguments:

  • $mmenu
  • $id
  • $name
  • $blocks

See also

mmenu.tpl.php

File

./mmenu.module, line 704
Primarily Drupal hooks and global API functions to manipulate mmenus.

Code

function template_preprocess_mmenu(&$variables) {
  $variables['mmenu'] = $variables['elements']['#mmenu'];
  $variables['id'] = $variables['elements']['#mmenu']['name'];
  $variables['name'] = $variables['elements']['#mmenu']['name'];
  $variables['blocks'] = array();
  static $mmenu_tree;

  // Get existing menus list from the site.
  $system_menus = mmenu_menu_get_menus();
  foreach ($variables['mmenu']['blocks'] as $mmenu_block) {
    if (empty($mmenu_block['plugin_id'])) {
      continue;
    }
    list($block_module, $block_delta) = explode(':', $mmenu_block['plugin_id']);

    // When it is a menu block.
    if ($block_module == 'system_menu_block' && isset($system_menus[$block_delta])) {

      // Builds the menu tree for rendering markup.
      $menu_parameters = isset($mmenu_block['menu_parameters']) ? $mmenu_block['menu_parameters'] : array();
      $menu_parameters['conditions']['hidden'] = 0;
      if (!isset($mmenu_tree[$block_delta])) {
        $menu_tree = \Drupal::menuTree();
        $parameters = new MenuTreeParameters();
        $min_depth = isset($menu_parameters['min_depth']) ? $menu_parameters['min_depth'] : 1;
        $parameters
          ->setMinDepth($min_depth);
        $tree = $menu_tree
          ->load($block_delta, $parameters);
        $mmenu_tree[$block_delta] = $tree;
      }
      else {
        $tree = $mmenu_tree[$block_delta];
      }
      $block['content'] = array(
        '#theme' => 'mmenu_tree',
        '#tree' => $tree,
        '#reset' => TRUE,
        '#depth' => 1,
      );
    }
    else {

      // dpm($mmenu_block['plugin_id'], 'plugin_id');
      $block_manager = \Drupal::service('plugin.manager.block');

      // You can hard code configuration or you load from settings.
      $plugin_block = $block_manager
        ->createInstance($mmenu_block['plugin_id'], []);

      // Some blocks might implement access check.
      $access_result = $plugin_block
        ->access(\Drupal::currentUser());

      // Return empty render array if user doesn't have access.
      // $access_result can be boolean or an AccessResult class
      if (is_object($access_result) && $access_result
        ->isForbidden() || is_bool($access_result) && !$access_result) {

        // You might need to add some cache tags/contexts.
        $block['content'] = [
          '#markup' => '',
        ];
      }
      else {
        $content = $plugin_block
          ->build();
        if (!empty($content)) {
          $block['content'] = $content;
        }
      }
    }

    // Uses the mmenu block title if it was defined.
    // Otherwise, uses default block subject.
    $subject = '';
    if ($mmenu_block['title'] == '<none>') {
      $subject = '';
    }
    elseif (!empty($mmenu_block['title'])) {
      $subject = $mmenu_block['title'];
    }
    elseif (isset($block['subject'])) {
      $subject = $block['subject'];
    }
    else {
      $subject = '';
    }
    $new_block['subject'] = $subject;

    // Renders block content.
    $new_block['content'] = render($block['content']);

    // Checks if the block is collapsed or expanded.
    $new_block['collapsed'] = isset($mmenu_block['collapsed']) ? $mmenu_block['collapsed'] : TRUE;

    // Checks if the block needs to wrap by
    // '<ul><li><span>xxxxxx</span></li></ul>'.
    $new_block['wrap'] = isset($mmenu_block['wrap']) ? $mmenu_block['wrap'] : FALSE;

    // Don't render if block content is empty.
    if (!empty($new_block['content'])) {
      $variables['blocks'][] = $new_block;
    }
  }

  // Adds CSS for particular mmenu.
  mmenu_add_libraries($variables);
}