0

How to retrieve value of hidden field in twig?

PFB screenshot: How to retrieve value of hidden field in twi

I am able to retrieve value of "Partition Count" field when not disabled. But not able to retrieve it's value when disabled.

Exact requirement: 1) If "Partition Count" field is Disabled from Drupal Admin, then field should not be displayed. But this value needs to be retrieved in twig file as display of "Reference Paragraph" will vary according to the value of "Partition Count".

2) If "Partition Count" field is Enabled from Drupal Admin, then field should be displayed. In this case, I am able to retrieve "Partition Count" field value in twig file.


paragraph.html.twig:: https://github.com/WondrousLLC/paragraphs_starter_pack/blob/master/template/paragraph/paragraph.html.twig

  • You can get all fields from the entity object. If this is a node: {{ node.field_partition_count.value }} – 4k4 Mar 29 '19 at 14:06
  • Where exactly did you disable it at? Manage Form Display or Manage Display ? – No Sssweat Mar 29 '19 at 14:09
  • If you have nested paragraphs and this is a paragraph referencing paragraphs: {{ paragraph.field_partition_count.value }} – 4k4 Mar 29 '19 at 14:28
  • @4k4, your code will work only if "partition count" field is not disabled. – Ayush Mar 29 '19 at 16:30
  • 1
    @Ayush, I'm confident you can find the field value in the entity object, but without knowing which template you have and what code is in there I can't provide an answer. – 4k4 Mar 29 '19 at 16:35
1

The variables in a paragraph template contain the fully loaded paragraph entity:

paragraph.html.twig

{#
/**
 * @file
 * Default theme implementation to display a paragraph.
 *
 * Available variables:
 * - paragraph: Full paragraph entity.

from which you can retrieve raw values of any field, not matter whether base field or configured field or displayed or not:

{% set value = paragraph.field_example.value %}

On the contrary {{ content }}

 * - content: All paragraph items. Use {{ content }} to print them all,
 *   or print a subset such as {{ content.field_example }}. Use
 *   {{ content|without('field_example') }} to temporarily suppress the printing
 *   of a given child element.

contains only the fields configured to be displayed in "Manage Display".

| improve this answer | |
  • it's working.Thanks. But just wanted to know why do we need to append ".value" after field name? Also, How can I print complete "paragraph" entity? – Ayush Mar 29 '19 at 18:57
  • 1
    See this blog post from @Berdir, which is for content vs. node, but is the same for all entity templates. In this case for content vs. paragraph. – 4k4 Mar 29 '19 at 19:31

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.