@markdorison The purpose of this code is to show the link to insert tokens in the field description and not as a form element. The use of :variable
instead of @variable
sintax was to escape dangerous sentences in the "href" attribute of the link to the token popup, but maybe this module already escapes those values and when using :variable
sintax again it validates twice causing that rendering error. I agree that it needs review by the community and maintainers to reach consensus on what is the correct and safest way to display that link in the field description.
Hello @markdorison
I test the code from MR33 but causes a malformed output:
Filename with its location can be entered. If left empty and Save the pdf option has been selected the generated filename defaults to the node's path.The .pdf extension will be appended automatically. This field supports tokens: 'container' --> <!-- BEGIN OUTPUT from 'core/modules/system/templates/container.html.twig' --> <div> <!-- THEME DEBUG --> <!-- THEME HOOK: 'token_tree_link' --> <!-- BEGIN OUTPUT from 'modules/contrib/token/templates/token-tree-link.html.twig' --> <a href="/drupal_site/token/tree?options=%7B%22token_types%22%3A%5B%22all%22%5D%7D&token=VntZh8KpVVl2BtCXlCOSSxVE2YDDDS2Y9ngz1JiCmGE" class="token-dialog use-ajax" data-dialog-type="dialog" data-dialog-options="{"dialogClass":"token-tree-dialog","width":600,"height":400,"position":{"my":"right bottom","at":"right-10 bottom-10"},"draggable":true,"autoResize":false}">Ojear comodines disponibles.</a> <!-- END OUTPUT from 'modules/contrib/token/templates/token-tree-link.html.twig' --> </div> <!-- END OUTPUT from 'core/modules/system/templates/container.html.twig' -->
It seems that the :variable
when validated is formatted erroneously by validation or other reason. For example :token_help
only output @browse_tokens_link
removing the plain text string, also @browse_tokens_link
was not substituted. Instead using @token_help correctly renders the plaintext and @browse_tokens_link, but does not seem to replace it with $token_args either using :token_args or @token_args.
When using @token_args the output is as follows:
Filename with its location can be entered. If left empty and Save the pdf option has been selected the generated filename defaults to the node's path.The .pdf extension will be appended automatically. This field supports tokens: @browse_tokens_link <!-- THEME DEBUG --> <!-- THEME HOOK: 'container' --> <!-- BEGIN OUTPUT from 'core/modules/system/templates/container.html.twig' --> <div> <!-- THEME DEBUG --> <!-- THEME HOOK: 'token_tree_link' --> <!-- BEGIN OUTPUT from 'modules/contrib/token/templates/token-tree-link.html.twig' --> <a href="/drupal_site/token/tree?options=%7B%22token_types%22%3A%5B%22all%22%5D%7D&token=VntZh8KpVVl2BtCXlCOSSxVE2YDDDS2Y9ngz1JiCmGE" class="token-dialog use-ajax" data-dialog-type="dialog" data-dialog-options="{"dialogClass":"token-tree-dialog","width":600,"height":400,"position":{"my":"right bottom","at":"right-10 bottom-10"},"draggable":true,"autoResize":false}">Ojear comodines disponibles.</a> <!-- END OUTPUT from 'modules/contrib/token/templates/token-tree-link.html.twig' --> </div> <!-- END OUTPUT from 'core/modules/system/templates/container.html.twig' -->
Then I changed this on line 209 $token_help = ' This field supports tokens: @browse_tokens_link';
to $token_help = ' This field supports tokens: ';
and this on line 220 '@token_args' => implode(", ",$token_args),
to '@token_args' => $token_args["@browse_tokens_link"],
Resulting code:
$token_args = [
'@browse_tokens_link' => \Drupal::service('renderer')->render($build),
];
$token_help = ' This field supports tokens: ';
}
$form['settings']['print_pdf_filename'] = [
'#type' => 'textfield',
'#title' => $this->t('PDF filename'),
'#default_value' => $this->config('printable.settings')
->get('pdf_location'),
'#description' => $this->t("Filename with its location can be entered. If left empty and Save the pdf option has been selected the generated filename defaults to the node's path.The .pdf extension will be appended automatically. @token_help @token_args", [
'@token_help' => $token_help,
'@token_args' => $token_args["@browse_tokens_link"],
]),
];
if (function_exists('token_element_validate')) {
$form['settings']['print_pdf_filename']['#element_validate'][] = 'token_element_validate';
}
I found the problem in src/Form/FormatConfigurationFormPdf.php line 218. It was caused by a translatable string with variables. Maybe a regression or merge conflict. It seems to use a substitution syntax, but I don't know it.
This is the diff:
'#title' => $this->t('PDF filename'),
'#default_value' => $this->config('printable.settings')
->get('pdf_location'),
'#description' => $this->t("Filename with its location can be entered. If left empty and Save the pdf option has been selected the generated filename defaults to the node's path.The .pdf extension will be appended automatically." . $token_help, $token_args),
'#description' => $this->t("Filename with its location can be entered. If left empty and Save the pdf option has been selected the generated filename defaults to the node's path.The .pdf extension will be appended automatically. :token_help :token_args", [
':token_help' => $token_help,
':token_args' => $token_args,
]),
];
if (function_exists('token_element_validate')) {
$form['settings']['print_pdf_filename']['#element_validate'][] = 'token_element_validate';
My changes were to revert that change and add a line break to improve readability.
$form['settings']['print_pdf_filename'] = [
'#type' => 'textfield',
'#title' => $this->t('PDF filename'),
'#default_value' => $this->config('printable.settings')
->get('pdf_location'),
'#description' => $this->t("Filename with its location can be entered. If left empty and Save the pdf option has been selected the generated filename defaults to the node's path.The .pdf extension will be appended automatically.</br>" . $token_help, $token_args),
];
I think it needs review, if that change was for compatibility issues with Drupal 10.
Patch provided
dariemlazaro → created an issue.
Same experience in Drupal 9.5.9. When I try to regenerate all URL aliases whether taxonomies, contents or users, nothing changes. Only the new contents get the patterns. Also the user aliases only get one language. So if I create the user in Spanish it only takes the alias in Spanish but never generates the alias in English and vice versa.