Score:-2

Why an exception is raised when I insert a token as a value of an html src attribute

sa flag

How to raise the exception:

In any text_area field using ckeditor, insert the following code <img src="[node:my_custom_token]"> and save the form.

You will get the following error: (assuming the edited form was http://mysite/fr/node/8/edit)

page not found|127.0.0.1|http://mysite/fr/node/8/[node:my_custom_token]

This is a NotFoundHttpException thrown in RouterListener

Here is the trace

PHP internal call Symfony\Component\HttpKernel\EventListener\RouterListener->onKernelRequest(Symfony\Component\HttpKernel\Event\GetResponseEvent $event)
<ROOT>/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php:142 call_user_func($function_name, $parameters)
<ROOT>/vendor/symfony/http-kernel/HttpKernel.php:134 Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch($event)
<ROOT>/vendor/symfony/http-kernel/HttpKernel.php:80 Symfony\Component\HttpKernel\HttpKernel->handleRaw(Symfony\Component\HttpFoundation\Request $request, int $type = 1)
<ROOT>/core/lib/Drupal/Core/StackMiddleware/Session.php:57 Symfony\Component\HttpKernel\HttpKernel->handle(Symfony\Component\HttpFoundation\Request $request, $type = 1, $catch = true)
<ROOT>/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php:47 Drupal\Core\StackMiddleware\Session->handle(Symfony\Component\HttpFoundation\Request $request, $type = 1, $catch = true)
<ROOT>/core/modules/page_cache/src/StackMiddleware/PageCache.php:106 Drupal\Core\StackMiddleware\KernelPreHandle->handle(Symfony\Component\HttpFoundation\Request $request, $type = 1, $catch = true)
<ROOT>/core/modules/page_cache/src/StackMiddleware/PageCache.php:85 Drupal\page_cache\StackMiddleware\PageCache->pass(Symfony\Component\HttpFoundation\Request $request, $type = 1, $catch = true)
<ROOT>/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php:47 Drupal\page_cache\StackMiddleware\PageCache->handle(Symfony\Component\HttpFoundation\Request $request, $type = 1, $catch = true)
<ROOT>/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php:52 Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Symfony\Component\HttpFoundation\Request $request, $type = 1, $catch = true)
<ROOT>/vendor/stack/builder/src/Stack/StackedHttpKernel.php:23 Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Symfony\Component\HttpFoundation\Request $request, $type = 1, $catch = true)
<ROOT>/core/lib/Drupal/Core/DrupalKernel.php:716 Stack\StackedHttpKernel->handle(Symfony\Component\HttpFoundation\Request $request, $type = 1, $catch = true)
Baud avatar
sa flag
Do you mind to explain the down votes. I think it is valuable for anyone to understand the process behind the "validation" (by ckeditor?) of the url in the html attributes like src.
Jaypan avatar
de flag
Your token isn't getting replaced. I can't tell you why not though. Maybe the text filter you are using doesn't replace tokens. It won't have anything to do with your editor though.
Baud avatar
sa flag
Thank you @Jaypan. My question is not about the replacement of the token but I would like to understand which/where is the process/code which is validating the url of an image src attribute. This is not a token related question: the exception is raised if you replace the token by a string like "i_am_not_a_token"
Kevin avatar
in flag
The token is not being replaced when the template/page is rendered, so the browser is requesting a URL that does not exist. That is basically it. Either the filter is not processing tokens, or there is something wrong in your custom token logic.
Jaypan avatar
de flag
Kevin filled in the reason why the answer I gave is resulting in the error you are seeing.
Score:3
us flag

If you are editing an entity and you save the entity, you are redirected to the page showing the entity.
As in the entity content you added <img src="[node:my_custom_token]">, the browser is trying to access http://your-site-domain/[node:my_custom_token] to get the image data for the image it should show. Drupal doesn't find any route associated with that URL and it logs a Page not found error and throws an exception.

That isn't related to using tokens, as that could happen whether you enter a URL for your site that doesn't exist, for the src attribute.

If you are wondering why that token isn't replaced, that's because the entity form field isn't set to replace tokens, which it can also happens when the entity field is a Text (formatted, long) field for which none of the input filters set for its format handle tokens.

Baud avatar
sa flag
Thank you!! What is strange, is the fact that the site is not redirecting to the page showing the (custom) entity; thus I don't know where the rendering happens... Anyway, regarding "Drupal doesn't find any route associated with that URL and it logs a Page not found error and throws an exception." Is it possible to avoid it? [FYI: This is a config entity which is storing templates (html + tokens) to produce pdf in other part of the site]
apaderno avatar
us flag
The question says the form was on http://mysite/fr/node/8/edit. That's a node entity, not a configuration entity.
Baud avatar
sa flag
You are right, it was to not over complicate the question... a very bad idea!!! The real path is `admin/structure/token_templates/pdf_aw_card_body/edit` where `pdf_aw_card_body` is the id of my config entity (Before asking the question here, I tested with a regular edit node route and the exception was also raised...)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.