Make LayoutBuilderEntityViewDisplay::getSectionComponentForFieldName public

Created on 25 August 2022, over 2 years ago
Updated 17 April 2024, about 1 year ago

Problem/Motivation

LayoutBuilderEntityViewDisplay::getSectionComponentForFieldName is currently private, but there are cases where it would be very helpful to be able to use it outside.

In extra_field_plus, for making it layout builder compatible, we need to get the components by their field name to retrieve their (third party / additional) settings. Currently, we have to duplicate most of the code in that method to find these fields:

/**
   * Helper function to replicate LayoutBuilderEntityViewDisplay::getSectionComponentForFieldName
   *
   * Gets the component for a given field name if any.
   * LayoutBuilderEntityViewDisplay::getSectionComponentForFieldName is private,
   * so we have to replicate it here until its visibility is changed to
   * public.
   *
   * @see
   * @see https://api.drupal.org/api/drupal/core!modules!layout_builder!src!Entity!LayoutBuilderEntityViewDisplay.php/function/LayoutBuilderEntityViewDisplay%3A%3AgetSectionComponentForFieldName/9.5.x
   *
   * @param Drupal\layout_builder\LayoutBuilderEntityViewDisplay $entityViewDisplay
   * @param string $field_name
   *   The field name.
   *
   * @return \Drupal\layout_builder\SectionComponent|null
   *   The section component if it is available.
   */
  protected static function getSectionComponentForFieldName(LayoutBuilderEntityViewDisplay $entityViewDisplay, $field_name) {

    // Loop through every component until the first match is found.
    foreach ($entityViewDisplay
      ->getSections() as $section) {
      foreach ($section
        ->getComponents() as $component) {
        $plugin = $component
          ->getPlugin();
        if ($plugin instanceof DerivativeInspectionInterface && in_array($plugin
          ->getBaseId(), [
          // 'field_block', we don't need this one here.
          'extra_field_block',
        ], TRUE)) {

          // FieldBlock derivative IDs are in the format
          // [entity_type]:[bundle]:[field].
          [
            ,
            ,
            $field_block_field_name,
          ] = explode(PluginBase::DERIVATIVE_SEPARATOR, $plugin
            ->getDerivativeId());
          if ($field_block_field_name === $field_name) {
            return $component;
          }
        }
      }
    }
    return NULL;
  }

called by

static::getSectionComponentForFieldName($entityViewDisplay, $field_id);

see https://git.drupalcode.org/project/extra_field_plus/-/blob/3069861-layou...

Steps to reproduce

Proposed resolution

Change visibility to public or provide a similar helper method.

Remaining tasks

  • Discuss
  • Implement
  • Release

User interface changes

None

API changes

Data model changes

None

Release notes snippet

โœจ Feature request
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component
Layout builderย  โ†’

Last updated about 24 hours ago

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany Anybody Porta Westfalica

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

Merge Requests

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.71.5 2024