Problem/Motivation

Gutenberg sends 3 different autocomplete AJAX calls. And the module responses with the same result to all of them.

Proposed resolution

I don't know what "type" parameter means...

Should we maybe add something like this?

    $type = $request->query->get('type');
    if ($type && $type !== 'post') {
      return new JsonResponse([]);
    }

Comments

Leksat created an issue. See original summary.

flyke’s picture

I confirm the problem. Each autocomplete result is displayed 3 times instead of 1.

I think because there are 3 serparate requests, like this:
- search=mieux&per_page=20&type=post&subtype=undefined
- search=mieux&per_page=20&type=term&subtype=undefined
- search=mieux&per_page=20&type=post-format&subtype=undefined

but none of the results are actually filtered by the type parameter, so each of these 3 requests return the same results.

So as you suggest, maybe we need to change something in gutenberg/js/api-fetch.js
(actually in gutenberg/js/api-fetch.es6.js which should afterwards be converted to gutenberg/js/api-fetch.js)

If you need quick fix you can change 'search-content' inside api-fetch.js into:

'search-content': {
      method: 'GET',
      regex: /\/wp\/v2\/search\?(.*)/g,
      process: function process(matches) {
        if (!matches[1].includes('&type=post&')){
          return [];
        }
        return new Promise(function (resolve, reject) {
          $.ajax({
            method: 'GET',
            url: Drupal.url('editor/search'),
            data: parseQueryStrings(matches[1])
          }).done(function (result) {
            resolve(result);
          }).fail(function (err) {
            reject(err);
          });
        });
      }
    },

this works for me.

This is a quick fix for the described problem, but the maintainer of the Drupal gutenberg module should find a way to either make sure only 1 request is happening, maybe without the type and subtype parameters, or make sure that type=post filters the results to actual nodes, and type=term filters the results to actual taxonomy terms.
That would be an actual fix they need to work on, I did not find out how I could do that.

flyke’s picture

Added a patch for this that can be used until someone creates a proper fix.

flyke’s picture

Allright, I think I fixed this the right way now in my new patch.

There ws no need to alter anything in the JS files.

When fixing another one of Gutenberg module's issues (autocomplete results translation) I stumbled upon the correct place to fix this issue too, namely in src/Controller/SearchController.php

This is the place that processes these 3 requests:
- search=mieux&per_page=20&type=post&subtype=undefined
- search=mieux&per_page=20&type=term&subtype=undefined
- search=mieux&per_page=20&type=post-format&subtype=undefined

Now this code did not do anything with the 'type' parameter, which is why there were always 3x the same results.

I have made some adjustments to this code:
- do nothing if type=post-format
- only search nodes if type=post
- only search taxonomy terms if type=term (this was not possible before to search for terms, now it is)
- if the found node or term is available in current language, return that. This fixes #3188514 also.
- Added the result node/term language and type (node or taxonomy term) so it is easier to find the correct result

Relating the other issue as this patch also fixes that issue.

flyke’s picture

In some case I got this error:
Notice: Undefined variable: result in Drupal\gutenberg\Controller\SearchController->search() (line 82 of /app/web/modules/contrib/gutenberg/src/Controller/SearchController.php)
this is now also fixed in new patch #5.

  • marcofernandes committed ce0b257 on 8.x-2.x
    Issue #3206600 by flyke, Leksat, marcofernandes: Link autocomplete:...
marcofernandes’s picture

Status: Active » Fixed

We need to figure out a bit more the Gutenberg internals in order to change those request with type parameter.
Ideally it would be possible to search for any "public" site link (nodes, terms, media, etc) but, for now, let's just handle nodes.
I fixed the duplicated search results based on @flyke discovery.
I also added language support.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.