Hello,
After updated my Pathauto module to 8.x-1.2, I get this message when I want to create a new content:
Error message
<strong> Deprecated function: mb_check_encoding(): Calling mb_check_encoding() without argument is deprecated in Drupal\pathauto\PathautoState::getPathautoStateKey() (line 182 of modules/contrib/pathauto/src/PathautoState.php).</strong>
Drupal\pathauto\PathautoState::getPathautoStateKey(NULL) (Line: 45)
Drupal\pathauto\PathautoFieldItemList->computeValue() (Line: 34)
Drupal\path\Plugin\Field\FieldType\PathFieldItemList->ensureComputedValue() (Line: 141)
Drupal\path\Plugin\Field\FieldType\PathFieldItemList->count()
count(Object) (Line: 74)
Drupal\Core\Field\WidgetBase->form(Object, Array, Object) (Line: 183)
Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm(Object, Array, Object) (Line: 121)
Drupal\Core\Entity\ContentEntityForm->form(Array, Object) (Line: 127)
Drupal\node\NodeForm->form(Array, Object) (Line: 106)
Drupal\Core\Entity\EntityForm->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 536)
Drupal\Core\Form\FormBuilder->retrieveForm('node_article_block_cta_form', Object) (Line: 283)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 580)
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: 169)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
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: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
<strong>Deprecated function: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\pathauto\PathautoState::getPathautoStateKey() (line 183 of modules/contrib/pathauto/src/PathautoState.php).
</strong>
Drupal\pathauto\PathautoState::getPathautoStateKey(NULL) (Line: 45)
Drupal\pathauto\PathautoFieldItemList->computeValue() (Line: 34)
Drupal\path\Plugin\Field\FieldType\PathFieldItemList->ensureComputedValue() (Line: 141)
Drupal\path\Plugin\Field\FieldType\PathFieldItemList->count()
count(Object) (Line: 74)
Drupal\Core\Field\WidgetBase->form(Object, Array, Object) (Line: 183)
Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm(Object, Array, Object) (Line: 121)
Drupal\Core\Entity\ContentEntityForm->form(Array, Object) (Line: 127)
Drupal\node\NodeForm->form(Array, Object) (Line: 106)
Drupal\Core\Entity\EntityForm->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 536)
Drupal\Core\Form\FormBuilder->retrieveForm('node_article_block_cta_form', Object) (Line: 283)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 580)
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: 169)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
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: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Comments
Comment #2
abhishek_gupta1 commentedComment #3
shreya_th commentedComment #4
shreya_th commentedComment #5
abhishek_gupta1 commentedComment #7
abhishek_gupta1 commented@lorseau, I have fix the issue Deprecated function: mb_check_encoding(), please kindly review the Patch file
Comment #8
abhishek_gupta1 commentedComment #10
roshni27 commentedPlease review the patch.
Comment #11
lorseau commentedHello,
I checked the 3394550-10.patch and now I get these errors:
Comment #12
lorseau commented@abhishek_virasat, looks like the patch failed
and there is the mb_check_encoding() error but also the strlen() error too
Comment #13
abhishek_gupta1 commented@lorseau, you mean to say, you applied the "pathauto-3394550.patch" but still you got same error and also for strlen() ? or you not able to apply ?
Comment #14
lorseau commented@abhishek_virasat, I didn't applied your patch as it failed
Comment #16
roshni27 commentedUpdated patch please review.
Comment #17
lorseau commentedthanks @roshni27
I still have the second error:
Comment #18
roshni27 commentedThis code should handle various cases, including when entity_id is null or not a valid string.
Comment #20
berdirThe caller of getPathautoStateKey() should ensure that it's a string, and I thought we already do that.
Comment #21
lorseau commented@roshni27, 3394550-18.patch works for me
thanks
Comment #22
roshni27 commented@Berdir Please suggest me what logic we should implement here when $entity_id is null?
Comment #23
berdirInvestigate the backtrace where this error happens and don't call this method then. We already have an isNew() check in \Drupal\pathauto\PathautoState::getOriginalValue() for example.
The backtrace in #17 suggests to me that @lorseau is *not* using the most recent version or is using patches because those line numbers are different to where I'm seeing calls.
Comment #24
lorseau commented@Berdir
I have the latest version, but I also have these patches:
"Issue #3179835: Migrate forum pattern to taxonomy term forums if forum is enabled on the source site": "https://www.drupal.org/files/issues/2022-01-06/pathauto-migrate_forum_pa...",
"Issue #3179865: Derive pathauto pattern migrations to solve inaccurate pattern migration dependencies": "https://www.drupal.org/files/issues/2021-04-06/pathauto-derive_pathauto_...",
"Issue #3182708: Migrate language-specific patterns": "https://www.drupal.org/files/issues/2022-01-06/pathauto-migrate_multilin...",
"Issue #3079275: Custom aliases (which are not generated with the actual patterns) can be lost during the migration": "https://www.drupal.org/files/issues/2022-01-06/pathauto-prevent_losing_c...",
"Issue #3190980: [PP-1] Allow source counts to be cached: implement ::doCount() instead of ::count()": "https://www.drupal.org/files/issues/2021-01-05/3190980-2.patch",
Comment #27
chrisdarke commented@Berdir, the issue that I was able to replicate with 8.x-1.x was that PathautoFieldItemList::computeValue() was getting a null value from $this->getEntity()->id(), which can happen.. unless I misunderstand there can be cases where enforceIsNew is set but id is null.
I added an updated MR that successfully installs and fixes the issue on my local instance.
Comment #29
mably commentedCode review
The commit correctly addresses the issue. When creating a new entity (before save),
$entity->id()returnsNULL. This NULL gets passed togetPathautoStateKey()which callsmb_check_encoding(NULL, 'ASCII')andstrlen(NULL)— both deprecated since PHP 8.1.The fix adds an early return
if ($entity_id === NULL) return NULL;which prevents the deprecation warnings. The return type is correctly updated toint|string|null.Downstream safety
The main caller that triggers this with NULL is
getOriginalValue()at line 75:It already has an
isNew()guard, but there are edge cases whereisNew()returns FALSE whileid()is still NULL (e.g. entity cloning). I've added an additional|| $entity->id() === NULLguard ingetOriginalValue()so NULL never reaches the key-value store'sget()method either — which would otherwise violate itsstringtype hint.The other callers (
persist()at line 109 andpurge()at line 117) only run on saved entities so they won't receive NULL.Comment #30
mably commentedAdded a kernel test
testUnsavedEntityPathFieldAccess()that reproduces the bug by creating a node without saving it and accessing itspathfield. A custom error handler captures any deprecation warnings, then the test asserts that nomb_check_encoding()orstrlen()deprecations were triggered. It also verifies thatPathautoState::getPathautoStateKey(NULL)returnsNULLgracefully. Verified that the test fails without the fix and passes with it.Comment #32
mably commentedComment #33
berdirPer my review comment, the test does not fail for me on $node->path->pathauto.
> unless I misunderstand there can be cases where enforceIsNew() is set but id is null.
there was in fact a a core bug a while ago that I fixed that allowed this, you could force a new entity to be not new, and then this would trigger this case. But that bug has been fixed in core, isNew() will _never_ return FALSE now if the entity has no ID. That did happen sometime between 2023 and now. So it is possible that some edge case triggered this back then, but that is no longer happening on supported core versions.