Untitled

                Never    
PHP
       
<?php

namespace Drupal\custom_menu_ana\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Class DefaultController.
 */
class DefaultController extends ControllerBase {

  /**
   * Iniciar.
   *
   * @return string
   *   Return Hello string.
   */
  public function Iniciar() {

    /*
    *@intrucciones:
    Editar el arreglo $nombre_menus.
    */

    $nombre_menus = [
      ["id" => "custom_menu_drinux_1", "label" => "Custom Menu 1","Description" => "Menu custom drinux 1"],
      ["id" => "custom_menu_drinux_2", "label" => "Custom Menu 2","Description" => "Menu custom drinux 2"],
      ["id" => "custom_menu_drinux_3", "label" => "Custom Menu 3","Description" => "Menu custom drinux 3"],
      ["id" => "custom_menu_drinux_4", "label" => "Custom Menu 4","Description" => "Menu custom drinux 4"],
      ["id" => "custom_menu_drinux_5", "label" => "Custom Menu 5","Description" => "Menu custom drinux 5"],
      ["id" => "custom_menu_drinux_6", "label" => "Custom Menu 6","Description" => "Menu custom drinux 6"],
      ["id" => "custom_menu_drinux_7", "label" => "Custom Menu 7","Description" => "Menu custom drinux 7"],
      ["id" => "custom_menu_drinux_8", "label" => "Custom Menu 8","Description" => "Menu custom drinux 8"],
      ["id" => "custom_menu_drinux_9", "label" => "Custom Menu 9","Description" => "Menu custom drinux 9"],
      ["id" => "custom_menu_drinux_10", "label" => "Custom Menu 10","Description" => "Menu custom drinux 10"]
    ];

    foreach($nombre_menus as $info ) {
      $menu = \Drupal\system\Entity\Menu::create([
        'id' => $info["id"],
        'label' => $info["label"],
        'description' => $info["Description"],
      ]);
      if ($menu->save()) {

           $all_menus = \Drupal\system\Entity\Menu::loadMultiple();
            foreach ($all_menus as $id => $menu) {
              if($menu->id() == $info["id"]){
                 $items = [
                  ['id' => "1" , "label" => 'Organización', "link" => "", "weight" => "0" ,
                     "child" => [
                      ["id" => "11", "label" => "Antecedentes", "link" => "", "weight" => "0" ],
                      ["id" => "12", "label" => "Quiénes somos", "link" => "", "weight" => "1" ],
                      ["id" => "13", "label" => "Funciones", "link" => "", "weight" => "2" ],
                      ["id" => "13", "label" => "Instrumentos de gestión gestión", "link" => "", "weight" => "3" ],
                      ["id" => "13", "label" => "Ámbito", "link" => "", "weight" => "4" ],
                      ["id" => "13", "label" => "Actores", "link" => "", "weight" => "5" ],
                      ["id" => "13", "label" => "Grupos de técnicos de trabajo", "link" => "", "weight" => "6" ],
                      ["id" => "13", "label" => "Grupos territorial", "link" => "", "weight" => "7" ],
                      ["id" => "13", "label" => "Agenda", "link" => "", "weight" => "8" ],
                      ["id" => "13", "label" => "Contacto", "link" => "","weight" => "9"]
                    ]
                  ],
                  ['id' => "2" , "label" => 'Nuestra Cuenca', "link" => "" ,"weight" => "1" ,
                    "child" => [
                      ["id" => "21", "label" => "Ubicación geográfíca", "link" => "", "weight" => "0"],
                      ["id" => "22", "label" => "Hidrografía", "link" => "", "weight" => "1"],
                      ["id" => "23", "label" => "Sistema Hidráulico", "link" => "", "weight" => "2"],
                      ["id" => "23", "label" => "Fisiografía", "link" => "", "weight" => "3"],
                      ["id" => "23", "label" => "Ocupación de suelo", "link" => "", "weight" => "4"],
                      ["id" => "23", "label" => "Climatología", "link" => "", "weight" => "5"],
                      ["id" => "23", "label" => "Hidrogeología", "link" => "", "weight" => "6"]
                    ]
                  ],
                  ['id' => "3" , "label" => 'Estado Situacional de los Recursos Hídricos', "link" => "" ,"weight" => "2" ,
                    "child" => [
                      ["id" => "31", "label" => "Oferta", "link" => "", "weight" => "0"],
                      ["id" => "32", "label" => "Calidad", "link" => "", "weight" => "1"],
                      ["id" => "32", "label" => "Demanda de Uso", "link" => "", "weight" => "2"],
                      ["id" => "32", "label" => "Balance Hídrico", "link" => "", "weight" => "3"],
                      ["id" => "33", "label" => "Cuentas del Agua", "link" => "", "weight" => "4"]
                    ]
                  ],
                  ['id' => "4" , "label" => 'Factores de Presión', "link" => "","weight" => "3" ,
                    "child" => [
                      ["id" => "41", "label" => "Crecimiento poblacional", "link" => "", "weight" => "0"],
                      ["id" => "41", "label" => "Crecimiento urbano", "link" => "", "weight" => "1"],
                      ["id" => "41", "label" => "Explotación de acuíferos", "link" => "", "weight" => "2"],
                      ["id" => "41", "label" => "Vertimientos y contaminación", "link" => "", "weight" => "3"],
                      ["id" => "41", "label" => "Amenazas climáticas", "link" => "", "weight" => "4"],
                      ["id" => "41", "label" => "Gestión de Conflictos Sociales por el agua", "link" => "", "weight" => "5"],
                      ["id" => "41", "label" => "Denuncias ambientales por el agua", "link" => "", "weight" => "6"]
                    ]
                  ],
                  ['id' => "5" , "label" => 'Acciones de Respuesta', "link" => "","weight" => "4" ,
                    "child" => [
                      ["id" => "51", "label" => "Avances institucionales", "link" => "", "weight" => "0"],
                      ["id" => "51", "label" => "Infraestructura", "link" => "", "weight" => "1"],
                      ["id" => "51", "label" => "Medidas de adaptación al cambío climático", "link" => "", "weight" => "2"],
                      ["id" => "51", "label" => "Sistema de monitoreo", "link" => "", "weight" => "3"],
                      ["id" => "51", "label" => "Proyecto de cooperación", "link" => "", "weight" => "4"]
                    ]
                  ],
                  ['id' => "6" , "label" => 'Cultura del Agua', "link" => "","weight" => "5" ,
                    "child" => [
                      ["id" => "61", "label" => "Participación del género femenino en la gestión del agua", "link" => "", "weight" => "0"],
                      ["id" => "61", "label" => "Mapa de actores", "link" => "", "weight" => "1"],
                      ["id" => "62", "label" => "Red de comunicadores hídricos", "link" => "", "weight" => "2"]
                    ]
                  ],
                  ['id' => "7" , "label" => 'Geovisor del CRHC', "link" => "","weight" => "6" ,
                    "child" => [
                      ["id" => "71", "label" => "Información cartográfica descargable de los estudios", "link" => "", "weight" => "0"]
                    ]
                  ],
                ];
                foreach($items as $nid ) {
                  $uri =  ($nid["link"] != "")? $nid["link"] : 'internal:/';

                  $menu_link = \Drupal\menu_link_content\Entity\MenuLinkContent::create([
                    'title' => $nid["label"],
                    'link' => ['uri' => $uri],
                    'menu_name' => $info["id"],
                    'expanded' => TRUE,
                    'weight' =>  $nid["weight"],
                  ]);
                  $menu_link->save();
                  if (count($nid["child"]) > 0 ) {
                    $mid = $menu_link->getPluginId();
                    foreach ($nid["child"] as $nidd ) {
                      $uri_ =  ($nidd["link"] != "")? $nidd["link"] : 'internal:/';
                      $menu_link_2 = \Drupal\menu_link_content\Entity\MenuLinkContent::create([
                        'title' => $nidd["label"],
                        'link' => ['uri' => $uri_],
                        'menu_name' => $info["id"],
                        'expanded' => TRUE,
                        'parent' => $mid,
                        'weight' =>  $nidd["weight"],
                      ]);
                      $menu_link_2->save();
                    }
                  }
                }

                break;
              }
            }

      }else{

      }
    }

  }//fin de f(x): Iniciar

}//fin de la calses