Commit 0b7bee7f authored by Tim Diels's avatar Tim Diels
Browse files

Issue #3324753 by tim-diels, Steven Dullaert: Add collections support for taxonomies

parent d78e1590
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -36,3 +36,34 @@ function hook_content_type_collections() {
    ]
  ];
}

/**
 * Provide an array describing vocabulary collections.
 *
 * Collections can group vocabularies together in the admin ui under the
 * categories menu.
 *
 * @return array
 *   [
 *     collection_machinename => [
 *       'label' => 'English label',
 *       'vocabularies' => [
 *           'bundle name vocabulary',
 *           ...
 *           'bundle name vocabulary'
 *       ]
 *     ],
 *     ...
 *   ]
 *
 */
function hook_vocabularies_collections() {
  return [
    'categories' => [
      'label' => 'Categories',
      'vocabularies' => [
        'tags',
      ],
    ],
  ];
}
+60 −7
Original line number Diff line number Diff line
@@ -67,19 +67,71 @@ class CategoriesMenuLinkDerivative extends DeriverBase implements ContainerDeriv
      /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager */
      $entityTypeManager = \Drupal::service('entity_type.manager');

      $vocabularies_collections = \Drupal::service('module_handler')
        ->invokeAll('vocabularies_collections');

      // Sort collections so grouping will use alphabetic order.
      asort($vocabularies_collections);

      $group_collections = $config->get('group_collections') ?? 0;
      if ($group_collections) {
        switch ($group_collections) {
          case 'bottom':
            $group_collections_weight = 100;
            break;

          case 'top':
            $group_collections_weight = -100;
            break;
        }
      }

      // Add a list terms for each vocabulary.
      $vocabularies = $entityTypeManager->getStorage('taxonomy_vocabulary')
        ->loadMultiple();

      foreach ($vocabularies_collections as $collection => $vocabulary_collections) {
        $links['categories.' . $collection] = [
            'title' => $this->t((string) $vocabulary_collections['label']),
            'route_name' => '<nolink>',
            'menu_name' => 'admin',
            'parent' => $base_plugin_definition['id'] . ':categories',
            'options' => [
              'attributes' => [
                'class' => [
                  'admin-toolbar-collection',
                  'admin-toolbar-collection--vocabulary',
                ],
              ],
            ],
          ] + $base_plugin_definition;

        // Use group collections weight if group collections is enabled.
        if (!empty($group_collections_weight)) {
          $links['categories.' . $collection]['weight'] = $group_collections_weight;
          $group_collections_weight++;
        }

        foreach ($vocabulary_collections['vocabularies'] as $vocabulary_id) {
          if (isset($vocabularies[$vocabulary_id])) {
            $this->addVocabularyLink($vocabularies[$vocabulary_id], $collection, $links, $base_plugin_definition);
            unset($vocabularies[$vocabulary_id]);
          }
        }
      }

      $collection = 'categories';
      foreach ($vocabularies as $vocabulary) {
        $this->addVocabularyLink($vocabulary, $links, $base_plugin_definition);
        $this->addVocabularyLink($vocabulary, $collection, $links, $base_plugin_definition);
      }
    }

    return $links;
  }

  protected function addVocabularyLink($vocabulary, &$links, $base_plugin_definition) {
  protected function addVocabularyLink($vocabulary, $collection, &$links, $base_plugin_definition) {

    $link_name = $collection . '.' . $vocabulary->id();

    $label = $vocabulary->label();
    if ($this->languageManager->isMultilingual()) {
@@ -87,28 +139,29 @@ class CategoriesMenuLinkDerivative extends DeriverBase implements ContainerDeriv
      $label = !empty($typeConfig->get('name')) ? $typeConfig->get('name') : $label;
    }

    $links['categories.' . $vocabulary->id()] = [
    $links['categories.' . $link_name] = [
      'title' => $this->t($label),
      'route_name' => 'entity.taxonomy_vocabulary.overview_form',
      'route_parameters' => [
        'taxonomy_vocabulary' => $vocabulary->id()
        'taxonomy_vocabulary' => $vocabulary->id(),
        'collection' => $collection,
      ],
      'menu_name' => 'admin',
      'parent' =>  $base_plugin_definition['id'] . ':' . 'categories',
      'parent' => !empty($links['categories.' . $collection]) ? $base_plugin_definition['id'] . ':categories.' . $collection : $base_plugin_definition['id'] . ':categories',
      'metadata' => [
        'entity_type' => 'taxonomy_vocabulary',
        'entity_id' => $vocabulary->id()
      ]
    ] + $base_plugin_definition;

    $links['categories.' . $vocabulary->id() . '.add'] = [
    $links['categories.' . $link_name . '.add'] = [
        'title' => $this->t('Add new'),
      'route_name' => "entity.taxonomy_term.add_form",
      'route_parameters' => [
        'taxonomy_vocabulary' => $vocabulary->id()
      ],
      'menu_name' => 'admin',
      'parent' =>  $base_plugin_definition['id'] . ':' . 'categories.' . $vocabulary->id(),
      'parent' =>  $base_plugin_definition['id'] . ':categories.' . $link_name,
      'metadata' => [
        'entity_type' => 'taxonomy_vocabulary',
        'entity_id' => $vocabulary->id()
+2 −0
Original line number Diff line number Diff line
@@ -93,7 +93,9 @@ class ContentMenuLinkDerivative extends DeriverBase implements ContainerDeriverI
            'options' => [
              'attributes' => [
                'class' => [
                  'admin-toolbar-collection',
                  'admin-toolbar-content-collection',
                  'admin-toolbar-collection--content',
                ],
              ],
            ],