Problem/Motivation

I would like to create a Paragraph instance with two features using theme preprocessing - and since the D8 documentation is scattered on the issue it would help to pull it together. Here on this thread, we can pull together some disparate parts which would end up on the documentation page.

I would like to get the image file source URL out of an image field inside a paragraph, turn that into a twig variable using THEME_preprocess_paragraph. Additionally another select list field would let me add a class to help determine the positioning of a background image. And the element with the class would have twig markup similar to, for example:

<div class="{{backgroundposition}}" style="background-image: url({{imagelocation}})"></div>

So using THEME_preprocess_paragraph (or another hook, like the paragraph's field hook if needed) how can I collect the value of a select list field from the paragraph, and the URL from the image field (assuming it is [0] and not a repeater image field)?

Additional questions are in which templates are various paragraph twig variables available? How best to explore and debug them?

Relevant theming hook in paragraphs.module:

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function paragraphs_theme_suggestions_paragraph(array $variables) {
  $suggestions = array();
  $paragraph = $variables['elements']['#paragraph'];
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');

  $suggestions[] = 'paragraph__' . $sanitized_view_mode;
  $suggestions[] = 'paragraph__' . $paragraph->bundle();
  $suggestions[] = 'paragraph__' . $paragraph->bundle() . '__' . $sanitized_view_mode;

  return $suggestions;
}

Remaining tasks

See also + related bug threads:

Here is a useful snippet: ( via #2769543: Issues with preprocess function)

function base_preprocess_paragraph(&$variables) {
	$paragraph = $variables['paragraph'];
	$variables['heading'] = $paragraph->field_heading->get(0)->value;
}

For debugging,

function THEME_preprocess_paragraph__text_large_image(&$variables) {
  $paragraph = $variables['paragraph'];
dpm($paragraph);

I will share working snippets here as I move ahead on this, with an eye to writing a better documentation page from it all.

Comments

HongPong created an issue. See original summary.

miro_dietiker’s picture

How about the new documentation pages? Can they be used for contrib too?

Thx for helping to push documentation forward in Paragraphs!

HongPong’s picture

Thanks for your support miro. I don't quite follow your question. The idea would be to put the documentation here: https://www.drupal.org/node/2444893

Also I would be thrilled to just find out how to get this URL string for the field's image out of the preprocessor function. I've been at it for days :(

miro_dietiker’s picture

If you look at the documentation for core, you will see that there is a new documentation system in place:
https://www.drupal.org/documentation

I don't know if this is available for contrib too.

Will ping someone from team to look into your field question.

miro_dietiker’s picture

Ah i found this: https://www.drupal.org/docs/8/modules and this

https://www.drupal.org/node/2762837 - so they're migrating the issue. Just create an own page and we are good.

So the new guide will be flat. I'll need to think about how to structure it in the future.

HongPong’s picture

If anyone else is looking for background image CSS rendering from image field sources, last night I was pleased to find that the latest commits with https://www.drupal.org/project/bg_image_formatter work correctly, you just have to hit the gear and set the selector to attach the image to your preferred div. Works great with paragraphs, no hassle really.

miro_dietiker’s picture

Connecting a related issue that didn't make it in 7.x

anruether’s picture

I'm glad this issue exists. I would like to add another use case and will happily post my solutions if I come up with any:

Amongst others I have two fields in a paragraph:
1. heading
2. heading_level

heading is a plain text field. heading_level is a list consisting of h1,h2,h3,.. Now I would like to change the markup of my heading from <div> to <heading_level> to let the editor adjust the heading appropriately.

miro_dietiker’s picture

@anruether There are tools like Fences that allow to choose the wrapper while site building.. If the HTML wrapper needs to be chosen in content, that's a pretty custom requirement we can't fulfill with this issue.
But it's super easy to do this on your own. You can simply implement a twig template and express exactly what you did above.
The idea is great for Paragraphs Collection. It's likely we will create many more plugins in Paragraphs Collection to fulfill advanced theming and HTML semantics requirements... I created an issue for a plugin: #2844741: Create a plugin to choose the HTML heading level / semantics

So to make the issue clearer, we are seeking healthy default classes and markup that makes sense to ship by default and allow easy theming without the need to overwrite templates.