I need to add a column to the table shown in the page listing the vocabularies defined for a site. How can I achieve this?
1 Answer
You would need to override the class generating the list builder output on this page, then fill in the methods with what you want:
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = t('Vocabulary name');
$header['foo'] = t('Custom Header');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity->label();
$row['foo'] = 'custom_value';
return $row + parent::buildRow($entity);
}
See this related question: Can you change the list builder class used by an entity?
So, in essence:
function mymodule_entity_type_alter(array &$entity_types) {
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
$entity_types['taxonomy_vocabulary']->setListBuilderClass('Drupal\mymodule\VocabularyListBuilder');
}
Then, in your mymodule/src folder, create a VocabularyListBuilder.php, and copy in from the core/modules/taxonomy/src/VocabularyListBuilder.php and modify the two methods above for your output (since they'll be basically identical).
You could also do this with a View page as well, but for archival purposes wanted to explain how this works in general for Drupal core. List builder classes are such a cool and useful new D8 thing.