Provide an option to remove item content wrappers for Rendered entity field formatter.

Created on 15 November 2022, over 1 year ago
Updated 28 January 2023, over 1 year ago

Problem/Motivation

When rendering referenced entities (node, taxonomy_term, ...) it is impossible to override the default Drupal core themes (node.html.twig, taxonomy-term.html.twig, ...), meaning, although field wrappers can be controlled from the module, it does not provide sufficient markup control when rendering entities (that by default use various wrappers).

Steps to reproduce

Add an entity reference field to Manage Display and set the field Format to the Rendered entity. Rendered entities will use the wrapper from the default core themes (node.html.twig, taxonomy-term.html.twig, ...).

Proposed resolution

Provide an option to remove item content wrappers when rendering referenced entities, i.e., replace entity reference field item content #theme with custom theme_hook() renderer.

  // Entity reference field type settings
  if ($field_definition->getSetting('target_type')) {
    $settings['fences']['entity_reference_fields'] = [
      '#type' => 'details',
      '#title' => t('Field Item Options for Entity Reference fields'),
      '#open' => TRUE
    ];
    $settings['fences']['entity_reference_fields']['fences_field_item_unset_content_theme'] = [
        '#type' => 'checkbox',
        '#title' => t('No wrapping HTML for referenced entity content'),
        '#description' => t('Unset field item(s) referenced entity content Twig theme to render referenced entity without Twig theme wrapping HTML.'),
        '#default_value' => $plugin->getThirdPartySettings('fences')['entity_reference_fields']['fences_field_item_unset_content_theme'] ?? FALSE,
    ];
  }
/**
 * Implements hook_theme().
 */
function fences_theme() {
  return [
    'fencescontent' => [
      'render element' => 'content',
    ],
  ];
}
    // Remove entity reference field item content #theme
    if ($fences_config['entity_reference_fields']['fences_field_item_unset_content_theme'] ?? FALSE) {
      // The item render array keys (deltas) are always 
      // numerically indexed starting from 0
      // therfore looping on those index keys to unset #theme
      $delta = 0;
      while (!empty($vars['items'][$delta])) {
        if (isset($vars['items'][$delta]['content']['#theme'])) {
          $vars['items'][$delta]['content']['#theme'] = 'fencescontent';
        }
        $delta++;
      }
    }
{#
/**
 * @file
 * Fences's module implementation to entity content.
 *
 * @see template_preprocess_node()
 */
#}
{{ content }}

Remaining tasks

Review the patch.

User interface changes

Add No wrapping HTML for entity content checkbox for "Field Item Options for Entity Reference fields".

Data model changes

Update configuration schema.

✨ Feature request
Status

Needs review

Version

3.0

Component

Code (fences)

Created by

πŸ‡ΈπŸ‡¦Saudi Arabia martins.bruvelis Thuwal

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024