Commit 77a49cc0 authored by Artem Dmitriiev's avatar Artem Dmitriiev
Browse files

Issue #3316018 by a.dmitriiev: Sort templates on "From Template" page

parent 27c84547
Loading
Loading
Loading
Loading
+22 −3
Original line number Diff line number Diff line
@@ -90,7 +90,7 @@ class TemplateController extends ControllerBase {
   * The controller for the content_templates.node.add route.
   *
   * @return array
   *   Add return description. @todo
   *   Render array for "From template" page.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
@@ -99,6 +99,7 @@ class TemplateController extends ControllerBase {
    $template_storage = $this->entityTypeManager()->getStorage('content_template');
    $query = $template_storage->getQuery();
    $query->condition('status', 1);
    $query->sort('name');
    $ids = $query->execute();
    $templates = $template_storage->loadMultiple($ids);
    $content_types = [
@@ -110,12 +111,16 @@ class TemplateController extends ControllerBase {
        'contexts' => [
          'user.permissions',
        ],
        'tags' => [
          'taxonomy_term_list:template_category',
        ],
      ],
      '#attached' => [
        'library' => 'content_templates/admin',
      ],
    ];
    if (!empty($templates)) {
      $category_weights = [];
      foreach ($templates as $template) {
        $bundle = $template->get('field_source')->entity->bundle();
        if (!$this->currentUser()
@@ -123,21 +128,35 @@ class TemplateController extends ControllerBase {
          continue;
        }
        $category_id = 0;
        $category = FALSE;
        if (!$template->get('field_category')->isEmpty()) {
          $category_id = $template->get('field_category')->target_id;
          /** @var \Drupal\taxonomy\TermInterface $category */
          $category = $template->get('field_category')->entity;
          if ($category) {
            $category_id = $category->id();
            $category_weights[$category_id] = $category->getWeight();
          }
        }
        if (empty($content_types[$category_id])) {
          $content_types[$category_id] = [
            '#theme' => 'item_list',
            '#attributes' => ['class' => 'category-list'],
            '#items' => [],
            '#title' => $category_id ? $template->get('field_category')->entity->label() : $this->t('Uncategorized'),
            '#title' => $category ? $category->label() : $this->t('Uncategorized'),
          ];
        }
        $content_types[$category_id]['#items'][] = $this->entityTypeManager()
          ->getViewBuilder('content_template')
          ->view($template);
      }
      if (!empty($category_weights)) {
        // Make the templates without category to be displayed as last.
        $category_weights[0] = 1000;
        asort($category_weights);
        $sorted_category_ids = array_keys($category_weights);
        // Sort templates and categories.
        $content_types = array_replace(array_flip($sorted_category_ids), $content_types);
      }
    }
    else {
      $content_types['empty'] = [