Unsupported operand types in LinkFormatter->buildUrl()

Created on 1 October 2020, about 4 years ago
Updated 26 July 2024, 5 months ago

Problem/Motivation

When populating a link field programmatically, if there is no options array entered, the following error happens:



This happens with PHP version 7.4.10.

Steps to reproduce

  $node->set('field_link', [
    'uri' => 'https://google.com',
    'title' => 'Google',
  ]);
  $node->save();

then view that node and an error is thrown. A work around for this is to enter an empty array for options:

  $node->set('field_link', [
    'uri' => 'https://google.com',
    'title' => 'Google',
    'options' => [],
  ]);
  $node->save();

Proposed resolution

The offending code is located on line 245 of /core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php.

    $options += $url->getOptions();

If options isn't set as an array on saving the field, in PHP 7.4 we cannot do the += operation. I can see two possible resolutions, one being on line 244:

    $options = $item->options ?: [];

The other option is change line 245 to:

    $options = array_merge($options,$url->getOptions());

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Closed: duplicate

Version

9.3

Component
Link 

Last updated about 7 hours ago

Created by

🇺🇸United States robbmlewis

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

  • 🇮🇳India lolmanKD

    Hi All,

    This issue is also related to this Error Unsupported operand types
    Error: Unsupported operand types in Drupal\Core\Utility\LinkGenerator->generate() (line 161 of /core/lib/Drupal/Core/Utility/LinkGenerator.php)

    We faced this issue on multiple pages like /sitemap and etc post D8.9.x(latest) to D9.3.x migration

    I have added a patch for Drupal 9.3.x

  • 🇮🇳India chaitanyadessai Goa

    Patch applied successfully from #7 , fixed issue for Drupal 9.3.x.

  • 🇺🇸United States hungdo

    I am attaching a new patch file to support 9.5.x.

  • Getting WSOD with Unsupported operand types in LinkFormatter->buildUrl() error in D10.1 after updating to PHP 8.1.27.
    Successfully installed the patch in #22, but still getting an error.

    PHP Fatal error:  Uncaught TypeError: array_merge(): Argument #1 must be of type array, string given in /var/www/.../web/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php:247
    Stack trace:
    #0 /var/www/.../web/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php(247): array_merge()
    #1 /var/www/.../web/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php(178): Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter->buildUrl()
    #2 /var/www/.../web/core/lib/Drupal/Core/Field/FormatterBase.php(89): Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter->viewElements()
    #3 /var/www/.../web/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php(265): Drupal\Core\Field\FormatterBase->view()
    #4 /var/www/.../web/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php(268): Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple()
    #5 /var/www/.../web/core/lib/Drupal/Core/Entity/EntityViewBuilder.php(339): Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildMultiple()
    #6 /var/www/.../web/core/modules/node/src/NodeViewBuilder.php(24): Drupal\Core\Entity\EntityViewBuilder->buildComponents()
    #7 /var/www/.../web/core/lib/Drupal/Core/Entity/EntityViewBuilder.php(281): Drupal\node\NodeViewBuilder->buildComponents()
    #8 /var/www/.../web/core/lib/Drupal/Core/Entity/EntityViewBuilder.php(238): Drupal\Core\Entity\EntityViewBuilder->buildMultiple()
    #9 [internal function]: Drupal\Core\Entity\EntityViewBuilder->build()
    #10 /var/www/.../web/core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php(111): call_user_func_array()
    #11 /var/www/.../web/core/lib/Drupal/Core/Render/Renderer.php(788): Drupal\Core\Render\Renderer->doTrustedCallback()
    #12 /var/www/.../web/core/lib/Drupal/Core/Render/Renderer.php(377): Drupal\Core\Render\Renderer->doCallback()
    #13 /var/www/.../web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\Core\Render\Renderer->doRender()
    #14 /var/www/.../web/core/lib/Drupal/Core/Render/Renderer.php(160): Drupal\Core\Render\Renderer->render()
    #15 /var/www/.../web/core/lib/Drupal/Core/Render/Renderer.php(583): Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}()
    #16 /var/www/.../web/core/lib/Drupal/Core/Render/Renderer.php(161): Drupal\Core\Render\Renderer->executeInRenderContext()
    #17 /var/www/.../web/core/modules/node/src/Plugin/Search/NodeSearch.php(525): Drupal\Core\Render\Renderer->renderPlain()
    #18 /var/www/.../web/core/modules/node/src/Plugin/Search/NodeSearch.php(489): Drupal\node\Plugin\Search\NodeSearch->indexNode()
    #19 /var/www/.../web/core/modules/search/search.module(80): Drupal\node\Plugin\Search\NodeSearch->updateIndex()
    #20 /var/www/.../web/core/lib/Drupal/Core/Cron.php(335): search_cron()
    #21 /var/www/.../web/core/lib/Drupal/Core/Extension/ModuleHandler.php(388): Drupal\Core\Cron->Drupal\Core\{closure}()
    #22 /var/www/.../web/core/lib/Drupal/Core/Cron.php(343): Drupal\Core\Extension\ModuleHandler->invokeAllWith()
    #23 /var/www/.../web/core/lib/Drupal/Core/Cron.php(159): Drupal\Core\Cron->invokeCronHandlers()
    #24 /var/www/.../web/core/lib/Drupal/Core/ProxyClass/Cron.php(75): Drupal\Core\Cron->run()
    #25 /var/www/.../web/core/modules/automated_cron/src/EventSubscriber/AutomatedCron.php(65): Drupal\Core\ProxyClass\Cron->run()
    #26 [internal function]: Drupal\automated_cron\EventSubscriber\AutomatedCron->onTerminate()
    #27 /var/www/.../web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func()
    #28 /var/www/.../vendor/symfony/http-kernel/HttpKernel.php(115): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch()
    #29 /var/www/.../web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(63): Symfony\Component\HttpKernel\HttpKernel->terminate()
    #30 /var/www/.../web/core/lib/Drupal/Core/DrupalKernel.php(688): Drupal\Core\StackMiddleware\StackedHttpKernel->terminate()
    #31 /var/www/.../vendor/drush/drush/src/Boot/DrupalBoot8.php(331): Drupal\Core\DrupalKernel->terminate()
    #32 [internal function]: Drush\Boot\DrupalBoot8->terminate()
    #33 {main}
      thrown in /var/www/.../web/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php on line 247
    
  • Curiously this error seems to have resolved itself. The error above was reported during a cache rebuild, but is now gone after the cache rebuild and http restart.

Production build 0.71.5 2024