1

I created a block on pages of the "Author" content type that lists all the articles the author has contributed to in a table (via contextual filter). Now I want to customize the table layout. I already did this before in a similar case, where I show the table on a page, not in a block. There I get file name suggestions for the template inside HTML comments like that:

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'views_view_table__articles' -->
<!-- FILE NAME SUGGESTIONS:
   x views-view-table--articles.html.twig
   x views-view-table--articles.html.twig
   * views-view-table.html.twig
-->
<!-- BEGIN OUTPUT from 'themes/MYTHEMEFOLDER/templates/views/views-view-table--articles.html.twig' -->

But in my latest case, the table in a block, I only get this suggestion:

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'views_view_table' -->
<!-- BEGIN OUTPUT from 'themes/MYPARENTTHEMEFOLDER/templates/views/views-view-table.html.twig' -->

Note that it outputs the template of my parent theme, not my actual theme.

I did some research and found this issue, which currently is not resolved and the proposed patch not included inside the Drupal code. Is this the reason I cannot create a custom template or is there something else I can do?

If not, how would I create that table of referenced articles on the author's page?

5
  • Did you apply the patch yes or no? You can apply patches to your project yourself. You could probably also take the code from the patch and create a custom module for it. You could install Devel or use Xdebug to see what's in the $variables when adding another template suggestion for the views table.
    – leymannx
    Mar 1, 2021 at 22:18
  • No, I didn't apply the patch. But after your suggestion I tried implementing the code of the patch in a custom module, but then I realized this patch is probably for something else. I need template suggestions for "block--views-block--XYZ", but this patch adds only template suggestions for "block--block-content--XYZ". How can I use Devel or Xdebug to see what's in $variables?
    – mtnair
    Mar 2, 2021 at 1:17
  • 1
    I now created a custom module which simply adds the missing template suggestion, although I'm not sure if it's the right way. It works, but I think this should be fixed in the Drupal core code...? pastebin.com/4U3CNEEi
    – mtnair
    Mar 2, 2021 at 2:44
  • If you think it's a bug or feature missing from core, please report it in the issue queue on drupal.org
    – Clive
    Mar 2, 2021 at 10:02
  • This definitely is the right way. Adding your own template suggestion in a custom module. Nice work! 👍 Consider adding that as a proper answer below.
    – leymannx
    Mar 2, 2021 at 13:47

1 Answer 1

1

Since Drupal doesn't suggest certain template file names I had to create a custom module which adds the template name for my needs. I had to implement the hook_theme_suggestions_HOOK() for my table view and add an appropriate name to the suggestions array.

<?php

use Drupal\views\ViewExecutable;

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function custom_template_suggestions_theme_suggestions_views_view_table_alter(array &$suggestions, array $variables) {
  if (isset($variables['view']) && $variables['view'] instanceof ViewExecutable) {
    $id = $variables['view']->current_display;

    if (!in_array('views_view_table__' . $id, $suggestions)) {
      $suggestions[] = 'views_view_table__' . $id;
    }
  }
}
1
  • (I think you can also have that in your theme, but not 100% sure.)
    – leymannx
    Mar 5, 2021 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.