When enabling both metatags and token I get a lot of warning on every page :

Warning : uasort(): Array was modified by the user comparison function dans Drupal\token\Token->getInfo() (ligne 56 de /home/site/web/modules/contrib/token/src/Token.php).

I thought it was a token related issue but by digging I saw this issue : https://www.drupal.org/node/1163976 : and the answer gave by Dave Reid was :

That would point to some module not providing proper token info via hook_token_info(). You don't have any other contrib modules enabled?

Am I the only one to get this warning ?

Comments

Kgaut created an issue. See original summary.

kgaut’s picture

Just to be clear, every thing seems to work properly. Except that my syslog get polluted ;)

mr.baileys’s picture

Status: Active » Postponed (maintainer needs more info)

While Metatag uses tokens, it does not provide any new tokens. Are you sure the issue is caused by the Metatag module (for example, are you still getting these errors when Metatag is disabled but Token remains enabled?)

kgaut’s picture

Hi,

Uninstalling metatag module remove the error.

I didn't put anything specific on my metatag configuration, even with the standard configuration fresh after a new metatag installation, the error is back.

Thanks for your help.

mr.baileys’s picture

Category: Bug report » Support request

Unable to reproduce with D8.1.x, Metatag 8.x-1.x-dev, Token 8.x-1.0-alpha2...

Can you list the versions you are using, along with your PHP version? Can you also show what's in your Token::sortTokens()-function (there is a known PHP bug where, if you use any debug-statement in that function, it gives the warning you're referring too even though the arrays have not been altered, see https://bugs.php.net/bug.php?id=50688)

Additionally, can you dump the contents of $token_info just prior to sorting in Token::getInfo():56 to see if it contains any clues?

kgaut’s picture

token 8.x-1.0-alpha2
metatag 8.x-1.0-beta4
drupal 8.0.2
PHP 5.6.11

Method Token::getInfo() :

  public function getInfo() {
    if (empty($this->tokenInfo)) {
      $token_info = parent::getInfo();
      foreach (array_keys($token_info['types']) as $type_key) {
        if (isset($token_info['types'][$type_key]['type'])) {
          $base_type = $token_info['types'][$type_key]['type'];
          // If this token type extends another token type, then merge in
          // the base token type's tokens.
          if (isset($token_info['tokens'][$base_type])) {
            $token_info['tokens'] += [$type_key => []];
            $token_info['tokens'][$type_key] += $token_info['tokens'][$base_type];
          }
        }
        else {
          // Add a 'type' value to each token type information.
          $token_info['types'][$type_key]['type'] = $type_key;
        }
      }

      // Pre-sort tokens.
      uasort($token_info['types'], [$this, 'sortTokens']);
      foreach (array_keys($token_info['tokens']) as $type) {
        uasort($token_info['tokens'][$type], [$this, 'sortTokens']);
      }

      $this->tokenInfo = $token_info;
    }

    return $this->tokenInfo;
  }
damienmckenna’s picture

Thanks for helping to track down the root cause of the bug, mr.baileys.

Kgaut: It seems like the bug is triggered by a problem with the tokens, which is why mr.baileys asked you to provide a dump of the $token_info. Looking at the sortTokens() method in the Token module, it uses strnatcmp() to compare the 'name' values of each token, so maybe one of the tokens doesn't have a 'name' value? You might try changing that method to the following:

  protected function sortTokens($token_a, $token_b) {
    if (!isset(token_a['name'])) {
      debug($token_a);
    }
    if (!isset($token_b['name'])) {
      debug($token_a);
    }
    return strnatcmp($token_a['name'], $token_b['name']);
  }

That'll print out the token that's causing the problem.

kgaut’s picture

I dig a bit and it seems that every tokens has a name.

here's the full trace :

note : 'bet, pronos, mespronos, pronostics' are the keywords I set.

Warning: uasort(): Array was modified by the user comparison function in Drupal\token\Token->getInfo() (line 55 of modules/contrib/token/src/Token.php).

Drupal\token\Token->getInfo()
Drupal\metatag\MetatagToken->coreReplace('bet, pronos, mespronos, pronostics', Array, Array)
Drupal\metatag\MetatagToken->contribReplace('bet, pronos, mespronos, pronostics', Array, Array)
Drupal\metatag\MetatagToken->tokenReplace('bet, pronos, mespronos, pronostics', Array)
Drupal\metatag\MetatagManager->generateElements(Array, NULL)
metatag_get_tags_from_route()
metatag_page_attachments(Array)
Drupal\Core\Render\MainContent\HtmlRenderer->invokePageAttachmentHooks(Array)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object)
Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(Object, 'kernel.view', Object)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object)
Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch('kernel.view', Object)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->fetch(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1)
Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1)
Stack\StackedHttpKernel->handle(Object, 1, 1)
Drupal\Core\DrupalKernel->handle(Object)

jose reyero’s picture

The problem is these names are not strings anymore, but TranslatableMarkup and related objects...

