1

I'm using the Paragraphs module to create components, i then make a content type and add the Paragraph as a reference field.

The form of the Paragraph has a field (a list) where a content creator can select a color name for a background color for the div. Each Paragraph has 3 values (pacific_blue, smokey_white, white).

However, i'm trying to get the selected value without displaying it on the site.

As long as the field is not disabled i can get the value using this code and add the class to style the div with the right background color:

<div class="c14a {{ content.group_wrap.field_color_backgroud.0["#markup"] }}">
  <div{{ attributes.addClass(classes) }}>
      {{ content }}
  </div>
</div>

So how do i get the value of a field that is disabled in 'manage display'?

Thx...

1
  • I update my answer with more details.
    – Yuseferi
    Mar 17, 2017 at 9:51

3 Answers 3

0

Try to add it on you hook_preproccess_node to variables.

something like following snippet could help you.

on your THEMENAME.theme

function THEMENAME_preprocess_node(&$vars) {
    $node = $vars['node'];
    if(isset($vars['node']) && $vars['node']->getType()=='YOUR_CONTENT_TYPE_MACHINE_NAME') {
        $values = $node->field_color_backgroud->entity->getValue();
        $vars['myvar'] = $values[0];
    }

}

Then on your TWIG file {{ myvar }} will be available.

5
  • Try to get it via node I doubt this will work since paragraphs is its own/separate entity (not part of node). So content.field_color_backgroud.0.getvalue or paragraphs_item.field_color_backgroud.0.getvalue
    – No Sssweat
    Mar 17, 2017 at 8:41
  • @NoSssweat yep, you right, I don't consider he ask about paragraph type . Thanks for your note ,I'm going to update my answer
    – Yuseferi
    Mar 17, 2017 at 8:46
  • What's the 'requested_course' string?
    – kevinius
    Mar 17, 2017 at 11:33
  • @kevinius Oops, it was my content type machine name :D . I update it to YOUR_CONTENT_TYPE_MACHINE_NAME
    – Yuseferi
    Mar 17, 2017 at 11:46
  • @kevinius adding type filtering prevent error on other content types, because this fields maybe not available on other content types.
    – Yuseferi
    Mar 17, 2017 at 11:54
0

Ok, i going to answer my own question because i think it's the most elegant option for my current situation.

The solution is to 'not' disable any field. Then in your template you just output the fields that you want. That way, you do have the value but you are not outputting it to the visitor.

<div class="c14a {{ content.field_background.0["#markup"] }}">
  <div{{ attributes.addClass(classes) }}>

    {{ content.field_title }}

  </div>
</div>
0

Expanding on your self-answered question, if you're using Display Suite and Paragraphs together and want to keep your formatting and any special token fields you've created, the without twig filter works great for excluding any fields you don't want to show. Here's my custom template that uses a couple of fields as CSS classes, then hides them from display:

<{{ ds_content_wrapper }}{{ attributes.addClass('ds-1col', 'clearfix') }}>
  {{ title_suffix.contextual_links }}
  {% set bgcolor = content.field_background_color.0['#markup'] %} 
  {% set angles = content.field_skew.0['#markup'] %} 
  <div class="wrapper {{ bgcolor | lower }} {{ angles | lower | replace({' ':'_'}) }}">
     {{ ds_content | without('field_background_color', 'field_skew') }}
  </div>   
</{{ ds_content_wrapper }}>

Your Answer

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

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