-1

I have created a paragraph type which has field of type entity reference, link, plain text.

Now I want to fetch the values present in these through the preprocessor.paragraph.inc and display it in the custom template(twig).

Can anyone please let me know how do i do this?

I have written a hook function in the preprocessor, but unable to fetch values of paragraph types.

Eg of my paragraphs type:

  1. Section
  2. Box

In the section contains 2 fields, first is the title which is a plain text and second is the box, which is an entity reference revision

The box, is a paragraph type which has many fields like

  1. Link
  2. Image
  3. text field
  4. Wysiwig

Now how do i access values present in these through the hook function and display the same in twig template.

  • What Drupal version is this? Twig sound like D8, but preprocessor.paragraph.inc sounds like D7. – leymannx Jun 12 at 12:03
  • Drupal 8 version – Guruprasad Jun 17 at 7:30
  • And what's this preprocessor.paragraph.inc? – leymannx Jun 17 at 7:42
  • We are creating hooks in this preprocessor.paragraph.inc – Guruprasad Jun 17 at 7:47
  • And include that into your .theme or .module file? – leymannx Jun 17 at 8:08
0

Every paragraph can be preprocessed with template_preprocess_paragraph or even more specific with template_preprocess_paragraph__PARAGRAPH_TYPE.

Check this sample below, where I get the value of field_foobar of the paragraph type box and pass it to the Twig template. It's a little bit redundant as this value can be accessed from the template itself, but you wanted to know how to pass values from a preprocess function to Twig. Here's how:

function MYTHEME_preprocess_paragraph(&$variables) {

  $paragraph = $variables['paragraph'];

  if ($paragraph->getType() === 'box') {
    if (!$paragraph->field_foobar->isEmpty()) {

      $variables['foobar'] = $paragraph->get('field_foobar')->first()->getValue();
    }
  }
}

And then in paragaph.html.twig:

{% if foobar %}
  <div class="foobar">{{ foobar }}</div>
{% endif %}

Same principle applies for other preprocess functions and templates. On node level, on page level, even in fields or views. See How to load paragraph entity on node object? It's just that you need to get hold of the entity ID your paragraph field is referencing, load the paragraph from that ID and then you get every field you want. For example inside template_preprocess_html:

function MYTHEME_preprocess_html(&$variables) {

  $node = \Drupal::routeMatch()->getParameter('node');

  if ($node instanceof \Drupal\node\NodeInterface) {
    if ($node->bundle() === 'article') {

      $my_paragraphs = $node->get('field_paragraphs')->getValue();

      foreach ($my_paragraphs as $item) {

        $paragraph = \Drupal\paragraphs\Entity\Paragraph::load($item['target_id']);

        if ($paragraph->getType() === 'box') {
          if (!$paragraph->field_foobar->isEmpty()) {

            $variables['foobar'] = $paragraph->get('field_foobar')->first()->getValue();
          }
        }
      }
    }
  }
}

Now you can access {{ foobar }} in html.html.twig.


You may want to install Devel's submodule Kint. Then you can dump all variables in templates with {{ kint() }} or in PHP with ksm($variables). Then check the dump for what values or methods you get and then use them in your code.

  • I am trying to replicate the way one of the hook is working, – Guruprasad Jun 19 at 9:43
  • I see that $section_column['entity_info']['bundle']['#markup']; this actually shows up the paragraph which is linked to the parent paragraph. In my case i do not have 'entity_info' in my json structure. – Guruprasad Jun 19 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.