You are here

public function CheeseburgerMenuService::getMenusAsRows in Cheeseburger Menu 5.0.x

Returns sortable menu rows for cheeseburger config form.

Parameters

array $default_value: Cheeseburger menu configuration.

Return value

array Sortable menu rows for cheeseburger config form.

File

src/CheeseburgerMenuService.php, line 83

Class

CheeseburgerMenuService
Base service providing functions.

Namespace

Drupal\cheeseburger_menu

Code

public function getMenusAsRows(array $default_value = []) {
  $rows = [];
  if ($this->entityTypeManager
    ->hasDefinition('menu')) {
    foreach ($this->entityTypeManager
      ->getStorage('menu')
      ->loadMultiple() as $id => $entity) {
      $rows[$id] = $this
        ->buildRowFromEntity($entity, isset($default_value[$id]) ? $default_value[$id] : []);
    }
  }
  if ($this->entityTypeManager
    ->hasDefinition('taxonomy_vocabulary')) {
    foreach ($this->entityTypeManager
      ->getStorage('taxonomy_vocabulary')
      ->loadMultiple() as $id => $entity) {
      $rows[$id] = $this
        ->buildRowFromEntity($entity, isset($default_value[$id]) ? $default_value[$id] : []);
    }
  }
  $rows['enabled_region'] = $this
    ->buildRegionRow($this
    ->t('Enabled'), 'enabled');
  $rows['hidden_region'] = $this
    ->buildRegionRow($this
    ->t('Hidden'), 'hidden');
  uasort($rows, function ($menu1, $menu2) use ($default_value) {
    if ($menu2['#menu_id'] === 'enabled') {
      return 1;
    }
    if ($menu1['#menu_id'] === 'enabled') {
      return -1;
    }
    if ($menu2['#menu_id'] === 'hidden') {
      return isset($default_value[$menu1['#menu_id']]) ? -1 : 1;
    }
    if ($menu1['#menu_id'] === 'hidden') {
      return isset($default_value[$menu2['#menu_id']]) ? 1 : -1;
    }
    return $menu1['#weight'] <=> $menu2['#weight'];
  });
  return $rows;
}