Fix the code in \Drupal\render_example\Controller\RenderExampleController::preRender()

Created on 17 July 2018, over 6 years ago
Updated 14 July 2024, 5 months ago

When \Drupal\render_example\Controller\RenderExampleController::preRender() is added as pre-render callback to the render array returned by \Drupal\render_example\Controller\RenderExampleController::arrays(), the following test methods fails.

  • Drupal\Tests\render_example\Functional\RenderExampleTest::testRenderExample(): The text "Hello ug26w3ez, welcome to the #cache example." was not found anywhere in the text of the current page.
  • Drupal\Tests\render_example\Functional\RenderExampleMenuTest::testRenderExampleLinksExist(): Current response status code is 500, but 200 expected.

\Drupal\render_example\Controller\RenderExampleController::preRender() must be changed to allow those tests to pass.

As a side note, the correct line to add RenderExampleController::preRender() as callback is the following one.

$build['#pre_render'] = [[static::class, 'preRender']];
📌 Task
Status

Needs work

Version

4.0

Component

Render Example

Created by

🇺🇸United States mile23 Seattle, WA

Live updates comments and jobs are added and updated live.
  • Needs reroll

    The patch will have to be re-rolled with new suggestions/changes described in the comments in the issue.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Drupal 11.x uses the following code, for example.

    $class = static::class;
    return [
      '#input' => TRUE,
      '#theme' => 'input__date',
      '#process' => [
        [$class, 'processAjaxForm'],
      ],
      '#pre_render' => [[$class, 'preRenderDate']],
      '#theme_wrappers' => ['form_element'],
      '#attributes' => ['type' => 'date'],
      '#date_date_format' => 'Y-m-d',
    ];
    

    The correct code to use in the RenderExampleController class would be the following one.

        // Properties that contain callbacks can also reference methods on a class
        // in addition to functions. See
        // \Drupal\render_example\Controller\RenderExampleController::preRender()
        $build['#pre_render'] = [[static::class, 'preRender']];
    
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    [static::class . '::postRenderAddPrefix'] used by that class works because it is an array with a singe string. [static::class, 'preRender'] contains two strings and Drupal would use '\Drupal\render_example\Controller\RenderExampleController' (the class name) as callback, which would work only if the class has a __invoke() method.

  • Merge request !42Issue #2986435: Fix RenderExampleController → (Open) created by apaderno
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    184 pass, 17 fail
  • Status changed to Needs review 6 months ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Pipeline finished with Success
    6 months ago
    Total: 183s
    #208615
  • 🇫🇷France andypost

    somehow 17 tests failed but only 13 in other issues

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    The only failing tests this module implements are the following ones.

    • Drupal\Tests\render_example\Functional\RenderExampleTest::testRenderExample()
      Behat\Mink\Exception\ResponseTextException: The text "Hello ug26w3ez, welcome to the #cache example." was not found anywhere in the text of the current page.
    • Drupal\Tests\render_example\Functional\RenderExampleMenuTest::testRenderExampleLinksExist
      Behat\Mink\Exception\ExpectationException: Current response status code is 500, but 200 expected.
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    184 pass, 17 fail
  • Pipeline finished with Success
    6 months ago
    Total: 178s
    #209471
  • Merge request !43Issue #2986435: Fix RenderExampleController → (Open) created by apaderno
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    186 pass, 13 fail
  • Pipeline finished with Success
    6 months ago
    Total: 178s
    #210037
  • Status changed to Active 6 months ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    I updated the issue summary to make clearer what needs to be fixed.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    apaderno changed the visibility of the branch 2986435-without-prerender-test to hidden.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    186 pass, 13 fail
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    MR !42 can be used as start to fix this issue.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Pipeline finished with Success
    6 months ago
    Total: 213s
    #210056
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    IMO, the code in \Drupal\render_example\Controller\RenderExampleController::preRender()is too complex to show what a pre-render callback can do, and it duplicates the code already used from \Drupal\render_example\Controller\RenderExampleController::arrays().

    $output = [];
    // We are going to create a new output render array that pairs each
    // example with a set of helper render arrays. These are used to display
    // the description as a title and the unrendered content alongside the
    // examples.
    foreach (Element::children($build) as $key) {
      if (isset($build[$key])) {
        $output[$key] = [
          '#theme' => 'render_array',
          'description' => [
            '#type' => 'markup',
            '#markup' => $build[$key]['#description'] ?? '',
          ],
          'rendered' => $build[$key],
          'unrendered' => [
            '#type' => 'markup',
            '#markup' => htmlentities(Variable::export($build[$key])), 
          ],
        ];
      }
    }
    
    foreach (Element::properties($build) as $key) {
      $output[$key] = $build[$key];
    }
    

    I would rather keep it simple like \Drupal\render_example\Controller\RenderExampleController::postRenderAddPrefix().

    public static function postRenderAddPrefix($markup, array $element) {
      $markup .= '<div style="color:blue">This markup was added after rendering by a #post_render callback.</div>';
      return $markup;
    }
    
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    PHPLint Failed
  • Status changed to Needs review 6 months ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Pipeline finished with Failed
    6 months ago
    Total: 244s
    #210156
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    184 pass, 17 fail
  • Pipeline finished with Success
    6 months ago
    Total: 242s
    #210183
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    184 pass, 17 fail
  • Pipeline finished with Success
    6 months ago
    Total: 181s
    #210242
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    184 pass, 17 fail
  • Pipeline finished with Success
    6 months ago
    Total: 186s
    #210459
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    184 pass, 17 fail
  • Pipeline finished with Success
    6 months ago
    Total: 206s
    #210480
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    184 pass, 17 fail
  • Pipeline finished with Success
    6 months ago
    Total: 179s
    #210525
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    184 pass, 17 fail
  • Pipeline finished with Success
    6 months ago
    Total: 179s
    #210543
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    apaderno changed the visibility of the branch 2986435-fix-RenderExampleController to hidden.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    The failing tests after the last commit in the fork are the following ones.

    Drupal\Tests\render_example\Functional\RenderExampleTest::testRenderExample()
    Behat\Mink\Exception\ResponseTextException: The text "This element has been altered by the #pre_render callback." was not found anywhere in the text of the current page.
    Drupal\Tests\hooks_example\Functional\HooksExampleTest::testHooksExample()
    Behat\Mink\Exception\ExpectationException: Current response status code is 403, but 200 expected.

    From the first failing test, I gather that either the #pre_render callback is not invoked, or the render array has a structure that is different from what that callback expects. Since the last commit commented out (just for testing) the code that builds a new render array from $build, the issue is not how the new render array is built.
    🐛 var_export() does not handle circular references Needs review , which does not use the #pre_render callback, but changes how the build array items are shown in the page (using json_export() instead of Variables::export()), does not cause any failure in the tests defined by the Render Example module. It means the tests failing for this MR are not caused by what reported in the other issue.

    As for the second failing test, the status code is now 403 instead of 500.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    186 pass, 13 fail
  • Pipeline finished with Success
    6 months ago
    Total: 190s
    #210696
  • Assigned to apaderno
  • Status changed to Needs work 6 months ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    PHPLint Failed
  • Pipeline finished with Failed
    6 months ago
    Total: 272s
    #210833
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    PHPLint Failed
  • Pipeline finished with Failed
    6 months ago
    Total: 202s
    #210862
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    186 pass, 13 fail
  • Pipeline finished with Success
    6 months ago
    Total: 187s
    #210894
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    184 pass, 17 fail
  • Pipeline finished with Success
    6 months ago
    Total: 189s
    #210944
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    184 pass, 17 fail
  • Pipeline finished with Success
    6 months ago
    Total: 181s
    #210986
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 6 months ago
    186 pass, 13 fail
  • Pipeline finished with Success
    6 months ago
    Total: 183s
    #211007
  • Issue was unassigned.
  • Status changed to Needs review 6 months ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    I am crediting jmohino, who created the related issue, which gave me a confirm of what should have been fixed for RenderExampleController::preRender() to work without tests failures, and optlai, who confirmed the bug described in the related issue was still present.

  • Status changed to Needs work 5 months ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
Production build 0.71.5 2024