You are here

class OffCanvas in Responsive and off-canvas menu 4.0.x

Same name and namespace in other branches
  1. 4.4.x src/OffCanvas.php \Drupal\responsive_menu\OffCanvas
  2. 4.1.x src/OffCanvas.php \Drupal\responsive_menu\OffCanvas
  3. 4.3.x src/OffCanvas.php \Drupal\responsive_menu\OffCanvas

Provides the HorizontalMenu block.

Plugin annotation


@Block(
  id = "responsive_menu_horizontal_menu",
  admin_label = @Translation("Horizontal menu")
)

Hierarchy

Expanded class hierarchy of OffCanvas

File

src/OffCanvas.php, line 15

Namespace

Drupal\responsive_menu
View source
class OffCanvas implements TrustedCallbackInterface {

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'preRender',
    ];
  }

  /**
   * Pre render callback to assemble the menu as markup.
   *
   * @param array $build
   *   The render array to modify.
   *
   * @return array
   *   The built render array.
   */
  public static function preRender(array $build) {
    $off_canvas_menus = \Drupal::config('responsive_menu.settings')
      ->get('off_canvas_menus');

    // Other modules can modify the menu names so we need to take this into
    // account when building the menu.
    \Drupal::ModuleHandler()
      ->alter('responsive_menu_off_canvas_menu_names', $off_canvas_menus);
    $menus = explode(',', $off_canvas_menus);
    $combined_tree = [];
    $menu_tree = \Drupal::menuTree();
    $manipulators = [
      // Show links to nodes that are accessible for the current user.
      [
        'callable' => 'menu.default_tree_manipulators:checkNodeAccess',
      ],
      // Only show links that are accessible for the current user.
      [
        'callable' => 'menu.default_tree_manipulators:checkAccess',
      ],
      // Use the default sorting of menu links.
      [
        'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
      ],
    ];

    // Iterate over the menus and merge them together.
    foreach ($menus as $menu_name) {
      $menu_name = trim($menu_name);
      $parameters = $menu_tree
        ->getCurrentRouteMenuTreeParameters($menu_name);

      // Force the entire tree to be build by setting expandParents to an
      // empty array.
      $parameters->expandedParents = [];
      $tree_items = $menu_tree
        ->load($menu_name, $parameters);
      $tree_manipulated = $menu_tree
        ->transform($tree_items, $manipulators);
      $combined_tree = array_merge($combined_tree, $tree_manipulated);
      $build['#cache']['contexts'][] = 'route.menu_active_trails:' . $menu_name;
      $build['#cache']['tags'][] = 'config:system.menu.' . $menu_name;
      $build['#cache']['tags'][] = 'offcanvas_render';
    }
    $menu = $menu_tree
      ->build($combined_tree);

    // Allow other modules to manipulate the built tree data.
    \Drupal::ModuleHandler()
      ->alter('responsive_menu_off_canvas_tree', $menu);
    $build['#markup'] = \Drupal::service("renderer")
      ->renderRoot($menu);
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OffCanvas::preRender public static function Pre render callback to assemble the menu as markup.
OffCanvas::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.