The token lookup seems causing crash when called with an unused or inexisting suggestion.

Problem/Motivation

When you use the token lookup, for example on Pathauto - as explain on the documentation: https://www.drupal.org/docs/8/modules/template-whisperer/token-replacement - on an still unexisting suggestion or on a suggestion w/o usage, the CRUD of Node using the Pathauto crash

Example of crashing Pathauto

/[suggestion:lookup:article_collection:entity:url:path]/[node:title]

How to reproduce:

  1. Create an entity (article)
  2. Create a Pathauto pattern using the following pattern: /[suggestion:lookup:article_collection:entity:url:path]/[node:title]
  3. Create the suggestion article_collection
  4. Don't use the created suggestion anywhere
  5. Create new article should crash

Solutions

In this file Drupal\template_whisperer\inc\tokens.inc at the line 111 :

    /* @var \Drupal\template_whisperer\Entity\TemplateWhispererSuggestionEntity */
    $suggestion = $data['suggestion'];
    $usages = $tw_manager_usage->listUsage($suggestion);
    $usage = reset($usages);

    // Prevent lookup when the given suggestion do not exists or is never used.
    if (!$usage) {
      return $replacements;
    }

    $entity_type_manager = \Drupal::service('entity_type.manager');

    $entity_storage = $entity_type_manager->getStorage($usage->type);
    $entity = $entity_storage->load($usage->id);

Comments

vints24 created an issue. See original summary.

wengerk’s picture

Issue summary: View changes
Status: Active » Needs work
wengerk’s picture

StatusFileSize
new4.84 KB
new4.84 KB

Here a first draft with & without the bug & the coverage.

Let's see if it's fail & success as desired.

vints24’s picture

@WengerK : I tested and everything works good.
Thank you.

  • wengerk authored e029d88 on 8.x-2.x
    Issue #2974817 by wengerk, vints24: Suggestion not found in tokens.inc...
wengerk’s picture

Status: Needs work » Reviewed & tested by the community
wengerk’s picture

Status: Reviewed & tested by the community » Fixed
wengerk’s picture

Status: Fixed » Closed (fixed)