mb_strlen(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\Component\Utility\Unicode::truncate()
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3308167. --> Reported by: [lolgm](https://www.drupal.org/user/3652004) Related to !1 >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>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):</p> <pre>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)<br>#0 /var/www/html/web/core/includes/bootstrap.inc(347): _drupal_error_handler_real(8192, 'mb_strlen(): Pa...', '/var/www/html/w...', 314)<br>#1 [internal function]: _drupal_error_handler(8192, 'mb_strlen(): Pa...', '/var/www/html/w...', 314)<br>#2 /var/www/html/web/core/lib/Drupal/Component/Utility/Unicode.php(314): mb_strlen(NULL)<br>#3 /var/www/html/web/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php(163): Drupal\Component\Utility\Unicode::truncate(NULL, 30, true, false)<br>#4 /var/www/html/web/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php(169): Drupal\Core\Menu\MenuParentFormSelector-&gt;parentSelectOptionsTreeWalk(Array, 'funcional', '----', Array, '', 8, Object(Drupal\Core\Cache\CacheableMetadata))<br>#5 /var/www/html/web/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php(71): Drupal\Core\Menu\MenuParentFormSelector-&gt;parentSelectOptionsTreeWalk(Array, 'funcional', '--', Array, '', 8, Object(Drupal\Core\Cache\CacheableMetadata))<br>#6 /var/www/html/web/core/lib/Drupal/Core/Menu/MenuParentFormSelector.php(81): Drupal\Core\Menu\MenuParentFormSelector-&gt;getParentSelectOptions('', Array, Object(Drupal\Core\Cache\CacheableMetadata))<br>#7 /var/www/html/web/core/modules/menu_ui/menu_ui.module(234): Drupal\Core\Menu\MenuParentFormSelector-&gt;parentSelectElement('main:', '', Array)<br>#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...')<br>#9 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(835): Drupal\Core\Extension\ModuleHandler-&gt;alter('form', Array, Object(Drupal\Core\Form\FormState), 'node_estrutural...')<br>...</pre><p>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".<br> 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.</p> <p>It's a PHP 8.1 + Drupal 9.4.5 installation</p> <h4 id="summary-steps-reproduce">Steps to reproduce</h4> <ol> <li>Use a Drupal 9 + PHP 8.1 installation</li> <li>Add some menu position rules in a menu</li> <li>In a content type allow the addition of nodes of this in the previously configured menu</li> <li>Edit a node of the previously configured content type</li> <li>Go to the reports page (/admin/reports/dblog)</li> </ol> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>In the<br> </p><div class="codeblock"> <pre><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br>MenuPositionLink</span><span style="color: #007700">::</span><span style="color: #0000BB">getTitle</span><span style="color: #007700">()<br></span><span style="color: #0000BB">?&gt;</span></span></pre></div> <p> method must be protected to never return NULL.<br> When it is not possible to obtain the title of the page through<br> </p><div class="codeblock"> <pre><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br>$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">titleResolver</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getTitle</span><span style="color: #007700">()<br></span><span style="color: #0000BB">?&gt;</span></span></pre></div> <p>, must be returned the variable<br> </p><div class="codeblock"> <pre><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br>$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pluginDefinition</span><span style="color: #007700">[</span><span style="color: #DD0000">'title'</span><span style="color: #007700">]<br></span><span style="color: #0000BB">?&gt;</span></span></pre></div> <p>, and finally, if this is also NULL, it must return an empty string.</p> <p><a href="https://git.drupalcode.org/project/menu_position/-/blob/8.x-1.x/src/Plugin/Menu/MenuPositionLink.php#L147">Referred Code</a></p> <div class="codeblock"> <pre><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br></span><span style="color: #FF8000">/**<br>&nbsp;* {@inheritdoc}<br>&nbsp;*/<br></span><span style="color: #007700">public function </span><span style="color: #0000BB">getTitle</span><span style="color: #007700">() {<br>&nbsp; </span><span style="color: #FF8000">// When we're in an admin route we want to display the name of the menu<br>&nbsp; // position rule.<br>&nbsp; // @todo Ensure this translates properly when using configuration<br>&nbsp; //&nbsp;&nbsp; translation.<br>&nbsp; </span><span style="color: #007700">if (</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">adminContext</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">isAdminRoute</span><span style="color: #007700">()) {<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pluginDefinition</span><span style="color: #007700">[</span><span style="color: #DD0000">'title'</span><span style="color: #007700">] ?? </span><span style="color: #DD0000">""</span><span style="color: #007700">;<br>&nbsp; }<br>&nbsp; </span><span style="color: #FF8000">// When we're on a non-admin route we want to display the page title.<br>&nbsp; </span><span style="color: #007700">else {<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$title </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">titleResolver</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getTitle</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">requestStack</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getCurrentRequest</span><span style="color: #007700">(), </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">routeMatch</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getRouteObject</span><span style="color: #007700">());<br>&nbsp;&nbsp;&nbsp; if (</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$title</span><span style="color: #007700">)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$title </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">renderer</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">renderPlain</span><span style="color: #007700">(</span><span style="color: #0000BB">$title</span><span style="color: #007700">);<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; return </span><span style="color: #0000BB">$title </span><span style="color: #007700">?? </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pluginDefinition</span><span style="color: #007700">[</span><span style="color: #DD0000">'title'</span><span style="color: #007700">] ?? </span><span style="color: #DD0000">""</span><span style="color: #007700">;<br>&nbsp; }<br>}<br></span><span style="color: #0000BB">?&gt;</span></span></pre></div> <h3 id="summary-remaining-tasks">Remaining tasks</h3> <p>None.</p> <h3 id="summary-ui-changes">User interface changes</h3> <p>None.</p> <h3 id="summary-api-changes">API changes</h3> <p>None.</p> <h3 id="summary-data-model-changes">Data model changes</h3> <p>None.</p>
issue