4

In Drupal 8, I have a content type which use Field Collection Module like that :

  • field_body (FieldCollection, repeatable)
    • field_image_alignment(Boolean)
    • field_picture (Image)
    • field_free_text (Long Text)

I want to theme this fields. I overrided the field template with a file named field--node--field-body--news.html (news is the content type that contains theses fields.).

Default template is this :

{% for item in items %}
    <div{{ item.attributes }}>{{ item.content }}</div>
{% endfor %}

And my template overrided is like that :

{% for item in items %}
  {% if item.content['#field_collection_item'].field_image_alignment.value == 1 %}
    {{ dump(item.content['#field_collection_item'].field_picture) }}
    {% endif %}
{% endfor %}

How can I render "child" fields field_pictures, field_free_text ? I used item.content['#field_collection_item'].field_image_alignment.valueto access to the plain value but I would like to have the rendered view.

Thanks a lot.

2 Answers 2

2

I've just started learning Drupal and encountered the same issue. I solved it by creating custom field formatter:

public function viewElements(FieldItemListInterface $items, $langcode)
{
  $elements = array();
  foreach ($items as $delta => $item)
    if ($item->value !== NULL)
    {
      $builder    = \Drupal::entityTypeManager()->getViewBuilder('field_collection_item');
      $elements[] = $builder->build($builder->view($item->getFieldCollectionItem()));
    }
  return $elements;
}
-1

Use this module for Nice Table format Look Link: https://www.drupal.org/project/reference_table_formatter

1
  • Welcome to Drupal Answers! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. I also don't see how this module helps at all with this situation?
    – mpdonadio
    Commented Oct 2, 2016 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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