In this article we are going to see how to read the dynamic data of the node or entity or field values to the template file, which are specific to the paragraph template.
Generally while the Paragraph module is used, default template suggestions given by the paragraph module or the template suggestions provided by the hooks are used and further template design is done. Here’s the article which helps to write the template suggestions.
In few cases, there will be a need to read the actual values of the node fields or the entity data like for example media. And in some cases there will be unlimited values stored and need to read such data to paragraph template files.
In this article, I will share the possible ways of reading the data to the template file and few of them might not be the best approach but it's the decision to be taken for what type of project you are working with.
Let's see now the possible ways to read the data to the paragraph template file.
- To read the Url of the current node, just you can use the below code, in the
paragraph.html.twig
file or in display ofparagraph–<display>.html.twig
{{ url("<current>") }}
- To read the Title of the node, you can use the preprocess hooks like
theme_preprocess_paragraph
and read the node title and store it into a variable. And the variable data can be printed in theparagraph.html.twig
file or in the display ofparagraph–<display>.html.twig
.
function bartik_preprocess_paragraph(&$variables) {
$node = Drupal::request()->attributes->get("node");
$variables["node_title"] = $node->getTitle();
}
{{ node_title }}
- To read the field value (for example text)
paragraph.field_text.entity.name.value
- To read the Image (created via Media type or with just Image type), you can use the below different types.
{% if paragraph.field_media_image is not empty %}
{{ file_url(paragraph.field_media_image.entity.field_media_image.entity.fileuri) }}
{% endif %}
{{ file_url(paragraph.field_just_image.entity.uri.value) }}
- If you want to read the Image (created via Media type) values in a custom approach or write some preprocess, then below code will help you. You can write the below code in the theme_preprocess_paragraph or other preprocess functions, then store into a variable and Render the stored variable in the paragraph template.
$paragraph = $term->get("field_custom_paragraph")->referencedEntities();
if ($paragraph) {
$paragraph = reset($paragraph);
$field_media_image = $paragraph>get("field_media_image");
if ($field_media_image[0] && $field_media_image[0]->entity) {
$build = Drupal::entityTypeManager()->getViewBuilder("media")->view($field_media_image[0]->entity);
$variables["content"]["field_media_image"] = $build;
}
}
{{ content.field_media_image }}
These are few possible ways to render read the data to the paragraph templates.
Ideally the best approach is not to tweak the data or reading of the data at the rendering layer, because this will have some impact on the performance. I suggest to not tweak the data in the preprocess or reload the same paragraph value in the preprocess, And suggest to only use, if the case you don’t have any other possibility during the development process of drupal application.
Hope this article was helpful, read this article on how to work with paragraphs and Thanks for reading this article.