You are here

public function RenderCheeseburgerMenuBlock::formatMenuArray in Cheeseburger Menu 8.4

Formats menu.

1 call to RenderCheeseburgerMenuBlock::formatMenuArray()
RenderCheeseburgerMenuBlock::getMenuTree in src/Controller/RenderCheeseburgerMenuBlock.php
Returning menu tree data.

File

src/Controller/RenderCheeseburgerMenuBlock.php, line 365
Controller used for rendering block.

Class

RenderCheeseburgerMenuBlock
Class RenderCheeseburgerMenuBlock.

Namespace

Drupal\cheeseburger_menu\Controller

Code

public function formatMenuArray($items) {
  $new_tree = [];
  foreach ($items as $menu_data) {
    $temp_array = [];
    if ($menu_data['url']
      ->isRouted()) {
      $temp_array['title'] = $menu_data['title'];
      if (method_exists($menu_data['url'], 'getRouteParameters')) {
        $temp_array['params'] = $menu_data['url']
          ->getRouteParameters();
      }
      $url = Url::fromRoute($menu_data['url']
        ->getRouteName(), $temp_array['params']);
      $temp_array['url'] = $url
        ->toString();
      $options = $menu_data['url']
        ->getOptions();
      if (isset($options['fragment'])) {
        $temp_array['url'] .= '#' . $options['fragment'];
      }
      $temp_array['route_name'] = $menu_data['url']
        ->getRouteName();
      $temp_array['entity_type_id'] = 'menu_item';
    }
    elseif ($menu_data['url']
      ->isExternal()) {
      $temp_array['title'] = $menu_data['title'];
      $temp_array['url'] = $menu_data['url']
        ->toUriString();
      $options = $menu_data['url']
        ->getOptions();
      if (isset($options['fragment'])) {
        $temp_array['url'] .= '#' . $options['fragment'];
      }
      $temp_array['route_name'] = 'external_link';
      $temp_array['entity_type_id'] = 'menu_item';
    }
    elseif (isset($menu_data['title']) && isset($menu_data['url'])) {
      if (strpos($menu_data['url']
        ->toUriString(), 'base:') !== FALSE) {
        $temp_array['title'] = $menu_data['title'];
        $temp_array['url'] = $menu_data['url']
          ->setAbsolute(TRUE)
          ->toString();
        $temp_array['route_name'] = 'external_link';
        $temp_array['entity_type_id'] = 'menu_item';
      }
    }
    else {
      $this->loggerChannelFactory
        ->get('cheeseburger_menu')
        ->warning('Cheeseburger menu was not able to recognize menu link as external or internal. Menu link name: ' . $menu_data['title'] . '. Maybe go and save menu link again on current language.');
      continue;
    }

    // For easier identifying when altering.
    if (isset($menu_data['original_link'])) {
      if (method_exists($menu_data['original_link'], 'getPluginId')) {
        $temp_array['plugin_id'] = $menu_data['original_link']
          ->getPluginId();
      }
    }
    $temp_array['children'] = [];
    if (array_key_exists('below', $menu_data)) {
      $temp_array['children'] = $this
        ->formatMenuArray($menu_data['below']);
    }
    $new_tree[] = $temp_array;
  }
  return $new_tree;
}