Blocked by #2256365: Factor render->fragment code out to a service

Problem/Motivation

We have a confusing double-architecture right now with "wrapping controllers" -- based on the mime type of a request and the type of _-property that was used in a route definition -- and View listeners, that can look at the same information to determine what to do with an HtmlFragment object in what circumstances. So let's fix that and standardize on the latter.

Proposed resolution

* Add another listener method to HtmlViewSubscriber that runs before the existing listeners.
* That listener looks for a controller response that is an array; it assumes that it is a render array and uses the RenderHtmlRenderer service to turn it into an HtmlFragment. Then save it back to the event so that the rest of the pipeline can work as it already does.
* Debatable: Also check for strings, turn those into a render array, and then do the above. (Alternatively, do the string handling inside RenderHtmlRenderer.)
* Eliminate HtmlPageController entirely as it will no longer be necessary.
* Instead of wrapping HtmlPageController around _content-using routes, convert the _content to a _controller.

Remaining tasks

Do it.

User interface changes

None.

API changes

_content becomes vestigial for pages. _controller routes, actually, would implicitly be able to return strings or arrays and get the same handling as if they were _content (See #2092647: Consolidate _form, _controller, _entity_form, etc. into just _controller?). Long-term we can deprecate and remove _content, but for BC reasons that will not happen within 8.x.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Crell’s picture

Issue summary: View changes
kgoel’s picture

Status: Active » Needs review
FileSize
1.44 KB

I have addressed first two proposed solution and going to address rest later today. This is just to see if test bot likes this patch.

kgoel’s picture

Addressed rest of the proposed resolutions except - Instead of wrapping HtmlPageController around _content-using routes, convert the _content to a _controller because of API change.

Status: Needs review » Needs work

The last submitted patch, 3: controller-render-2334229-3.patch, failed testing.

kgoel’s picture

Status: Needs work » Needs review
FileSize
10.89 KB

Status: Needs review » Needs work

The last submitted patch, 5: controller-render-2334229-5.patch, failed testing.

kgoel’s picture

Status: Needs work » Needs review
FileSize
10.86 KB

Status: Needs review » Needs work

The last submitted patch, 7: controller-render-2334229-7.patch, failed testing.

kgoel’s picture

Status: Needs work » Needs review
FileSize
11.21 KB

Status: Needs review » Needs work

The last submitted patch, 9: controller-render-2334229-9.patch, failed testing.

Crell’s picture

  1. +++ b/core/lib/Drupal/Core/Controller/CreateHtmlFragmentTrait.php
    @@ -0,0 +1,62 @@
    +trait CreateHtmlFragmentTrait {
    +
    +  /**
    +   * The title resolver.
    +   *
    +   * @var \Drupal\Core\Controller\TitleResolverInterface
    +   */
    +  protected $titleResolver;
    +
    +  /**
    +   * The render array to HTML fragment renderer.
    +   *
    +   * @var \Drupal\Core\Page\RenderHtmlRendererInterface
    +   */
    +  protected $renderHtmlRenderer;
    +
    

    I'm unclear on the point of this trait.

    If making a trait like this, don't rely on properties being defined properly. You can't guarantee what the using class will do. Instead, get these services via a protected abstract method. Then it's the using class's responsibility to get access to those services and expose them via those methods.

  2. +++ b/core/lib/Drupal/Core/Controller/CreateHtmlFragmentTrait.php
    @@ -0,0 +1,62 @@
    +    // Allow controllers to return a HtmlFragment or a Response object directly.
    

    This trait doesn't technically no from controllers. Just "If the content is already a fragment or a response there is no work to do."

  3. +++ b/core/lib/Drupal/Core/EventSubscriber/HtmlViewSubscriber.php
    @@ -42,10 +46,31 @@ class HtmlViewSubscriber implements EventSubscriberInterface {
    +    if (is_array($result)) {
    +      $renderer = $this->htmlRenderer;
    +      $fragment = $this->createHtmlFragment($result, $event->getRequest());
    +      $event->setControllerResult($fragment);
    +    }
    

    The $renderer = line seems unused.

It looks like a lot of the test fails are fatal errors, which means they should be easy to replicate manually in the UI for debugging.

jibran’s picture

kgoel’s picture

I am not sure if any work needed on this issue since https://www.drupal.org/node/2327277 is postponed and https://www.drupal.org/node/2352155?

kgoel’s picture

Status: Needs work » Postponed

Marking it as postponed

catch’s picture

Status: Postponed » Closed (duplicate)

Yes marking duplicate of #2352155: Remove HtmlFragment/HtmlPage.