mb_strlen(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\Component\Utility\Unicode::truncate()

Created on 6 September 2022, about 2 years ago
Updated 28 April 2023, over 1 year ago

Problem/Motivation

When editing a node that has the option of adding it to a menu that has links of the "Menu Position" type, I get the following error in the logs (admin/reports/dblog):

Deprecated function: mb_strlen(): Passing null to parameter #1 ($string) of type string is deprecated na Drupal\Component\Utility\Unicode::truncate() (linha 314 de /var/www/html/web/core/lib/Drupal/Component/Utility/Unicode.php)
#0 /var/www/html/web/core/includes/bootstrap.inc(347): _drupal_error_handler_real(8192, 'mb_strlen(): Pa...', '/var/www/html/w...', 314)
#1 [internal function]: _drupal_error_handler(8192, 'mb_strlen(): Pa...', '/var/www/html/w...', 314)
#2 /var/www/html/web/core/lib/Drupal/Component/Utility/Unicode.php(314): mb_strlen(NULL)
#3 /var/www/html/web/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php(163): Drupal\Component\Utility\Unicode::truncate(NULL, 30, true, false)
#4 /var/www/html/web/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php(169): Drupal\Core\Menu\MenuParentFormSelector->parentSelectOptionsTreeWalk(Array, 'funcional', '----', Array, '', 8, Object(Drupal\Core\Cache\CacheableMetadata))
#5 /var/www/html/web/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php(71): Drupal\Core\Menu\MenuParentFormSelector->parentSelectOptionsTreeWalk(Array, 'funcional', '--', Array, '', 8, Object(Drupal\Core\Cache\CacheableMetadata))
#6 /var/www/html/web/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php(81): Drupal\Core\Menu\MenuParentFormSelector->getParentSelectOptions('', Array, Object(Drupal\Core\Cache\CacheableMetadata))
#7 /var/www/html/web/core/modules/menu_ui/menu_ui.module(234): Drupal\Core\Menu\MenuParentFormSelector->parentSelectElement('main:', '', Array)
#8 /var/www/html/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(562): menu_ui_form_node_form_alter(Array, Object(Drupal\Core\Form\FormState), 'node_estrutural...')
#9 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(835): Drupal\Core\Extension\ModuleHandler->alter('form', Array, Object(Drupal\Core\Form\FormState), 'node_estrutural...')
...

Although this error does not show anything that indicates that the problem comes from this module, I discovered that it is being caused by trying to generate the list of available items to add the node to the menu, trying to also list the items from the "Menu Position".
As the "Menu Position" items have a dynamic title based on the current route, is passed as NULL in this case, thus giving rise to a warning entry, in the logs (admin/reports/dblog), for each "Menu Position" item of the menus available for selection.

It's a PHP 8.1 + Drupal 9.4.5 installation

Steps to reproduce

  1. Use a Drupal 9 + PHP 8.1 installation
  2. Add some menu position rules in a menu
  3. In a content type allow the addition of nodes of this in the previously configured menu
  4. Edit a node of the previously configured content type
  5. Go to the reports page (/admin/reports/dblog)

Proposed resolution

In the MenuPositionLink::getTitle() method must be protected to never return NULL.
When it is not possible to obtain the title of the page through $this->titleResolver->getTitle(), must be returned the variable $this->pluginDefinition['title'], and finally, if this is also NULL, it must return an empty string.

Referred Code

/**
 * {@inheritdoc}
 */
public function getTitle() {
  // When we're in an admin route we want to display the name of the menu
  // position rule.
  // @todo Ensure this translates properly when using configuration
  //   translation.
  if ($this->adminContext->isAdminRoute()) {
    return $this->pluginDefinition['title'] ?? "";
  }
  // When we're on a non-admin route we want to display the page title.
  else {
    $title = $this->titleResolver->getTitle($this->requestStack->getCurrentRequest(), $this->routeMatch->getRouteObject());
    if (is_array($title)) {
      $title = $this->renderer->renderPlain($title);
    }
    return $title ?? $this->pluginDefinition['title'] ?? "";
  }
}

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇵🇹Portugal lolgm

Live updates comments and jobs are added and updated live.
  • PHP 8.1

    The issue particularly affects sites running on PHP version 8.1.0 or later.

Sign in to follow issues

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