Skip to content

Paragraphs, Template Files, and Pre processing

Laryn - CEDC.org edited this page Dec 5, 2021 · 2 revisions

There are two template files provided by Paragraphs — paragraphs-item.tpl.php and paragraphs-items.tpl.php. (Notice the second is plural) To change the template of an individual bundle, you would override paragraphs-item.tpl.php.

So if your bundle is "Hero Image with Headline" (machine name: hero_image_with_headline), then your template file would be paragraphs-item--hero-image-with-headline.tpl.php.

For fields within that bundle, you would override field.tpl.php. So, field--field-hero-image.tpl.php and field--field-headline.tpl.php in my example.

Source: Mattbarnes

  • If you are having trouble finding the right filename for your template file, turn on "Theme debug" in the devel module and view the source code around where the Paragraph bundle is displayed. It should show you a variety of options for filenames.
  • Reference: default template files

Pre-processing

Paragraphs uses Entity Plus. As @argiepiano notes:

Preprocessing in Entity Plus is confusing. One would logically assume that the following preprocess hook would work: template_preprocess_paragraphs_item. But no. These preprocessing functions never get called (I suffered through this issue a few times myself!) ... [Y]ou must use hook_preprocess_entity_plus and use if statements to check that you are preprocessing the right template/entity. Eventually this may be something we could fix within Entity Plus.

For example:

/**
 * Implements hook_preprocess_HOOK().
 */
function MODULE_preprocess_entity_plus(&$variables) {
  if ($variables['entity_type'] == 'paragraphs_item') {
    // Do stuff.
  }
}

Clone this wiki locally