I can see that "Content:Has taxonomy term (with Depth)" is available as a contextual filter.
But I would like to use the Group by function to display my Taxonomy Terms in their hierarchical form, if nothing else than to provide a menu to use to select the term for the contextual filter view.
Am I just not finding the built-in functionality to do that, or is this a feature request?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bunthorne created an issue. See original summary.

DamienMcKenna’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 8.x-3.x-dev » 8.6.x-dev
Component: taxonomy data » views.module

Views was moved into core in D8, so I'm moving the issue over to its issue queue.

bunthorne’s picture

I have figured out how to Group by a taxonomy depth by creating a Relationship for a Taxonomy term to its Parent, then adding a field for the Term that uses the parent Relationship. I can repeat this for a second level of relationship. I then have to Filter out terms that are empty so the higher levels don't repeat themselves in the view, but the whole process seems awkward. It would be much nicer to have a Taxonomy Depth value available as a field for Group by.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

cristiroma’s picture

In our case we wanted to group the terms by their root ancestors, so we did in the following manner: Add to the view fields the parent field and, configure for it a field formatter plugin that always returns the root ancestor label, then group the results by the parent label.

- Term R1
  - Term A.1
    - Term A.1.1
- Term R2
   - Term B.1
      - Term B.1.1

Will result:


Term R1
- Term A1
- Term A1.1

Term R2
- Term B.1
- Term B.1.1
Steps to implement

1. Create a field formatter view plugin that always returns the root parent label


namespace Drupal\judicial\Plugin\Field\FieldFormatter;

use Drupal\Core\Url;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
use Drupal\taxonomy\TermInterface;

/**
 * Plugin implementation of the 'entity reference taxonomy term RSS' formatter.
 *
 * @FieldFormatter(
 *   id = "entity_reference_root_term_name",
 *   label = @Translation("Root term name"),
 *   description = @Translation("Display label of the root term for this term ancestry."),
 *   field_types = {
 *     "entity_reference"
 *   }
 * )
 */
class RootTaxonomyTermNameFieldFormatter extends EntityReferenceFormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    /** @var \Drupal\taxonomy\Entity\Term $item */
    $item = $items->getEntity();
    $ret = '';
    if ($item instanceof TermInterface) {
      $ancestors = \Drupal::service('entity_type.manager')->getStorage("taxonomy_term")->loadAllParents($item->id());
      /** @var \Drupal\taxonomy\Entity\Term $root */
      $root = end($ancestors);
      $ret = $root->getName();
    }
    return [['#markup' => $ret]];
  }

  /**
   * {@inheritdoc}
   */
  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    // This formatter is only available for taxonomy terms.
    return $field_definition->getFieldStorageDefinition()->getSetting('target_type') == 'taxonomy_term';
  }
}

2. Add name and parent field to the list of fields

3. Set the plugin as field formatter for the parents field

4. Configure grouping by the parent field (which returns always to root label)

5. Save the view and check the listing

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.