Fields excluded via without in template show after installing ui_patterns

Created on 6 June 2025, about 2 months ago

Problem/Motivation

I created a card component with a similar structure like the card component in umami, where it is simple to customize the content variable depending on the needs of the template: https://git.drupalcode.org/project/drupal/-/blob/11.x/core/profiles/demo...

For some fields, I only want to check in the component if they have content. So I keep fields in the display (content variable) but do not want to display them. Inside the component I only check if they have content. So in my template I use:

  {% block content %}
    {{ content|without(
      '...'
    ) }}
  {% endblock %}

When I enable ui_patterns, the fields in the without filter suddenly show up via the content variable. There is no further configuration needed.

How can I avoid this?

Steps to reproduce

My full code:

card.twig

{% if project_id %}
  {% set project_id_rendered = project_id|render|striptags|trim %}
  {% set url = '/node/' ~ project_id_rendered ~ offer_link %}
{% endif %}

{# Preserve node vars like contextual links by adding node attributes, classes and header prefix/suffix #}
<{{html_tag|default('article')}}{{ attributes.addClass(classes, 'card') }}>
  <header>
    {{ title_prefix }}
    {{ title_suffix }}
  </header>
  {% if image and view_mode != 'full' %}
    <div class="card__logo {% if user_offer %}card__logo-user{% endif %}">
      <a href="{{ url }}">{{ image }}</a>
    </div>
  {%  endif %}
  <div class="card__title">
    {% if label %}
    <h3>
      {% if link_to_content != 'no' %}<a href="{{ url }}">{{ label }}</a>
      {% else %}
        {{ label }}
      {% endif %}
      </h3>
    {% endif %}
    {% if user_name %}
      <a href="{{ url }}">{{ user_realname }}</a>
    {% endif %}
  </div>
  <div class="card__content text-content">

    {% block content %}{% endblock %}

    {% include "h4d_olive:project-icons" with {
      size: 'size-1',
      link: 'link',
      align: 'justify-center',
      url,
      offers_commercial,
      offers_person,
      offers_invest,
      profile_housing,
      profile_coop,
      profile_provider,
    } %}

  </div>
  {% set project_crumbs_rendered = project_crumbs|render %}
  {% if project_crumbs_rendered %}
    {{ project_crumbs }}
  {% endif %}

</{{html_tag|default('article')}}>

node--h4d-project--h4d-card.html.twig

{# Preserve node classes #}
{%
  set classes = [
  'node',
  'node--type-' ~ node.bundle|clean_class,
  not node.isPublished() ? 'node--unpublished',
  view_mode ? 'node--view-mode-' ~ view_mode|clean_class,
]
%}

{% embed "h4d_olive:card" with {
  content,
  attributes: attributes,
  classes: classes,
  title_prefix: title_prefix,
  title_suffix: title_suffix,
  label: node.label,
  image: content.field_h4d_project_image,
  offers_commercial: content.h4d_offer_commercial_group_content_eva_entity_view_1,
  offers_invest: content.h4d_offer_invest_group_content_eva_entity_view_1,
  offers_person: content.h4d_offer_person_group_content_eva_entity_view_1,
  profile_housing: content.field_h4d_profile_housing,
  profile_provider: content.field_h4d_profile_provider,
  profile_coop: content.field_h4d_profile_coop,
  url: url,
  view_mode: 'h4d-card',
  type: 'project',
  nodeid: node.id,
} %}
  {% block content %}
    {{ content|without(
      'h4d_is_archived',
      'h4d_entity_moderation_form',
      'field_h4d_project_image',
      'h4d_offer_commercial_group_content_eva_entity_view_1',
      'h4d_offer_invest_group_content_eva_entity_view_1',
      'h4d_offer_person_group_content_eva_entity_view_1',
      'field_h4d_profile_housing',
      'field_h4d_profile_coop',
      'field_h4d_profile_provider',
    ) }}
  {% endblock %}
{% endembed %}

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇩🇪Germany anruether Bonn

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @anruether
  • 🇫🇷France pdureau Paris

    Hi @anruether,

    You are using loading your card component via a presenter template (node--h4d-project--h4d-card.html.twig).

    So, as far as I know, UI Patterns has a limited action on your content slot (TwigExtension::normalizeProps() calling SlotPropType::normalize()) because UI Patterns is focusing on displays built in config and processed through the Render API.

    So, I don't know why something is different after you enable ui_patterns.

    It may not be related, but I see some "risky" stuff in the snippets you shared. For example, you are feeding component props with data coming from content variable (so renderables, perfect for the slots) instead of data coming from node variable (typed data, perfect for the props):

      image: content.field_h4d_project_image,
      offers_commercial: content.h4d_offer_commercial_group_content_eva_entity_view_1,
      offers_invest: content.h4d_offer_invest_group_content_eva_entity_view_1,
      offers_person: content.h4d_offer_person_group_content_eva_entity_view_1,
      profile_housing: content.field_h4d_profile_housing,
    

    Can you run https://www.drupal.org/project/sdc_devel and see of you have some errors?

  • 🇩🇪Germany anruether Bonn

    @pdureau Thanks for looking into this. I also appreciate the feedback on the code!

    Can you run https://www.drupal.org/project/sdc_devel and check if you have some errors?

    I installed sdc_devel, but I don't experience the same behaviour. The variables excluded via |without filter do not show up.

  • 🇫🇷France pdureau Paris

    The variables excluded via |without filter do not show up.

    So you are back to the expected behaviour? Is it OK now?

    I installed sdc_devel, but I don't experience the same behaviour.

    I didn't propose sdc_devel as a fix, and this module has normally nothing to do with the behaviour of |without filter in a Twig block.

    It was to run the component validator to check if some errors were caught. You can run the validaor

    ...with a drush command:

     $ vendor/bin/drush sdcv --install my_theme
     [notice] Start validation of my_theme...
      ----------------- 
      Component          Severity   Message
     ----------------- 
    local-tasks          Warning    Deprecated Twig filter: `spaceless`. The spaceless filter is deprecated as o
    page                 Warning    Use slots instead of hard embedding a component in the template with `embed`
    form-element-label   Error      Unknown variable: `title_display`.
    form-element-label   Warning    The exact same as just testing the variable, empty is not needed. 
    dropdown-menu        Error      Array of empty object. 
    dropdown-menu        Warning    `is iterable` test is too ambiguous. Use `is sequence` or `is mapping`.               
    

    ... or from the admin UI:

  • 🇩🇪Germany anruether Bonn

    @pdureau

    So you are back to the expected behaviour? Is it OK now?

    I didn't propose sdc_devel as a fix, and this module has normally nothing to do with the behaviour of |without filter in a Twig block.

    That was a misunderstanding on my side. I assumed you asked me to deinstall ui_patterns and install sdc_devel to check if there is code in sdc_devel which triggers the error as well. And no, installing ui_patterns and sdc_devel leads to the behaviour described in the issue summary.

    I have proposed it to run the component validator to check if some errors were caught. You can run the validaor

    That makes sense ;) I'm getting a lot of errors. In the next step I'll create an isolated SDC and try to reproduce the error without the errors.

Production build 0.71.5 2024