I need to create a custom admin view to replace the default taxonomy term listing page for a given vocabulary. The reason is that we have added custom fields on the vocabulary and the admins need to see those fields on the term listing page. To be clear, I am not talking about the individual term page that shows the content attached to that term, but the actual admin listing page that displays all of the terms for a given vocabulary. Obviously, I can create a view to do this, but I can not figure out how to preserve the drag and drop sorting functionality. I looked at the Draggable Views module, which I have used before, but that creates its own separate weight field and does not sort on the actual taxonomy term weight. Is there a better way to go about this? I just want to be able to sort the taxonomy weight in a view OR even better would be a way to just display the custom fields in the default taxonomy term listing screen, which I could not figure out how to do. I figured that was just a view that I could edit like the main content view, but I guess it is not. I couldn't figure out how to display the custom fields on that default admin taxonomy listing page.
-
It seems to be a known issue with Drupal8/9. Best solution seems to override the overview route. See this example (website is in French but code is self-explanatory) kgaut.net/snippets/2020/… or this thread drupal.org/project/drupal/issues/2975863#comment-12805681– misterdidiCommented Dec 27, 2021 at 17:11
Add a comment
|
1 Answer
We ended up creating a custom module that overrides the taxonomy overview form. I pasted our code here in case this helps someone else out. In our case we needed to add a field called Acronym, a field called Type and we also added the Status. You can adjust as needed. Our custom module was called mc_taxonomy and this was in the mc_taxonomy.module file.
<?php
use Drupal\field\FieldConfigInterface;
/**
* Implements hook_form_FORM_ID_alter().
*
* Add status, field_acronym, and field_type to the overview page.
*/
function mc_taxonomy_form_taxonomy_overview_terms_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Get the vocab from the form to read its field config data.
$vocab = $form_state->get(['taxonomy', 'vocabulary']);
$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('taxonomy_term', $vocab->id());
// Insert fields if they exist.
mc_taxonomy_overview_insert_field($form, 'status', 'Status', 1, function ($value) {
return $value ? 'Published' : 'Unpublished';
});
if (isset($fields['field_type'])) {
mc_taxonomy_overview_insert_field($form, $fields['field_type']);
}
if (isset($fields['field_acronym'])) {
mc_taxonomy_overview_insert_field($form, $fields['field_acronym']);
}
}
function mc_taxonomy_overview_insert_field(&$form, $field, $label = null, $index = 1, $valueFunction = null) {
if ($index <= 0) {
// TODO Allow index == 0.
// TODO Check upper bound.
throw new Exception('index must be >= 1.');
}
// Extract data from $field if it's the right type.
if ($field instanceof FieldConfigInterface) {
if (!$label) {
$label = $field->label();
}
$fieldName = $field->getName();
} else {
$fieldName = $field;
}
// Add field to the header.
$form['terms']['#header'] = array_merge(
array_slice($form['terms']['#header'], $index - 1, $index, TRUE),
[$label],
array_slice($form['terms']['#header'], $index, NULL, TRUE)
);
foreach ($form['terms'] as &$term) {
// Find terms within the render array.
if (is_array($term) && !empty($term['#term'])) {
// Add field to the term for the body.
$fieldValue = $term['#term']->get($fieldName)->value;
$term = array_merge(
array_slice($term, $index - 1, $index, TRUE),
[
$fieldName => [
'#markup' => $valueFunction ? $valueFunction($fieldValue) : $fieldValue,
'#type' => 'item',
]
],
array_slice($term, $index, NULL, TRUE)
);
}
}
}