Hi,

I'm trying to create my own theme suggestion for a block (ds_4col). I created a function in mytheme.theme:

function mytheme_theme_suggestions_ds_4col_alter(array &$suggestions, array $variables) {
  if(array_key_exists('#block_content', $variables['content']))
  {
	  $suggestions[] = $variables['theme_hook_original'].'__block-content__'.preg_replace("/[^a-zA-Z0-9]+/", '_', strtolower($variables['content']['#block_content']->label()));
	  $suggestions = array_reverse($suggestions);
  }
}

This works and I have these suggestions output:

<!-- FILE NAME SUGGESTIONS:
   * ds-4col--block-content--department-links.html.twig
   * ds-4col--block-content--2.html.twig
   * ds-4col--block-content-four-links-equal-columns-full.html.twig
   * ds-4col--block-content-four-links-equal-columns.html.twig
   * ds-4col--block-content-full.html.twig
   * ds-4col--block-content.html.twig
   x ds-4col.html.twig
-->

However, when I place a twig in my templates directory with the file name 'ds-4col--block-content--department-links.html.twig', it is not being picked up and DS uses the default 'ds-4col.html.twig' in it's own directory...

Any help would be greatly appreciated. Thanks!

Comments

telegraph created an issue. See original summary.

renaudcuny’s picture

Hi,

I face a quite similar issue with suggestion for DS fields:

<!-- FILE NAME SUGGESTIONS:
 ......
   * field--node--entityreference-field-field--node-ds-source-breadcrumb--aarhus-center.html.twig
   * field--node--entityreference-field-field--node-ds-source-breadcrumb.html.twig
   * field--node--aarhus-center.html.twig
   * field--entityreference-field-field--node-ds-source-breadcrumb.html.twig
   * field--ds.html.twig
   x field.html.twig
-->

field--node--entityreference-field-field--node-ds-source-breadcrumb--aarhus-center.html.twig is not being picked-up, and Drupal uses field.html.twig from my theme.

Have you found any solution?

jonodunnett’s picture

Also having the same issue with field templates. In fact I can use "field--ds.html.twig" suggestion, but the more specific templates are not picked up.

telegraph’s picture

I have not yet found a solution and ended up using one of the more basic template suggestions...

ABaier’s picture

I am facing something similar. The file name suggestions for a ds block field show up with a colon inside the pattern. I could not find a combination that got rendered.

<!-- FILE NAME SUGGESTIONS:
   * field--default--node--dynamic-block-field:node-article-video--article.html.twig
   * field--default--node--dynamic-block-field:node-article-video.html.twig
   * field--default--dynamic-block-field:node-article-video--article.html.twig
   * field--default--dynamic-block-field:node-article-video.html.twig
   * field--theme-ds-field-default.html.twig
   * field--node--dynamic-block-field:node-article-video--article.html.twig
   * field--node--dynamic-block-field:node-article-video.html.twig
   * field--node--article.html.twig
   * field--dynamic-block-field:node-article-video.html.twig
   * field--ds.html.twig
   x field.html.twig
-->

That problem also seems to affect the class names, if you look at the missing separator inside "dynamic-block-field|node-article-video", where the colon probably gets stripped out by |clean_class:

<div class="field field-node--dynamic-block-fieldnode-article-video field-formatter-ds-field field-name-dynamic-block-fieldnode-article-video field-type-ds">

Kint outputs the following:
file_name: "dynamic_block_field:node-article_video"
field_type: "ds"

Does somebody maybe know the correct file name pattern for this one? Thanks for your help!

renaudcuny’s picture

Hi toni4i,
For the colon problem, see: https://www.drupal.org/node/2792811
Best,
Renaud

swentel’s picture

Status: Active » Closed (duplicate)

So suggestions for the entity templates should be fine now after #2802429: Display layout twig not overridable. got in (this is for the 8.x-3.x branch for D8 8.3.x).
Field templates will be further investigated in #2856283: Overridden twig field templates have empty content.