1

New to Drupal and trying to output the value of a textfield called mytextfield from my Basic Page content type edit page into the webpage sidebar. I have the sidebar configured with a region set in Block Layouts and that displays fine. Now I want to be able to type text e.g foobar, into the 'mytextfield` field hit Save and have the field value foobar appear in the sidebar.

From Googing it appears that I have to edit block.html.twig from the templates folder. block.html.twig (I will use the Twig Debugger to edit a more appropriate file at a later stage)

{%
  set classes = [
    'block',
    'block-' ~ configuration.provider|clean_class,
    'block-' ~ plugin_id|clean_class,
  ]
%}
<div{{ attributes.addClass(classes) }}>
  {{ title_prefix }}
  {% if label %}
    <h2{{ title_attributes }}>{{ label }}</h2

  {% endif %}
  {{ title_suffix }}
  {% block content %}
    <div{{ content_attributes.addClass('content') }}>
      {{ content }}
    </div>
  {% endblock %}
</div>

I added:

Output 1:  {{ content.mytextfield[0] }}
Output 2:  {{ content.mytextfield }}

as below hoping to output the value foobar but alas no joy.

{%
  set classes = [
    'block',
    'block-' ~ configuration.provider|clean_class,
    'block-' ~ plugin_id|clean_class,
  ]
%}
<div{{ attributes.addClass(classes) }}>
Output 1:  {{ content.mytextfield[0] }}
Output 2:  {{ content.mytextfield }}
  {{ title_prefix }}
  {% if label %}
    <h2{{ title_attributes }}>{{ label }}</h2

  {% endif %}
  {{ title_suffix }}
  {% block content %}
    <div{{ content_attributes.addClass('content') }}>
      {{ content }}
    </div>
  {% endblock %}
</div>

...am I on the right track here or is there a visibility setting failed to set someplace? Just starting out with Drupal and couldn't find how to do it. Couldn't see the answer in my Drupal 8 Theming with Twig book either. Basically I want the values I add to fields in the edit page to appear in the sidebar on the webpage as easy as possible. Using the Barrio theme. Thanks!!

User301276
  • 55
  • 1
  • 8
  • If this doesn't work it should be possible to create pre-process hook inside your module (or theme maybe) which will collect the field value and create twig variable. Sorry, I didn't work with twig for years, don't have any example. – MilanG Aug 02 '23 at 14:48

0 Answers0