Side effect in links prop type normalization casting

Created on 20 November 2024, about 1 month ago

Problem/Motivation

In 🐛 [2.0.0-beta4] LinksPropType normalization minor update Active , it introduced:

  protected static function normalizeLink(array $item): array {
...
    if (!is_string($item["title"])) {
      $item["title"] = (string) $item["title"];
    }

In Ensure media library popin is usable Postponed , I have a side effect compared with UIP1 + UIP settings due to: https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/media...

$display_title = [
        '#markup' => $this->t('<span class="visually-hidden">Show </span>@title<span class="visually-hidden"> media</span>', ['@title' => $title]),
      ];
//      $display_title = $this->t('<span class="visually-hidden">Show </span>@title<span class="visually-hidden"> media</span>', ['@title' => $title]);
      if ($allowed_type_id === $selected_type_id) {
        $display_title = [
          '#markup' => $this->t('<span class="visually-hidden">Show </span>@title<span class="visually-hidden"> media</span><span class="active-tab visually-hidden"> (selected)</span>', ['@title' => $title]),
        ];
//        $display_title = $this->t('<span class="visually-hidden">Show </span>@title<span class="visually-hidden"> media</span><span class="active-tab visually-hidden"> (selected)</span>', ['@title' => $title]);
      }

I have tested with the commented out line it is then ok.

I don't know why Core adds a depth level with #markup, but I get "Array" as link title...

I will search why this i done like that, otherwise I will create a core issue to remove this. But I think links prop type should handle this case because I guess It can still happen elsewhere.

Steps to reproduce

Trigger normalize of media library links in a _preprocess_links__media_library_menu:

$variables['preprocessed_items'] = LinksPropType::normalize(\array_filter(
      $variables['links'],
    ));

Proposed resolution

Check if title is a renderable array before cast?

Remaining tasks

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇫🇷France Grimreaper France 🇫🇷

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

Merge Requests

Comments & Activities

  • Issue created by @Grimreaper
  • 🇫🇷France Grimreaper France 🇫🇷
  • 🇫🇷France Grimreaper France 🇫🇷

    I made a small change that fix my use case.

    At least it can be used to open discussion.

  • Pipeline finished with Success
    about 1 month ago
    Total: 347s
    #344633
  • 🇫🇷France pdureau Paris

    We don't allow render arrays in prop values, so in links item titles.

    Instead of skipping the cast to string, we may render the renderable instead.

    Let's discuss

  • 🇫🇷France Grimreaper France 🇫🇷

    Just made a quick call with @pdureau,

    Let's add a normalize in StringPropType to handle those cases then call this normalize in LinksPropType.

    Also use the occasion to potentially get rid of the normalizeAttributes method in LinksPropType to use directly the normalize of AttributesPropType.

  • 🇫🇷France Grimreaper France 🇫🇷

    MR updated

  • Pipeline finished with Failed
    about 1 month ago
    Total: 331s
    #344999
  • 🇫🇷France pdureau Paris

    I see there were already a call to AttributesPropType::normalize() in LinksPropType::normalize(), but you removed some apparently useless logic executed before. I hope it was not done like that in purpose.

    There are 3 phpunit failures in your MR:

    • 1) Drupal\Tests\ui_patterns_field_formatters\Functional\FieldFormatterRenderTest::testRender
    • Test 'field_formatter_default_2' failed for prop/slot 'props' of component ui_patterns_test:test-component. Selector .ui-patterns-props-string.
    • 2) Drupal\Tests\ui_patterns_field_formatters\Functional\LayoutBuilderFieldFormatterRenderTest::testRender
    • Test 'field_formatter_default_2' failed for prop/slot 'props' of component ui_patterns_test:test-component. Selector .ui-patterns-props-string.
    • 3) Drupal\Tests\ui_patterns_field_formatters\Functional\LayoutFieldFormatterRenderTest::testRender
    • Test 'field_formatter_default_2' failed for prop/slot 'props' of component ui_patterns_test:test-component. Selector .ui-patterns-props-string.

    Is it related to your change?

    Also, maybe we can also move some string related logic (like ::normalizeObject()) from AttributesPropType::normalize() to StringPropType::normalize()

  • 🇫🇷France Grimreaper France 🇫🇷

    but you removed some apparently useless logic executed before. I hope it was not done like that in purpose.

    Yes, I think it is a duplicated logic part and the change will give an equivalent result.

    The failures are due to the normalize in StringPropType.

  • 🇫🇷France Grimreaper France 🇫🇷

    The problem is that it is a render array that is passed and if rendered then later on, the

    are escaped. That's why the tests are failing.

    I currently don't know if I should adapt the normalize or it highlights an existing problem.

  • 🇫🇷France Grimreaper France 🇫🇷

    I prefer to discuss it live when you will be back.

  • 🇫🇷France Grimreaper France 🇫🇷

    Other side effect of normalizing link title into a string with ui_icons_menu.

  • 🇫🇷France pdureau Paris

    I have rebased and I got a look. Let's talk.

  • Pipeline finished with Failed
    about 1 month ago
    #350620
  • 🇫🇷France Grimreaper France 🇫🇷
  • Pipeline finished with Canceled
    29 days ago
    Total: 152s
    #351969
  • Pipeline finished with Success
    29 days ago
    Total: 429s
    #351972
  • 🇫🇷France Grimreaper France 🇫🇷
  • Pipeline finished with Success
    28 days ago
    Total: 319s
    #353303
  • Pipeline finished with Failed
    22 days ago
    Total: 680s
    #359291
  • Pipeline finished with Skipped
    22 days ago
    #359366
Production build 0.71.5 2024