backdrop-contrib / paragraphs

Paragraphs module to control your content flow
https://backdropcms.org/project/paragraphs
GNU General Public License v2.0
5 stars 11 forks source link

Can theme preprocess hooks work for paragraphs_item types/bundles? #194

Closed yorkshire-pudding closed 1 month ago

yorkshire-pudding commented 1 month ago

While I see that THEMENAME_preprocess_paragraphs_item() is recognised and invoked, I thought, that THEMENAME_preprocess_paragraphs_item__BUNDLEMACHINENAME() might also work assuming that a template existed in the theme called paragraphs-item--BUNDLEMACHINENAME.tpl.php but I can't seem to get it to work. Am I doing something wrong or is this not possible?

argiepiano commented 1 month ago

You can see in template_preprocess_paragraphs_items() that the theme suggestion for BUNDLENAME is not provided, thus the template won't be used. You can create a preprocess function in your theme to add the bundlename option.

EDIT: you are talking about paragraphs_item or paragraphs_items?

argiepiano commented 1 month ago

Notice in your message you wrote paragraphs-item--BUNDLEMACHINENAME.tpl.php (plural on paragraphs, which won't work)

Just ignore me haha. I'm not sure what I'm doing today

yorkshire-pudding commented 1 month ago

You can see in template_preprocess_paragraphs_items() that the theme suggestion for BUNDLENAME is not provided, thus the template won't be used. You can create a preprocess function in your theme to add the bundlename option.

EDIT: you are talking about paragraphs_item or paragraphs_items?

Yes, I am using paragraphs_item. The template is being picked up, but I can't see similarly named preprocess functions working image

I can work around it if is not intended to work, but just wanted to see if it is possible.

argiepiano commented 1 month ago

I think there may be a syntax error in the suggestions for paragraphs-item. I see these lines in template_preprocess_paragraphs_item

// Add suggestions.
  $variables['theme_hook_suggestions'][] = $paragraphs_item_type;
  $variables['theme_hook_suggestions'][] = $paragraphs_item_type . '__' . $bundle;
  $variables['theme_hook_suggestions'][] = $paragraphs_item_type . '__' . $bundle . '__' . $variables['view_mode'];
  $variables['theme_hook_suggestions'][] = $paragraphs_item_type . '__' . $paragraphs_item_id;

This is missing the name of the hook. I believe the correct suggestions should be:

// Add suggestions.
  $variables['theme_hook_suggestions'][] = 'paragraphs_item__' . $paragraphs_item_type;
  $variables['theme_hook_suggestions'][] = 'paragraphs_item__' . $paragraphs_item_type . '__' . $bundle;
  $variables['theme_hook_suggestions'][] = 'paragraphs_item__' . $paragraphs_item_type . '__' . $bundle . '__' . $variables['view_mode'];
  $variables['theme_hook_suggestions'][] = 'paragraphs_item__' . $paragraphs_item_type . '__' . $paragraphs_item_id;

Try that and see what happens.

yorkshire-pudding commented 1 month ago

I'm a bit confused. THEMENAME_preprocess_paragraphs_item__paragraphs_item__header() doesn't seem right. I did dpm to see what those variables were.

argiepiano commented 1 month ago

Huh? My suggestion was to modify those lines I pasted above in template_preprocess_paragraphs_item. Those suggestions seem wrong as they are - I think that's a bug.

argiepiano commented 1 month ago

OK, I think I probably misread your OP. So, your problem is not that your template suggestion is not working, but rather, that your preprocess function is not being called? AFAIK the preprocess functions do not use the pattern THEMENAME_preprocess_paragraphs_item__BUNDLEMACHINENAME. They only work as THEMENAME_preprocess_paragraphs_item and you have to do a conditional to check the bundle name inside the implementation.

yorkshire-pudding commented 1 month ago

Thanks @argiepiano - I'll do that. I wanted to check I wasn't missing something, but I can work with that approach.

laryn commented 1 month ago

@yorkshire-pudding I haven't delved into that in terms of whether it's possible (I believe you that it's not working currently), but I'm not opposed to THEMENAME_preprocess_paragraphs_item__BUNDLEMACHINENAME functionality if you figure out what's needed and it's not intrusive to add/fix.