在树枝中访问段落的子字段

时间:2017-07-17 14:20:52

标签: drupal twig drupal-8

如何访问段落字段的子字段。

对于我的旋转木马,我制作了一个英雄形象旋转木马"段落,然后对于田地,我做了另一个分段"英雄形象旋转木马领域"我分配到第一个。这让我有了多张幻灯片,它在后端完美运行。

现在我想在树枝模板中设置样式。

我可以直接访问第一个,第二个等子字段,如下所示: content.field_hero_image_c_fields.0['#paragraph'].field_hero_image_c_content 但我需要在循环中拥有它。现在的问题是,当我做类似的事情时:

{% for item in content.field_hero_image_c_fields %}
    {{ item }}
{% endfor %}

它确实获取了我的字段,还有其他默认字段,如uuid,field_name等。

这里是内容转储:



array (size=2)
  'field_hero_image_c_fields' => 
    array (size=18)
      '#theme' => string 'field' (length=5)
      '#title' => string 'Hero Image Karussell Felder' (length=27)
      '#label_display' => string 'hidden' (length=6)
      '#view_mode' => string 'default' (length=7)
      '#language' => string 'de' (length=2)
      '#field_name' => string 'field_hero_image_c_fields' (length=25)
      '#field_type' => string 'entity_reference_revisions' (length=26)
      '#field_translatable' => boolean false
      '#entity_type' => string 'paragraph' (length=9)
      '#bundle' => string 'hero_image_carousel' (length=19)
      '#object' => 
        object(Drupal\paragraphs\Entity\Paragraph)[1484]
          protected 'unserializedBehaviorSettings' => null
          protected 'values' => 
            array (size=21)
              ...
          protected 'fields' => 
            array (size=6)
              ...
          protected 'fieldDefinitions' => 
            array (size=19)
              ...
          protected 'languages' => 
            array (size=4)
              ...
          protected 'langcodeKey' => string 'langcode' (length=8)
          protected 'defaultLangcodeKey' => string 'default_langcode' (length=16)
          protected 'activeLangcode' => string 'x-default' (length=9)
          protected 'defaultLangcode' => string 'de' (length=2)
          protected 'translations' => 
            array (size=1)
              ...
          protected 'translationInitialize' => boolean false
          protected 'newRevision' => boolean false
          protected 'isDefaultRevision' => string '1' (length=1)
          protected 'entityKeys' => 
            array (size=4)
              ...
          protected 'translatableEntityKeys' => 
            array (size=2)
              ...
          protected 'validated' => boolean false
          protected 'validationRequired' => boolean false
          protected 'loadedRevisionId' => string '247' (length=3)
          protected 'entityTypeId' => string 'paragraph' (length=9)
          protected 'enforceIsNew' => null
          protected 'typedData' => 
            object(Drupal\Core\Entity\Plugin\DataType\EntityAdapter)[1554]
              ...
          protected 'cacheContexts' => 
            array (size=0)
              ...
          protected 'cacheTags' => 
            array (size=0)
              ...
          protected 'cacheMaxAge' => int -1
          protected '_serviceIds' => 
            array (size=0)
              ...
          protected 'needsSave' => boolean false
      '#items' => 
        object(Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList)[870]
          protected 'list' => 
            array (size=2)
              ...
          protected 'langcode' => string 'de' (length=2)
          protected 'definition' => 
            object(Drupal\field\Entity\FieldConfig)[1544]
              ...
          protected 'name' => string 'field_hero_image_c_fields' (length=25)
          protected 'parent' => 
            object(Drupal\Core\Entity\Plugin\DataType\EntityAdapter)[1554]
              ...
          protected 'stringTranslation' => null
          protected 'typedDataManager' => 
            object(Drupal\Core\TypedData\TypedDataManager)[765]
              ...
      '#formatter' => string 'entity_reference_revisions_entity_view' (length=38)
      '#is_multiple' => boolean true
      0 => 
        array (size=6)
          '#paragraph' => 
            object(Drupal\paragraphs\Entity\Paragraph)[2440]
              ...
          '#view_mode' => string 'default' (length=7)
          '#cache' => 
            array (size=3)
              ...
          '#theme' => string 'paragraph' (length=9)
          '#weight' => int 0
          '#pre_render' => 
            array (size=1)
              ...
      1 => 
        array (size=6)
          '#paragraph' => 
            object(Drupal\paragraphs\Entity\Paragraph)[2264]
              ...
          '#view_mode' => string 'default' (length=7)
          '#cache' => 
            array (size=3)
              ...
          '#theme' => string 'paragraph' (length=9)
          '#weight' => int 0
          '#pre_render' => 
            array (size=1)
              ...
      '#cache' => 
        array (size=3)
          'contexts' => 
            array (size=0)
              ...
          'tags' => 
            array (size=0)
              ...
          'max-age' => int -1
      '#weight' => int 1
  'field_hero_image_c_scroll_link' => 
    array (size=17)
      '#theme' => string 'field' (length=5)
      '#title' => string 'Scroll Link' (length=11)
      '#label_display' => string 'hidden' (length=6)
      '#view_mode' => string 'default' (length=7)
      '#language' => string 'de' (length=2)
      '#field_name' => string 'field_hero_image_c_scroll_link' (length=30)
      '#field_type' => string 'link' (length=4)
      '#field_translatable' => boolean false
      '#entity_type' => string 'paragraph' (length=9)
      '#bundle' => string 'hero_image_carousel' (length=19)
      '#object' => 
        object(Drupal\paragraphs\Entity\Paragraph)[1484]
          protected 'unserializedBehaviorSettings' => null
          protected 'values' => 
            array (size=21)
              ...
          protected 'fields' => 
            array (size=6)
              ...
          protected 'fieldDefinitions' => 
            array (size=19)
              ...
          protected 'languages' => 
            array (size=4)
              ...
          protected 'langcodeKey' => string 'langcode' (length=8)
          protected 'defaultLangcodeKey' => string 'default_langcode' (length=16)
          protected 'activeLangcode' => string 'x-default' (length=9)
          protected 'defaultLangcode' => string 'de' (length=2)
          protected 'translations' => 
            array (size=1)
              ...
          protected 'translationInitialize' => boolean false
          protected 'newRevision' => boolean false
          protected 'isDefaultRevision' => string '1' (length=1)
          protected 'entityKeys' => 
            array (size=4)
              ...
          protected 'translatableEntityKeys' => 
            array (size=2)
              ...
          protected 'validated' => boolean false
          protected 'validationRequired' => boolean false
          protected 'loadedRevisionId' => string '247' (length=3)
          protected 'entityTypeId' => string 'paragraph' (length=9)
          protected 'enforceIsNew' => null
          protected 'typedData' => 
            object(Drupal\Core\Entity\Plugin\DataType\EntityAdapter)[1554]
              ...
          protected 'cacheContexts' => 
            array (size=0)
              ...
          protected 'cacheTags' => 
            array (size=0)
              ...
          protected 'cacheMaxAge' => int -1
          protected '_serviceIds' => 
            array (size=0)
              ...
          protected 'needsSave' => boolean false
      '#items' => 
        object(Drupal\Core\Field\FieldItemList)[2265]
          protected 'list' => 
            array (size=1)
              ...
          protected 'langcode' => string 'de' (length=2)
          protected 'definition' => 
            object(Drupal\field\Entity\FieldConfig)[1546]
              ...
          protected 'name' => string 'field_hero_image_c_scroll_link' (length=30)
          protected 'parent' => 
            object(Drupal\Core\Entity\Plugin\DataType\EntityAdapter)[1554]
              ...
          protected 'stringTranslation' => null
          protected 'typedDataManager' => 
            object(Drupal\Core\TypedData\TypedDataManager)[765]
              ...
      '#formatter' => string 'link' (length=4)
      '#is_multiple' => boolean false
      0 => 
        array (size=4)
          '#type' => string 'link' (length=4)
          '#title' => string 'Die entdecken!' (length=27)
          '#options' => 
            array (size=0)
              ...
          '#url' => 
            object(Drupal\Core\Url)[2421]
              ...
      '#cache' => 
        array (size=3)
          'contexts' => 
            array (size=0)
              ...
          'tags' => 
            array (size=0)
              ...
          'max-age' => int -1
      '#weight' => int 2




这里是

的转储

我如何只为我需要的字段访问和制作循环?

感谢您的任何建议!

1 个答案:

答案 0 :(得分:0)

这种方法对我有用

{% for item in content['#node'].field_myparagraphfield_items %}
....
<img class="carousel-image-fullsize" src="{{ file_url( item.entity.field_picture_item_image_main[0].entity.uri.value | image_style("my_carousel_image_style") )}}">
....
{% endfor %}

段落中的字段将在for循环中找到item.entity.field_picture_item_image_main。不是最容易理解的事情!