strnatcasecmp(): Passing null to parameter #2 ($string2) of type string is deprecated

Created on 17 October 2022, almost 3 years ago
Updated 10 February 2024, over 1 year ago

Problem/Motivation

When using PHP 8.1 the following warnings are displayed/logged.

Deprecated function: strnatcasecmp(): Passing null to parameter #2 ($string2) of type string is deprecated in Drupal\Core\Block\BlockManager->Drupal\Core\Plugin\{closure}() (line 97 of core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php).

Steps to reproduce

See ๐Ÿ› Add missing category to Drupal\layout_builder\Plugin\Layout\BlankLayout and let modules and themes alter the list of layouts Fixed . This issue is a duplicate.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Closed: duplicate

Version

11.0 ๐Ÿ”ฅ

Component
Pluginย  โ†’

Last updated 18 days ago

Created by

๐Ÿ‡ฏ๐Ÿ‡ดJordan hanii.gerges

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia nterbogt

    I debugged this further and the error appears to be caused by the strict 'string' argument to `strnatcasecmp`.

    Drupal is passing in strings or `Drupal\Core\StringTranslation\TranslatableMarkup` objects... which used to work when typecast to string.

  • Status changed to Needs review over 2 years ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia nterbogt

    Setting this to needs review. The risk of an empty label is gone, and the bug is clearer, imo.

  • Status changed to Needs work over 2 years ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia nterbogt

    Actually, I think I might have solved a different issue. More thinking required.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly balbogbf

    width #16 i have this error:
    Error: Object of class Drupal\system\Entity\Menu could not be converted to string in Drupal\Core\Block\BlockManager->Drupal\Core\Plugin\{closure}() (linea 99 di ..../core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php).

  • last update over 2 years ago
    Patch Failed to Apply
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States jimconte

    Hello balbogbf ref #19
    Regarding your error, I had the same error and it was due to the project drupal/we_megamenu
    If this is also your issue, there is patch to fix this:

    we_megamenu blocks using "\Drupal\system\Entity\Menu" object as admin label
    https://www.drupal.org/project/we_megamenu/issues/3357546 ๐Ÿ› we_megamenu blocks using "\Drupal\system\Entity\Menu" object as admin label Fixed

  • First commit to issue fork.
  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States jrglasgow Idaho

    I found another similar error and I have it fixed in this updated patch... this patch applies to both 10.2.x and 11.x

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    We have test coverage of editing a layout in core and this error does not occur. This is pointing to an issue somewhere in contrib or custom code. Somewhere where a label is expected there is none.

    Sounds like we need additional test coverage.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States tawellman

    I recently updated to 10.2 and am now getting the following on any Entity Manage form display pages.

    Deprecated function: strnatcasecmp(): Passing null to parameter #2 ($string2) of type string is deprecated in Drupal\Core\Layout\LayoutPluginManager->Drupal\Core\Layout\{closure}() (line 204 of core/lib/Drupal/Core/Layout/LayoutPluginManager.php).
    
    Drupal\Core\Layout\LayoutPluginManager->Drupal\Core\Layout\{closure}(Object, Object)
    uasort(Array, Object) (Line: 207)
    Drupal\Core\Layout\LayoutPluginManager->getSortedDefinitions(Array, 'label') (Line: 217)
    Drupal\Core\Layout\LayoutPluginManager->getGroupedDefinitions() (Line: 230)
    Drupal\Core\Layout\LayoutPluginManager->getLayoutOptions() (Line: 64)
    Drupal\field_layout\Form\FieldLayoutEntityFormDisplayEditForm->form(Array, Object) (Line: 107)
    Drupal\Core\Entity\EntityForm->buildForm(Array, Object)
    call_user_func_array(Array, Array) (Line: 536)
    Drupal\Core\Form\FormBuilder->retrieveForm('entity_form_display_edit_form', Object) (Line: 283)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
    Drupal\Core\Controller\FormController->getContentResult(Object, Object) (Line: 39)
    Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult(Object, Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 627)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 50)
    Drupal\ban\BanMiddleware->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
    Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
    
    Deprecated function: strnatcasecmp(): Passing null to parameter #2 ($string2) of type string is deprecated in Drupal\Core\Layout\LayoutPluginManager->Drupal\Core\Layout\{closure}() (line 204 of core/lib/Drupal/Core/Layout/LayoutPluginManager.php).
    Deprecated function: strnatcasecmp(): Passing null to parameter #2 ($string2) of type string is deprecated in Drupal\Core\Layout\LayoutPluginManager->Drupal\Core\Layout\{closure}() (line 204 of core/lib/Drupal/Core/Layout/LayoutPluginManager.php).
    Deprecated function: strnatcasecmp(): Passing null to parameter #2 ($string2) of type string is deprecated in Drupal\Core\Layout\LayoutPluginManager->Drupal\Core\Layout\{closure}() (line 204 of core/lib/Drupal/Core/Layout/LayoutPluginManager.php).
    Deprecated function: strnatcasecmp(): Passing null to parameter #1 ($string1) of type string is deprecated in Drupal\Core\Layout\LayoutPluginManager->Drupal\Core\Layout\{closure}() (line 204 of core/lib/Drupal/Core/Layout/LayoutPluginManager.php).

    I have searched for a solution. I can confirm the patch from #25 worked for me.

  • ๐Ÿ‡ฆ๐Ÿ‡นAustria jordik

    I see the same message as in #27, but the patch in #25 did not solve it for me.

  • ๐Ÿ‡ฆ๐Ÿ‡นAustria jordik

    Here is a full list of occurrences of the function with file name, line number and in which function. I can gladly look into it and provide a patch, but we I would need guidance on whether we do a typecasting like in #16 or a check if NULL as in #25?

    \core\lib\Drupal\Component\Utility\SortArray.php"(105,12): return strnatcasecmp($a_title, $b_title);
    \core\lib\Drupal\Core\Config\Entity\ConfigEntityBase.php"(237,14): return strnatcasecmp($a_label, $b_label);
    \core\lib\Drupal\Core\Datetime\Entity\DateFormat.php"(91,14): return strnatcasecmp($a_label, $b_label);
    \core\lib\Drupal\Core\Entity\EntityDisplayModeBase.php"(72,19): $type_order = strnatcasecmp($a_type, $b_type);
    \core\lib\Drupal\Core\Language\Language.php"(161,16): return strnatcasecmp($a_name, $b_name);
    \core\lib\Drupal\Core\Layout\LayoutPluginManager.php"(204,16): return strnatcasecmp($a->getCategory() ?? '', $b->getCategory() ?? '');
    \core\lib\Drupal\Core\Layout\LayoutPluginManager.php"(206,14): return strnatcasecmp($a->getLabel() ?? '', $b->getLabel() ?? '');
    \core\lib\Drupal\Core\Plugin\CategorizingPluginManagerTrait.php"(95,16): return strnatcasecmp($a['category'], $b['category']);
    \core\lib\Drupal\Core\Plugin\CategorizingPluginManagerTrait.php"(97,14): return strnatcasecmp($a[$label_key], $b[$label_key]);
    \core\lib\Drupal\Core\Plugin\DefaultLazyPluginCollection.php"(100,12): return strnatcasecmp($a->getPluginId(), $b->getPluginId());
    \core\lib\Drupal\Core\Test\TestDiscovery.php"(190,20): uksort($list, 'strnatcasecmp');
    \core\lib\Drupal\Core\Test\TestDiscovery.php"(192,23): uksort($tests, 'strnatcasecmp');
    \core\modules\config\src\Form\ConfigSingleExportForm.php"(89,28): uasort($entity_types, 'strnatcasecmp');
    \core\modules\config\src\Form\ConfigSingleImportForm.php"(262,28): uasort($entity_types, 'strnatcasecmp');
    \core\modules\config_translation\src\Controller\ConfigTranslationEntityListBuilder.php"(123,12): return strnatcasecmp($a_value, $b_value);
    \core\modules\config_translation\src\Controller\ConfigTranslationMapperList.php"(69,16): return strnatcasecmp($a_title, $b_title);
    \core\modules\filter\src\FilterPluginCollection.php"(100,14): return strnatcasecmp($a->provider, $b->provider);
    \core\modules\help\src\Plugin\HelpSection\HelpTopicSection.php"(146,16): return strnatcasecmp($a_label, $b_label);
    \core\modules\shortcut\src\Entity\Shortcut.php"(186,14): return strnatcasecmp($a->getTitle(), $b->getTitle());
    \core\modules\system\src\Entity\Action.php"(178,14): return strnatcasecmp($a_type, $b_type);
    \core\modules\views_ui\tests\src\Functional\FilterEntityReferenceWebTest.php"(46,14): return strnatcasecmp($a->getTitle(), $b->getTitle());
    \core\modules\workspaces\src\WorkspaceRepository.php"(67,16): return strnatcasecmp($a->label(), $b->label());

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    Prashant.c โ†’ made their first commit to this issueโ€™s fork.

  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    Added the null coalescing operator to occurrences of strnatcasecmp()wherever it was applicable.

    I was not able to apply the patches provided in 11.x therefore submitting the patch file as well.
    Changes needs to be reviewed.

    Thank you.

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Still say we need to research why/how this value got null.

    Will need test coverage

  • ๐Ÿ‡ฆ๐Ÿ‡นAustria jordik

    @smustgrave - can you write the tests?

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States tjtj

    I am also getting this error in Drupal\Core\Layout\LayoutPluginManager->Drupal\Core\Layout\{
    closure}() (line 2

  • Status changed to Closed: duplicate over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States benjifisher Boston area

    @tawellman, thanks for posting the stack trace in Comment #27. That shows the (core, experimental) field_layout module. When I enable that, I can reproduce the error by visiting /en/admin/structure/types/manage/article/form-display (Umami demo profile) as you suggested.

    I have updated the steps to reproduce on that issue, and I am closing this one as a duplicate.

Production build 0.71.5 2024