[2.0.0-alpha1] Add PropTypeInterface::normalize()

Created on 29 February 2024, 9 months ago
Updated 19 April 2024, 7 months ago

Problem/Motivation

Today, each source plugin is responsible of the validity of the data returned by SourceInterface::getData(): mixed

For better safety, we need to add an extra check managed by the prop type themselves.

Proposed resolution

Add PropTypeInterface::normalize(mixed $value): mixed method and implement it in every prop type plugins.

Example for strings:

function normalize(mixed $value): mixed {
  return (string) $value;
}

This new method may be the opportunity of merging 2 over-specific mechanisms.

ComponentElementAlter::cleanSlotsValues(array $element): array

This can be moved to the new normalize() method of Slot prop type.

However, even if it will look a bit empty after the removal of cleanSlotsValues we want to keep ComponentElementAlter, because :

  • we want PropTypeInterface::normalize() to be called by ComponentElementAlter and not by ComponentElementBuilder, so every component will be normalized, even when they are not build by the ComponentElementBuilder
  • we will add other mechanisms to ComponentElementAlter later

LinksPropType::normalize(array $items): array

This method can be updated to use the new method instead of being public without being described by any interface

This method is currenly static, does that means PropTypeInterface::normalize() will be static too?

This method is currently explicitly called from preprocesses, example:

$before = LinksPropType::normalize(\array_filter([
  $variables['items']['first'] ?? [],
  $variables['items']['previous'] ?? [],
));

But we hope than calling PropTypeInterface::normalize() from ComponentElementAlter will help remove those explicit direct calls.

Remaining tasks

There is an ongoing discussion about the naming of this method. Maybe ::sanitize() or prepare() is better.... Let's talk.

Don't hesitate to share your brainstorming with maintainers of UI Suite themes.

πŸ“Œ Task
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡«πŸ‡·France pdureau Paris

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024