You are here

public function SearchLinks::getLinks in Admin Toolbar 3.x

Same name and namespace in other branches
  1. 8.2 admin_toolbar_search/src/SearchLinks.php \Drupal\admin_toolbar_search\SearchLinks::getLinks()

Gets extra links for admin toolbar search feature.

Return value

array An array of link data for the JSON used for search.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

admin_toolbar_search/src/SearchLinks.php, line 100

Class

SearchLinks
Extra search links.

Namespace

Drupal\admin_toolbar_search

Code

public function getLinks() {
  $max_bundle_number = $this->config
    ->get('max_bundle_number');
  $additional_keys = $this->cacheContextManager
    ->convertTokensToKeys([
    'languages:' . LanguageInterface::TYPE_INTERFACE,
    'user.permissions',
  ])
    ->getKeys();
  $cid_parts = array_merge([
    'admin_toolbar_search:links',
  ], $additional_keys);
  $cid = implode(':', $cid_parts);
  if ($cache = $this->toolbarCache
    ->get($cid)) {
    return $cache->data;
  }
  $links = [];
  $cache_tags = [];
  $content_entities = $this
    ->getBundleableEntitiesList();

  // Adds common links to entities.
  foreach ($content_entities as $entities) {
    $content_entity_bundle = $entities['content_entity_bundle'];
    $content_entity = $entities['content_entity'];

    // Load the remaining items that were not loaded by the toolbar.
    $content_entity_bundle_storage = $this->entityTypeManager
      ->getStorage($content_entity_bundle);
    $bundles_ids = $content_entity_bundle_storage
      ->getQuery()
      ->range($max_bundle_number)
      ->execute();
    if (!empty($bundles_ids)) {
      $bundles = $this->entityTypeManager
        ->getStorage($content_entity_bundle)
        ->loadMultiple($bundles_ids);
      foreach ($bundles as $machine_name => $bundle) {
        $cache_tags = Cache::mergeTags($cache_tags, $bundle
          ->getEntityType()
          ->getListCacheTags());
        $tparams = [
          '@entity_type' => $bundle
            ->getEntityType()
            ->getLabel(),
          '@bundle' => $bundle
            ->label(),
        ];
        $label_base = $this
          ->t('@entity_type > @bundle', $tparams);
        $params = [
          $content_entity_bundle => $machine_name,
        ];
        if ($this
          ->routeExists('entity.' . $content_entity_bundle . '.overview_form')) {

          // Some bundles have an overview/list form that make a better root
          // link.
          $url = Url::fromRoute('entity.' . $content_entity_bundle . '.overview_form', $params);
          if ($url
            ->access()) {
            $url_string = $url
              ->toString();
            $links[] = [
              'labelRaw' => $label_base,
              'value' => $url_string,
            ];
          }
        }
        if ($this
          ->routeExists('entity.' . $content_entity_bundle . '.edit_form')) {
          $url = Url::fromRoute('entity.' . $content_entity_bundle . '.edit_form', $params);
          if ($url
            ->access()) {
            $url_string = $url
              ->toString();
            $links[] = [
              'labelRaw' => $label_base . ' > ' . $this
                ->t('Edit'),
              'value' => $url_string,
            ];
          }
        }
        if ($this->moduleHandler
          ->moduleExists('field_ui')) {
          if ($this
            ->routeExists('entity.' . $content_entity . '.field_ui_fields')) {
            $url = Url::fromRoute('entity.' . $content_entity . '.field_ui_fields', $params);
            if ($url
              ->access()) {
              $url_string = $url
                ->toString();
              $links[] = [
                'labelRaw' => $label_base . ' > ' . $this
                  ->t('Manage fields'),
                'value' => $url_string,
              ];
            }
          }
          if ($this
            ->routeExists('entity.entity_form_display.' . $content_entity . '.default')) {
            $url = Url::fromRoute('entity.entity_form_display.' . $content_entity . '.default', $params);
            if ($url
              ->access()) {
              $url_string = $url
                ->toString();
              $links[] = [
                'labelRaw' => $label_base . ' > ' . $this
                  ->t('Manage form display'),
                'value' => $url_string,
              ];
            }
          }
          if ($this
            ->routeExists('entity.entity_view_display.' . $content_entity . '.default')) {
            $url = Url::fromRoute('entity.entity_view_display.' . $content_entity . '.default', $params);
            if ($url
              ->access()) {
              $url_string = $url
                ->toString();
              $links[] = [
                'labelRaw' => $label_base . ' > ' . $this
                  ->t('Manage display'),
                'value' => $url_string,
              ];
            }
          }
          if ($this->moduleHandler
            ->moduleExists('devel') && $this
            ->routeExists('entity.' . $content_entity_bundle . '.devel_load')) {
            $url = Url::fromRoute($route_name = 'entity.' . $content_entity_bundle . '.devel_load', $params);
            if ($url
              ->access()) {
              $url_string = $url
                ->toString();
              $links[] = [
                'labelRaw' => $label_base . ' > ' . $this
                  ->t('Devel'),
                'value' => $url_string,
              ];
            }
          }
          if ($this
            ->routeExists('entity.' . $content_entity_bundle . '.delete_form')) {
            $url = Url::fromRoute('entity.' . $content_entity_bundle . '.delete_form', $params);
            if ($url
              ->access()) {
              $url_string = $url
                ->toString();
              $links[] = [
                'labelRaw' => $label_base . ' > ' . $this
                  ->t('Delete'),
                'value' => $url_string,
              ];
            }
          }
        }
      }
    }
  }

  // Add menu links.
  if ($this->moduleHandler
    ->moduleExists('menu_ui')) {
    $menus = $this->entityTypeManager
      ->getStorage('menu')
      ->loadMultiple();
    uasort($menus, [
      Menu::class,
      'sort',
    ]);
    $menus = array_slice($menus, $max_bundle_number);
    $cache_tags = Cache::mergeTags($cache_tags, [
      'config:menu_list',
    ]);
    foreach ($menus as $menu_id => $menu) {
      $route_name = 'entity.menu.edit_form';
      $params = [
        'menu' => $menu_id,
      ];
      $url = Url::fromRoute($route_name, $params);
      if ($url
        ->access()) {
        $url_string = $url
          ->toString();
        $links[] = [
          'labelRaw' => $this
            ->t('Menus > @menu_label', [
            '@menu_label' => $menu
              ->label(),
          ]),
          'value' => $url_string,
        ];
      }
      $route_name = 'entity.menu.add_link_form';
      $params = [
        'menu' => $menu_id,
      ];
      $url = Url::fromRoute($route_name, $params);
      if ($url
        ->access()) {
        $url_string = $url
          ->toString();
        $links[] = [
          'labelRaw' => $this
            ->t('Menus > @menu_label > ', [
            '@menu_label' => $menu
              ->label(),
          ]) . $this
            ->t('Add link'),
          'value' => $url_string,
        ];
      }
      $menus = [
        'admin',
        'devel',
        'footer',
        'main',
        'tools',
        'account',
      ];
      if (!in_array($menu_id, $menus)) {
        $route_name = 'entity.menu.delete_form';
        $params = [
          'menu' => $menu_id,
        ];
        $url = Url::fromRoute($route_name, $params);
        if ($url
          ->access()) {
          $url_string = $url
            ->toString();
          $links[] = [
            'labelRaw' => $this
              ->t('Menus > @menu_label > ', [
              '@menu_label' => $menu
                ->label(),
            ]) . $this
              ->t('Delete'),
            'value' => $url_string,
          ];
        }
      }
      if ($this->moduleHandler
        ->moduleExists('devel') && $this
        ->routeExists('entity.menu.devel_load')) {
        $route_name = 'entity.menu.devel_load';
        $params = [
          'menu' => $menu_id,
        ];
        $url = Url::fromRoute($route_name, $params);
        if ($url
          ->access()) {
          $url_string = $url
            ->toString();
          $links[] = [
            'labelRaw' => $this
              ->t('Menus > @menu_label > ', [
              '@menu_label' => $menu
                ->label(),
            ]) . $this
              ->t('Devel'),
            'value' => $url_string,
          ];
        }
      }
    }
  }
  $this->toolbarCache
    ->set($cid, $links, Cache::PERMANENT, $cache_tags);
  return $links;
}