As the title says, I have encountered a strange bug, where the preprocess hook is not executed after changing the view mode of the paragraph in a different preprocess function. The case gets even stranger, because this issue only happens on some environments and machines. My guess is that it's related to the operating system (it seems to run fine on Linux, not on Mac or Windows).

I have a nested paragraph setup, a parent called "section", and a child "text_image_component".
Both of these paragraph types have a preprocess hook, but if I modify the view mode of "text_image_component" in the other hook, it's preprocess hook will not be registered.

This is the code:

function ekw_preprocess_paragraph__section(&$variables) {
  foreach ($variables['content']['field_ref_components'] as $key => $reference) {
    if (is_int($key)) {
      if ($reference['#paragraph']->getType() === 'text_image_component') {
        $image_size = $reference['#paragraph']->get('field_image_size')
          ->getValue();
        if ($image_size && $image_size[0]['value'] === 'small') {
          // If I comment out the line below, the code works.
          $variables['content']['field_ref_components'][$key]['#view_mode'] = 'alternative';
        }
      }
    }
  }
}

function ekw_preprocess_paragraph__text_image_component(&$variables) {
  // Some code will not be executed :(
}

A workaround is to create an explicit template for the default view mode:
paragraph--text-image-component--default.html.twig

Does anyone have an idea what could be happening?

Comments

kkri created an issue. See original summary.

miro_dietiker’s picture

Triple check case of filenames where these hooks live in. Some systems are not case sensitive and things still work if these don't match.
On linux though (with case sensitive filesystem which is the default) it needs to match exactly.

kkri’s picture

Thanks for the advice, I didn't know that before. Sadly - or luckily, depending on how you look at it -, there is nothing wrong with the naming. Both hooks are in the same file, right underneath each other.

To make the bug more clear, I had a file called "paragraph--text-image-component.html.twig" and the preprocess hook started working when the template was renamed to "paragraph--text-image-component--default.html.twig".

There seems to be a different behaviour on how Drupal handles switching between the default and a specified Paragraph view mode based on the system setup.

StephenN’s picture

Have experienced exactly the same issue. Drupal 8.5 Only occurs on one environment, despite having two identical AWS environments provisioned with the same Ansible script.

In my case I had a template file without a view mode and one with. When altering the view mode (in hook_entity_view_mode_alter ) the preprocessor was not run for the mode with the custom template, although the template file itself was in use. Fix was to explicitly name template files for every view mode in use, including --default.

miro_dietiker’s picture

Status: Active » Postponed (maintainer needs more info)

I'm not so sure this is a Paragraphs bug.

This seems to be triggered very rarely and reproduction is hard without any failing test - also because we maintain Paragraphs on Linux dev environments.

It seems you need to do more debugging with the misbehaving machine with xdebug to look into the inner states of the registry and figure out what's missing. Chances are this is a race condition of items that have no defined order.

We will need to know more to get this resolved.

hdotnet’s picture

Same/similar problem for me, however the view mode is changed by a view and not in code.

The preprocess hook worked locally but never in production.

My workaround was to create twig filters and functions to do the processing. If we ever manage to recreate the bug locally we'll trace to see what's happening.

Etroid’s picture

This is not just limited to paragraphs, I have encountered the same issue with other entities (e.g. node). From what I could uncover stepping through the theme registry process is that a pre-process hook will only be called if a template exists for it. This becomes especially noticeable when you start enabling view modes for a given entity.

Related: https://www.drupal.org/project/drupal/issues/3085525

ericdsd’s picture

Version: 8.x-1.1 » 8.x-1.12

I had the same issue with core 8.9.12 and Paragraphs 8.x-1.12
The workarround worked fine, note that you need to rename not only copy the twig file.

Rename paragraph--text-image-component.html.twig to paragraph--text-image-component--default.html.twig WORKS
Copy paragraph--text-image-component.html.twig to paragraph--text-image-component--default.html.twig DOESN'T WORK

t_stallmann’s picture

So glad to find this thread! I'm having the same issue with core 8.9.13 -- I have a content type, let's call it info_page, and have theme hook template_preprocess_node__info_page defined in my theme file. Locally (on ddev), this hook runs for all view modes. In production, it does not run in teaser mode. Creating a stub hook fixed it, e.g.

function template_preprocess_node__info_page__teaser(&$variables) {
  template_preprocess_node__info_page($variables);
}

But it took several hours of pulling my hair out trying to understand what differences bxt dev and production might cause this, before I found this thread!

johnchque’s picture

Version: 8.x-1.12 » 8.x-1.x-dev

Bug reports go always against the dev branch. :)

paulmckibben’s picture

Adding a data point. I have also run into this issue, where it works on my local but not on the hosting environment. Both environments are linux-based (DDEV for local, Acquia for hosting). Even stranger, the problem would occur on Acquia once in a while, but something unrelated would make it go away.

The workaround in #9 seems to fix it for me.

rocasey’s picture

Hi all,

I want to add my experience with this bug here as it was pretty head scratching and had I not found this thread I don't think I ever would have solved it.

Some Stats:
Drupal 9.2.10 (but the problem occurred throughout 8.9 to current version).

We use Pantheon for hosting.

We noticed the issue start by content not displaying on our test environment, but displaying on our live environment.

Pantheon support then narrowed it down to a custom hook firing on the test environment but not on live. This hook was in a custom module created by a dev agency: modules/custom/st_pgf/st_pgf.module

function st_pgf_preprocess_paragraph__50_50_hero(&$variables) { ... }
I'll leave all the code out but the gist is that it displays stories on an aggregate page the content of which it gets from SOLR search results. Initially we thought it was a caching issue or a SOLR indexing issue so you can see the rabbit holes we had to go down. If needed I'm happy to supply the full code of the preprocess hook.

This hook does have a part where it sets a view mode further down the code to be precise:
$variables['content']['field_media'][$child]['#view_mode'] = $view_mode;

I RENAMED the related paragraph template from:
paragraph--50-50-hero.html.twig

to

paragraph--50-50-hero--default.html.twig

And it now seems to be displaying as it is supposed to.

Hope this helps a bit.

jcash’s picture

I also experienced this issue today on Drupal 10.1.6 for a paragraph referenced within another paragraph, after adding a third view mode for the child paragraph. The original two view modes (which shared a TWIG template) preprocessed correctly, but the third did not (it had its own template). Renaming all the templates as described in #4 and #12 worked like a charm.