Well it might be a simple solution but I don't get it.

I have a field in my content type "field_color". I can get the value of that field in my page.html.twig with {{ node.field_color.value }}

It works really good.

With Display Suite I created several layouts, but how to I access the field value within the layout.

I already tried:

{{ node.field_color.value }}
{{ ds_content.field_color.value }}
{{ ds_content.field_ }}
{{ ds_content.0 }}
{{ ds_content.1 }}
{{ ds_content.3 }}

nothing works.

Drupal 8, DS 2.5 stables

In my layouts.yml

  label: 'Text 1col Collapse'
  category: 'my cat'
  class: '\Drupal\ds\Plugin\DsLayout'
  template: layouts/sw_default_text/sw-default-text
      label: Content

My sw-default-text.html.twig:

 * @file
 * Template for a 1 column layout.
<div {{ attributes.setAttribute('id', settings.css_id).addClass('row shrink', 'sw-default-text', settings.css_classes) }}>
  <div class="small-12 columns">
    {{ content.middle }}

My suspicion is you are having an issue because you are trying to access this in a Display Suite template, rather than from a page template.

You can probably access it via {{ ds_region.field_color.0 }} in the ds-3col.html.twig file. Some notes:

  1. Replace ds_region with the name of the ds_region you want - e.g. middle.
  2. The 0 will mean it prints the first item in the array - if you only have one item, this should be enough
  3. You will need to have the field in the region on your Manage Display page.
  4. Once done, you can then print {{ left|without('body') }}.
  5. Make sure to clear your cache

I hope this helps.

You could also try to {{ dump }} the variables and see what is available for you (or better still {{ kint }} if you have the devel module installed), that will help you drill down through the options to get the one you need.

| improve this answer | |
  • Well thanks it show that the field is available under node {content} doesn't work within in the page.twig It shows also that the field is available under "protected 'fieldDefinitions'" whatever that means. – Daniel Aug 15 '16 at 7:40
  • {{ content }} is available as a node variable in the node template. {{ node }} is available as a page variable in the page template. See the list of available variables at the comments at the top of page.html.twig and node.html.twig. Are you trying to access these via the DS UI or via a DS template? – Mark Conroy Aug 15 '16 at 9:28
  • Via DS template – Daniel Aug 15 '16 at 9:37
  • What is the name of the template and can you post the relevant code from it for us? Also, what type of field are you trying to get the value of? List? Term reference? – Mark Conroy Aug 15 '16 at 12:22
  • A normal plain textfield. There is not much to see because it’s a DS layout. The child theme parent is Zurb Foundation for Drupal. I added both things in my first post @Pierre.Vriens – Daniel Aug 15 '16 at 15:06

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.