[1.0.0-rc2] The menu_footer pattern hook causes a 500 error

Created on 20 May 2024, 6 months ago
Updated 7 June 2024, 6 months ago

Problem/Motivation

Users are encountering a 500 error when displaying links in the footer_menu pattern in the latest version of the DSFR UI Suite theme. This problem has been present since the update to this issue #3340244 📌 [beta6] Replace PHP hooks by more themer-friendly solutions Needs review , which move management to target blank in the pattern.

Steps to reproduce

  1. Create a menu
  2. Enter links (internal, external, mailto)
  3. Associate this menu with the footer_menu pattern
  4. Observe the appearance of the 500 error.

Proposed resolution

Delete the obsolete hook: ui_suite_dsfr_preprocess_pattern_footer_menu from the .theme file

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇷🇪Réunion Martygraphie Saint-Denis (Réunion)

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

Merge Requests

Comments & Activities

  • Issue created by @Martygraphie
  • Status changed to Needs review 6 months ago
  • 🇷🇪Réunion Martygraphie Saint-Denis (Réunion)
  • Status changed to Needs work 6 months ago
  • 🇫🇷France pdureau Paris

    I would not be against removing the hook like you did, because it will not be compatible with SDC / UI patterns 2 anyway.

    But the feature needs to be kept for now because we need to consider people using it.
    Can we add a condition in the hook to prevent the issue instead?

    Associate this menu with the footer_menu pattern

    How? By site building? Where? From presenter template?

    I will try to reproduce the issue.

  • 🇫🇷France mh_nichts

    Hello,

    I've seen the same error, but caused by the button pattern preprocess (visible on pages : /patterns or /patterns/button ou /patterns/button_group) :

    TypeError: strtolower(): Argument #1 ($string) must be of type string, Drupal\Core\Render\Markup given in strtolower() (line 440 of themes/contrib/ui_suite_dsfr/ui_suite_dsfr.theme).
    
    ui_suite_dsfr_preprocess_pattern_button(Array, 'pattern_button', Array)
    ...

    It seems the same kind of condition causes the same error.

    The strange thing is :

    • if we comment those lines in ui_suite_dsfr but implement the same preprocess in our child theme, there is no error, everything works as expected
    • I see the link pattern preprocess has the same kind of condition but it doesn't trigger any error...

    I confirm that this preprocess is not obsolete, as it handles the "external" setting automatically based on the URL, so the best would be to find the cause of the error and fix it, if possible, instead of removing.

  • 🇫🇷France pdureau Paris

    OK, this is 1.0.0-RC2 material :) Let's try to understand and fix this ASAP!

    Do we need this kind of checks?

     use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
     use Drupal\Component\Plugin\Exception\PluginNotFoundException;
    +use Drupal\Component\Render\MarkupInterface;
     use Drupal\Component\Utility\Crypt;
     use Drupal\Component\Utility\UrlHelper;
     use Drupal\Core\Menu\MenuTreeParameters;
    
    @@ -421,6 +401,9 @@ function ui_suite_dsfr_preprocess_pattern_link(array &$variables) {
       if (empty($variables['url'])) {
         return;
       }
    +  if (is_object($variables['url']) && ($variables['url'] instanceof MarkupInterface)) {
    +    $variables['url'] = (string) $variables['url'];
    +  }
       // It seems UrlHelper::setAllowedProtocols() doesn't support mailto.
       if (!str_starts_with(strtolower($variables['url']), 'mailto:')
         && UrlHelper::isExternal($variables['url'])
    @@ -436,6 +419,9 @@ function ui_suite_dsfr_preprocess_pattern_button(array &$variables) {
       if (empty($variables['url'])) {
         return;
       }
    +  if (is_object($variables['url']) && ($variables['url'] instanceof MarkupInterface)) {
    +    $variables['url'] = (string) $variables['url'];
    +  }
       // It seems UrlHelper::setAllowedProtocols() doesn't support mailto.
       if (!str_starts_with(strtolower($variables['url']), 'mailto:')
         && UrlHelper::isExternal($variables['url'])
    @@ -466,6 +452,9 @@ function ui_suite_dsfr_preprocess_pattern_footer_menu(array &$variables) {
         if (empty($item['url'])) {
           return;
         }
    +    if (is_object($item['url']) && ($item['url'] instanceof MarkupInterface)) {
    +      $item['url'] = (string) $item['url'];
    +    }
         // It seems UrlHelper::setAllowedProtocols() doesn't support mailto.
         if (!str_starts_with(strtolower($item['url']), 'mailto:')
             && UrlHelper::isExternal($item['url']) 
  • 🇫🇷France NicociN

    There are cases in `ui_suite_dsfr_preprocess_pattern_footer_menu()` where `$item['url']` is an `Url`object, so we also need to check this "instanceof". I create a MR.

    NB : no need to use `is_object()` as `instanceof` deals with it.

  • Assigned to pdureau
  • Status changed to Needs review 6 months ago
  • 🇫🇷France pdureau Paris

    Great, i will merge tomorrow and release the RC2.

    For your information, with the upcoming UI Patterns 2.x, such task will not needed because the prop type will automatically take care of the normalisation: https://git.drupalcode.org/project/ui_patterns/-/blob/2.0.x/src/PropType...

  • Issue was unassigned.
  • Status changed to Fixed 6 months ago
  • 🇫🇷France pdureau Paris

    Merged

  • Status changed to Needs work 6 months ago
  • 🇷🇪Réunion Martygraphie Saint-Denis (Réunion)

    Hello,
    I'm still having problems with this approach:

    1. Tokens ( and returns an empty url with the approach $item[‘url’]->toString(); , which is annoying because you can no longer distinguish between elements.
    2. Attributes defined for links in the administration space are not retrieved (e.g. class).

    @pdureau, should I create a new isssue?

  • Status changed to Fixed 6 months ago
  • 🇷🇪Réunion Martygraphie Saint-Denis (Réunion)

    I've created a new issue #3449679 , I'm closing this one again

  • 🇫🇷France G4MBINI Bègles
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024