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:
- Create an entity (article)
- Create a Pathauto pattern using the following pattern:
/[suggestion:lookup:article_collection:entity:url:path]/[node:title]
- Create the suggestion
article_collection
- Don't use the created suggestion anywhere
- 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
Comment #2
wengerkComment #3
wengerkHere a first draft with & without the bug & the coverage.
Let's see if it's fail & success as desired.
Comment #4
vints24 commented@WengerK : I tested and everything works good.
Thank you.
Comment #6
wengerkComment #7
wengerkComment #8
wengerk