SDC: Breadcrumb handling consistency

Created on 17 April 2024, 7 months ago
Updated 18 June 2024, 5 months ago

Problem/Motivation

Breadcrumbs text may be a string or a TranslatableMarkup object, depending on its source. Routes provide TranslatableMarkup by default but nodes provides strings.

Schema need to reflect that or the theme function signature needs to change,

Steps to reproduce

  • Define a breadcrumb component with type: string for the text value.
  • Install a module that generate route-based breacrumbs
  • Visit such a route
  • Fatal error

Proposed resolution

We have several options here. Note the following:

Option 1: schema arrays

This schema will fix the problem but seems inadequate:

          text:
            title: Text
            type:
              - string
              - object

Option 2: schema arrays with class registry

This actually fails, since TranslatableMarkup isn't registered with the validator the way that Attributes are:

          text:
            title: Text
            type:
              - string
              - Drupal\Core\StringTranslation\TranslatableMarkup

Throws:

Drupal\Core\Render\Component\Exception\InvalidComponentException: [props.properties.items.items.properties.text.type] Does not have a value in the enumeration ["array","boolean","integer","null","number","object","string"]/n[props.properties.items.items.properties.text.type[1]] Does not have a value in the enumeration ["array","boolean","integer","null","number","object","string"]/n[props.properties.items.items.properties.text.type] Failed to match at least one schema/n[props.properties.items.items] Object value found, but an array is required/n[props.properties.items.items] Failed to match at least one schema in Drupal\Core\Theme\Component\ComponentValidator->validateDefinition() (line 121 of core/lib/Drupal/Core/Theme/Component/ComponentValidator.php). 

So perhaps we should register TranslatableMarkup as a valid class here.

Option 3: allow validation of markup as a string

We currently allow render arrays to be passed with special validation. Perhaps items designated as strings should also pass validation if they send TranslatableMarkup to the component.

Reference: ComponentValidator::validateProps

    // Dismiss type errors if the prop received a render array.
    $errors = array_filter(
      $validator->getErrors(),
      function (array $error) use ($context): bool {
        if (($error['constraint'] ?? '') !== 'type') {
          return TRUE;
        }
        return !Element::isRenderArray($context[$error['property']] ?? NULL);
      }
    );

Option 4: enforce strict typing throughout the system of breadcrumb generation

Perhaps the breadcrum function should pre-translate and always pass a string here.

Remaining tasks

Decide on the solution

User interface changes

None

API changes

Uncertain

Data model changes

None?

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
single-directory componentsΒ  β†’

Last updated 3 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

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

Comments & Activities

Production build 0.71.5 2024