Error on Printable PDF configuration page

Created on 17 May 2023, over 1 year ago
Updated 18 July 2023, over 1 year ago

Problem/Motivation

When updating the printable Module from version 3.0.0-rc1 the site does not request database updates. Then when I try to convert a node to pdf it is generated correctly, but it includes the following error between its lines:

The website encountered an unexpected error. Please try again later.
TypeError: strpos(): Argument #1 ($haystack) must be of type string, array given in strpos() (line 358 of core\lib\Drupal\Component\Utility\UrlHelper.php).

strpos(Array, ':') (Line: 358)
Drupal\Component\Utility\UrlHelper::stripDangerousProtocols(Array) (Line: 221)
Drupal\Component\Render\FormattableMarkup::placeholderFormat('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', Array) (Line: 195)
Drupal\Core\StringTranslation\TranslatableMarkup->render() (Line: 15)
Drupal\Core\StringTranslation\TranslatableMarkup->__toString() (Line: 134)
__TwigTemplate_0837c79033cef929ca21109ca257dc7a->doDisplay(Array, Array) (Line: 405)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
Twig\Template->display(Array) (Line: 390)
Twig\Template->render(Array) (Line: 55)
twig_render_template('core/themes/claro/templates/form-element.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('form_element', Array) (Line: 489)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 242)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 580)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 243)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 174)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Then when accessing the Printable PDF configuration page the exact same error appears.
I tried uninstalling and reinstalling the module, updating the drupal database even though it did not request changes and clearing caches, but the error persists.

Steps to reproduce

1-Install the PDF API module and Printable version 3.0.0-rc1.
2-Activate the PDF API, Printable and Printable PDF modules.
3-Configure Printable and Printable PDF (in this last one I only added a token for the generation of PDF file names).
4-Update the Printable module to version 3.0.0-rc2.
5-Try to generate a PDF from a node or other content or access the Printable PDF configuration page in /admin/config/user-interface/printable/pdf.

Everything seems to indicate some error with the filename generation field when reviewing the error traces.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡¨πŸ‡ΊCuba dariemlazaro Havana

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @dariemlazaro
  • Status changed to Postponed: needs info over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States markdorison

    @dariemlazaro Thanks for the report! This issue does not occur on rc1? I am reviewing the diff between rc1 and rc2 and nothing is jumping out at me that would be the likely culprit.

  • Status changed to Needs review over 1 year ago
  • πŸ‡¨πŸ‡ΊCuba dariemlazaro Havana

    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

  • @markdorison opened merge request.
  • πŸ‡ΊπŸ‡ΈUnited States markdorison

    Hi @dariemlazaro,
    Here is the documentation for the FormattableMarkup::placeholderFormat. I wonder if this could be occurring in scenarios where $token_args is an empty array?

    Could you test the change I made in MR33 to see if that resolves the issue?

  • πŸ‡¨πŸ‡ΊCuba dariemlazaro Havana

    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&amp;token=VntZh8KpVVl2BtCXlCOSSxVE2YDDDS2Y9ngz1JiCmGE" class="token-dialog use-ajax" data-dialog-type="dialog" data-dialog-options="{&quot;dialogClass&quot;:&quot;token-tree-dialog&quot;,&quot;width&quot;:600,&quot;height&quot;:400,&quot;position&quot;:{&quot;my&quot;:&quot;right bottom&quot;,&quot;at&quot;:&quot;right-10 bottom-10&quot;},&quot;draggable&quot;:true,&quot;autoResize&quot;: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&amp;token=VntZh8KpVVl2BtCXlCOSSxVE2YDDDS2Y9ngz1JiCmGE" class="token-dialog use-ajax" data-dialog-type="dialog" data-dialog-options="{&quot;dialogClass&quot;:&quot;token-tree-dialog&quot;,&quot;width&quot;:600,&quot;height&quot;:400,&quot;position&quot;:{&quot;my&quot;:&quot;right bottom&quot;,&quot;at&quot;:&quot;right-10 bottom-10&quot;},&quot;draggable&quot;:true,&quot;autoResize&quot;: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';
        }
  • πŸ‡ΊπŸ‡ΈUnited States markdorison

    @dariemlazaro I updated the MR with your suggestion. This should fix the error though I am unsure if this fulfills the original intention of what was trying to be achieved with this code. πŸ€”

  • πŸ‡¨πŸ‡ΊCuba dariemlazaro Havana

    @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.

  • πŸ‡¦πŸ‡ΊAustralia Nigel Cunningham Geelong

    Perhaps we should just use the normal token element?

  • Status changed to RTBC over 1 year ago
  • πŸ‡¨πŸ‡¦Canada leducdubleuet Chicoutimi QC

    I reviewed the commit 4d8c5c7a - Fix token_args rendering and it does not work. No error on the page but the token link is not functional.

    With the complete patch printable-pdf2.patch in comment #6, all is well and the token link displays correctly.

    Thank you!

  • Status changed to Needs review over 1 year ago
  • Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    Waiting for branch to pass
  • πŸ‡ΊπŸ‡ΈUnited States markdorison

    The patch in #6 is not applying cleanly for me. I have rerolled it if someone could take another look.

  • Status changed to RTBC over 1 year ago
  • πŸ‡¨πŸ‡¦Canada leducdubleuet Chicoutimi QC

    I have tested the new patch 3361048-11.patch and it does not work work here on 3.0.0 and current dev.

    But the patch printable-pdf2.patch in comment #6 still works well on 3.0.0. and current dev.

    So the already reviewed patch is still valid, I believe we should hide the new one and commit printable-pdf2.patch if the maintainers agree that it is ok with the original intended purpose.

    Thank you.

  • Status changed to Fixed over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States markdorison
  • πŸ‡¨πŸ‡¦Canada leducdubleuet Chicoutimi QC

    This is great, thanks for committing this!

    But since this is quite major, will there be a new official release including this fix?

  • πŸ‡ΊπŸ‡ΈUnited States markdorison

    3.0.1 has been released!

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

Production build 0.71.5 2024