You are here

function toolbar_menu_update_8001 in Toolbar Menu 8

Same name and namespace in other branches
  1. 8.2 toolbar_menu.install \toolbar_menu_update_8001()

Transform all current toolbar_menu items to toolbar_menu_element entities.

File

./toolbar_menu.install, line 11
Contains installation and update hooks..

Code

function toolbar_menu_update_8001() {

  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  $config_factory = \Drupal::service('config.factory');

  /** @var \Drupal\Core\Entity\EntityTypeManager $entity_manager */
  $entity_manager = \Drupal::service('entity_type.manager');

  // Load old configuration and create a toolbar_menu_element entity
  // if is active.
  foreach ($config_factory
    ->get('toolbar_menu.settings')
    ->get('menus') as $menu_name => $menu_config) {
    if ($menu_config['active'] == TRUE && !$entity_manager
      ->getStorage('toolbar_menu_element')
      ->load($menu_name)) {
      $entity_manager
        ->getStorage('toolbar_menu_element')
        ->create([
        'id' => $menu_name,
        'label' => $menu_name,
        'menu' => $menu_name,
        'weight' => $menu_config['weight'],
        'rewrite_label' => TRUE,
      ])
        ->save();
    }
  }

  // Delete old and deprecated configuration.
  $config_factory
    ->getEditable('toolbar_menu.settings')
    ->delete();
}