Problem/Motivation

Steps to reproduce

When I visit URL /yoast_seo/tokens I get PHP warnings.

Warning: foreach() argument must be of type array|object, null given in /app/web/modules/contrib/yoast_seo/src/Controller/YoastSeoController.php on line 46
[]

The same warning is logged every time to the logs when I write something to the body field. With every character, I get one line in the logs.

For the fix, I added not empty if to the code to fix it but then I got the next error.
This is the fix I tried to do:

    if (!empty($tokens)) {
      // Retrieve the tokens values.
      // Use the metatag token service, which use either core or token module
      // regarding if this one is installed.
      foreach ($tokens as $token) {
        $token_values[$token] = $metatag_token->replace($token, $data);
      }
    }

This is the next error I got.
Symfony\Component\HttpKernel\Exception\BadRequestHttpException: Input value "tokens" contains a non-scalar value. in Symfony\Component\HttpKernel\HttpKernel->handle() (line 83 of /app/vendor/symfony/http-kernel/HttpKernel.php).

Drupal 10.1.4
PHP 8.1.18

Issue fork yoast_seo-3392193

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mikkmiggur created an issue. See original summary.

mikkmiggur’s picture

Issue summary: View changes
hkirsman’s picture

I believe the issue is the same as here

https://www.drupal.org/project/search_api/issues/3328702

src/Controller/YoastSeoController.php

  public function tokens(Request $request) {
    /** @var MetatagToken $metatag_token */
    $metatag_token = \Drupal::service('metatag.token');
    $token_values = array();
    $tokens       = $request->request->get('tokens');
    $data         = $request->request->get('data');

    if (is_null($data)) {
      $data = array();
    }

->

  public function tokens(Request $request) {
    /** @var MetatagToken $metatag_token */
    $metatag_token = \Drupal::service('metatag.token');
    $token_values = array();
    $tokens       = $request->request->all()['tokens'] ?? [];
    $data         = $request->request->all()['data'];

    if (is_null($data)) {
      $data = array();
    }

No more error locally.

Working:
working

Broken:
broken

hkirsman’s picture

StatusFileSize
new186.93 KB
new190.07 KB

asanchezs made their first commit to this issue’s fork.

bessonweb’s picture

#5 work for me on Drupal 10.2.5

bessonweb’s picture

After update to Drupal 10.3.5, the patch seems not work.

ahmad abbad’s picture

After using patch #5 I'm getting this warning:
Warning: Undefined array key "data" in Drupal\yoast_seo\Controller\YoastSeoController->tokens() (line 37