Problem/Motivation

Some global page-response properties should be changed for some responses. This is currently possible by implement the provided alter hook or an event subscriber, but the decision to do so often is happening during the custom-elements render process. E.g., a layout builder layout plugins wants to change the global page layout when rendered.

Some use-cases:
- switching to a custom layout, changing property "page": "layout--default",
- changing settings
- customizing metatags

Proposed resolution

Support customizing the page response by writing it into request attributes. By using request attributes the response customization happens cleanly per request and no global state is introduced.

For example, we name the attributes key "lupus_ce_renderer_response_data". After generating the default response, the provided response data attributes should overwrite the default response by following a deep-merge and replace-keys (array-replace?) strategy.
hook_lupus_ce_renderer_response_alter() should come last.

Finally, this should be documented at hook_lupus_ce_renderer_response_alter().

API changes

Only additions, no BC break.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

fago created an issue. See original summary.

fago’s picture

Title: Allow setting global response properties during rendering » Allow modifying response data during rendering

useernamee made their first commit to this issue’s fork.

useernamee’s picture

Assigned: Unassigned » useernamee

I've added this code and a README entry.

  /**
   * Modify response data with request attributes.
   *
   * @param array $data
   *   Response data.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   Request.
   */
  private function modifyResponseData(array &$data, Request $request) {
    $overrides = $request->attributes->get('lupus_ce_renderer_response_data');
    if (is_array($overrides)) {
      $data = array_merge_recursive($data, $overrides);
    }
  }

useernamee’s picture

Status: Active » Needs review
Issue tags: +custom elements
fago’s picture

Status: Needs review » Needs work

there is some missing use statement somewhere still

Exception: TypeError: Argument 2 passed to Drupal\lupus_ce_renderer\CustomElementsRenderer::modifyResponseData() must be an instance of Drupal\lupus_ce_renderer\Request, instance of Symfony\Component\HttpFoundation\Request given, called in /var/www/html/modules/contrib/lupus_ce_renderer/src/CustomElementsRenderer.php on line 210

fago’s picture

else this looks good!

fago’s picture

Status: Needs work » Reviewed & tested by the community

code looks good now, let's do some more testing before merge.

fago’s picture

This passed testing, so is ready to go!

mostepaniukvm made their first commit to this issue’s fork.

mostepaniukvm’s picture

Assigned: useernamee » Unassigned
Status: Reviewed & tested by the community » Fixed

Changed target branch to 2.x in PR , run test on D9.2 and merged

Status: Fixed » Closed (fixed)

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