I've tried converting them to string before using strnatcmp but no luck either, so I'm trying this quick and dirty workaround, note the '@':

      // Pre-sort tokens.
      @uasort($token_info['types'], [$this, 'sortTokens']);
      foreach (array_keys($token_info['tokens']) as $type) {
        @uasort($token_info['tokens'][$type], [$this, 'sortTokens']);
      }
jose reyero’s picture

Project: Metatag » Token
Category: Support request » Bug report

However the solution to this bug would be not doing such ordering in the first place. TranslatableMarkup objects cannot be ordered without they being converted to strings and that order doesn't make sense anyway because it applies only for a specific language.

I we want to display tokens in order we should be ordering them when being displayed -maybe an after render hook- for a specific language, not here.

Moving this to Token module which is where it belongs and tagging it as Bug.

I've filed a similar issue for Search API, https://www.drupal.org/node/2758321

berdir’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)
lucasgrecco’s picture

There is a problem with uasort function.
Here is the ticket https://bugs.php.net/bug.php?id=50688

matthb’s picture

Im getting this on any php >=5.6 version, extremely annoying.

@lucasgrecco is absolutely right, but I wonder if its worth detecting version and silencing the warning when the uasort is called?

berdir’s picture

On Drupal 8? We don't use that function anymore, you need to figure out which module is causing it for you and report a bug there.

phenom’s picture

Hi,
on Drupal 8.2.7 I found this on
Drupal\Core\Plugin\DefaultLazyPluginCollection->sort() (line 90 of core/lib/Drupal/Core/Plugin/DefaultLazyPluginCollection.php).
and also on
Warning: uasort(): Array was modified by the user comparison function in Drupal\shortcut\Entity\ShortcutSet->getShortcuts() (line 120 of core/modules/shortcut/src/Entity/ShortcutSet.php).
for info I've just enabled Webprofiler. I didn't have any error before.
Cheers.

BartGysens’s picture

Hey,

I just activated Devel Kint and Web Profiler and also get this error. I have Token enabled.
Uninstalling Web Profiler resolved the error.

Backtrace:

( ! ) Warning: uasort(): Array was modified by the user comparison function in /Users/bartgysens/Sites/argus_D8/core/modules/system/src/Form/ModulesListForm.php on line 176
Call Stack
#	Time	Memory	Function	Location
1	0.0002	251128	{main}( )	../index.php:0
2	0.0189	1724296	Drupal\Core\DrupalKernel->handle( )	../index.php:19
3	0.0603	5128736	Stack\StackedHttpKernel->handle( )	../DrupalKernel.php:652
4	0.0603	5128848	Drupal\Core\StackMiddleware\NegotiationMiddleware->handle( )	../StackedHttpKernel.php:23
5	0.0604	5129392	Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware->handle( )	../NegotiationMiddleware.php:50
6	0.0609	5153464	Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle( )	../WebprofilerMiddleware.php:38
7	0.0609	5153896	Drupal\page_cache\StackMiddleware\PageCache->handle( )	../ReverseProxyMiddleware.php:47
8	0.0615	5162280	Drupal\page_cache\StackMiddleware\PageCache->pass( )	../PageCache.php:78
9	0.0616	5162360	Drupal\Core\StackMiddleware\KernelPreHandle->handle( )	../PageCache.php:99
10	0.1452	11935160	Drupal\Core\StackMiddleware\Session->handle( )	../KernelPreHandle.php:47
11	0.1543	12439800	Symfony\Component\HttpKernel\HttpKernel->handle( )	../Session.php:57
12	0.1544	12440752	Symfony\Component\HttpKernel\HttpKernel->handleRaw( )	../HttpKernel.php:64
13	0.3455	25821592	call_user_func_array:{/Users/bartgysens/Sites/argus_D8/vendor/symfony/http-kernel/HttpKernel.php:144} ( )	../HttpKernel.php:144
14	0.3455	25821792	Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}( )	../HttpKernel.php:144
15	0.3455	25822048	Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext( )	../EarlyRenderingControllerWrapperSubscriber.php:97
16	0.3457	25840368	Drupal\Core\Render\Renderer->executeInRenderContext( )	../EarlyRenderingControllerWrapperSubscriber.php:124
17	0.3457	25840816	Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}( )	../Renderer.php:574
18	0.3457	25840864	call_user_func_array:{/Users/bartgysens/Sites/argus_D8/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:123} ( )	../EarlyRenderingControllerWrapperSubscriber.php:123
19	0.3457	25841560	Drupal\Core\Controller\FormController->getContentResult( )	../EarlyRenderingControllerWrapperSubscriber.php:123
20	0.3708	26467464	Drupal\Core\Form\FormBuilder->buildForm( )	../FormController.php:74
21	0.3709	26468312	Drupal\Core\Form\FormBuilder->retrieveForm( )	../FormBuilder.php:271
22	0.3729	26543952	call_user_func_array:{/Users/bartgysens/Sites/argus_D8/core/lib/Drupal/Core/Form/FormBuilder.php:514} ( )	../FormBuilder.php:514
23	0.3729	26544696	Drupal\system\Form\ModulesListForm->buildForm( )	../FormBuilder.php:514
24	2.5552	38144744	uasort ( )