I have a taxonomy terms view, filtered by a vocabulary. I would like to add the dashes before the term name like in the Parent terms field on the edit term page. The code in TermForm.php does that when creating $options using str_repeat
based on the item's depth.
This is the code from TermForm.php:
$tree = $taxonomy_storage->loadTree($vocabulary->id());
$options = ['<' . $this->t('root') . '>'];
if (empty($parent)) {
$parent = [0];
}
foreach ($tree as $item) {
if (!in_array($item->tid, $exclude)) {
$options[$item->tid] = str_repeat('-', $item->depth) . $item->name;
}
}
}
I copied core/modules/views/templates/views-view-field.html.twig
to my module's templates directory and renamed it views-view-field--my-view-name--name.html.twig
I added hook_theme
to the module file to start using the template.
/**
* Implements hook_theme().
*/
function my_module_theme($existing, $type, $theme, $path) {
return [
'views_view_field__my_view_name__name'=> [
'render element' => 'elements',
'template' => 'views-view-field--my-view-name--name',
],
];
}
If I add some markup to the template, like <h3>hello</h3>
, hello
is printed on the View's page but, no data is being passed to the template.
Ultimately I need to get the term object and update it with
str_repeat('-', $item->depth) . $item->name
before passing the field to the template. I'm guessing I get the term object in a preprocessor and add the updated name
field to hook_theme?