- 🇮🇹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. - last update
6 months ago 184 pass, 17 fail - Status changed to Needs review
6 months ago 11:37am 26 June 2024 - 🇮🇹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.
- last update
6 months ago 184 pass, 17 fail - last update
6 months ago 186 pass, 13 fail - Status changed to Active
6 months ago 6:26pm 27 June 2024 - 🇮🇹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.
- last update
6 months ago 186 pass, 13 fail - 🇮🇹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; }
- last update
6 months ago PHPLint Failed - Status changed to Needs review
6 months ago 9:18pm 27 June 2024 - last update
6 months ago 184 pass, 17 fail - last update
6 months ago 184 pass, 17 fail - last update
6 months ago 184 pass, 17 fail - last update
6 months ago 184 pass, 17 fail - last update
6 months ago 184 pass, 17 fail - last update
6 months ago 184 pass, 17 fail - 🇮🇹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.
- Merge request !47Issue #2986435: Fix the code in RenderExampleController::preRender() → (Open) created by apaderno
- last update
6 months ago 186 pass, 13 fail - Assigned to apaderno
- Status changed to Needs work
6 months ago 11:55am 28 June 2024 - last update
6 months ago PHPLint Failed - last update
6 months ago PHPLint Failed - last update
6 months ago 186 pass, 13 fail - last update
6 months ago 184 pass, 17 fail - last update
6 months ago 184 pass, 17 fail - last update
6 months ago 186 pass, 13 fail - Issue was unassigned.
- Status changed to Needs review
6 months ago 4:11pm 28 June 2024 - 🇮🇹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 8:55am 14 July 2024