0

I need to reference field_role from a paragraph inside of a field--node template. The paragraph is an unlimited repeating field in the content type. The field template I am working with is the field in the content type which references the paragraph entity.

In the field template:

{% for item in items %}
  <li>
    <a href="#">{{ paragraph.field_role }}</a>
  </li>
{% endfor %}

I've tried {{ content.field_role }} and {{ item.field_role }} as well.

Separately, I've also tried creating a preprocess hook for the field.

function theme_preprocess_field(&$variables, $hook) {
  $paragraph = $variables['paragraph']->field_role->value;
  $variables['var_role'] = $paragraph;
}
function theme_preprocess_entity(&$variables) {
  if ($variables['entity_type'] == 'paragraphs_item') {
    if(isset($variables['elements']['field_role']['#items'][0]['value'])){
      $variables['var_role'] = $variables['elements']['field_role']['#items'][0]['value'];
    }
  }
}

(I know this would only pull the first item from the repeating field, but I'm just trying to get anything to render at this point.)

How can I achieve what I am trying to do?

2

I was able to solve this by using the following in the field template:

{% for item in items %}
  {{ item.content['#paragraph'].field_role.0.value|striptags }}
{% endfor %}
1

It's because you're operating a field, but not a paragraph entity. In the preprocess hook you can get specific paragraph field view, like this:

$paragraphField = $variables['paragraph']->get('field_role');
$variables['content']['field_role'] = $paragraphField->view();

Then render in field template:

{{ content.field_role }}
  • 3
    This may help solve the problem, but it doesn't explain why/how. Please elaborate. – Shawn Conn May 20 '16 at 16:13
  • To Conn and Adrian: Guys you can use private messages to inform me about something that doesn't relative to the question (unless you wanted public lecturing). – xcono May 22 '16 at 4:41
  • There aren't private messages, here on Stack Exchange, with the exception of the messages sent from moderators, but those are for more serious things than telling you your answer is wrong. Comments here have been used for their exact purpose. – kiamlaluno May 22 '16 at 5:35

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.