Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Drupal8 render pipeline


Published on

Insight view on how Drupal8 render pages.

Published in: Technology
  • Be the first to comment

Drupal8 render pipeline

  1. 1. Drupal 8 render pipeline ~ Mahesh Salaria (@salaria) Kelltontech
  2. 2. 2. renderResponse() Select page display variant Is a #type ‘page’ render array? hook_page_attachments() hook_page_(top|bottom)() Example: BlockPageVariant selected. render() main content Yes No Build page display variant The result of a built page display variant is a #type ‘page’ render array. hook_page_attachments() operates on #type ‘page’. The main content must already be rendered, because it might set the page title. render() entire HTML document Only accepts render arrays of #type ‘page’! Wrap in #type ‘html’ Example: response for the path /admin. Renders the 3 parts of the HTML body: page_top, page, page_bottom (where page corresponds to #type ‘page’ and hence page.html.twig). Then renders the entire HTML (i.e. the html.html.twig template). render() main content Add InsertCommand new AjaxResponse() Get dialog options from Request Calculate title if not provided by main content Calculate title if not provided by main content render() main content Add OpenDialogCmd new AjaxResponse() The flow for all four formats supported in core in their respect is displayed next, but the explanations below only apply to the HtmlController! If an unsupported format was negotiated, a 406 response is generated. new HtmlResponse() 5. Calling the Controller 6. The ker nel . vi ew Event 7. The ker nel . r esponse Event Di al ogRender er Modal Render erHt ml Render er Aj axRender er No Response Initialize the corresponding main content renderer Is render array? Yes Main content renderer for request format exists? Yes Call : : r ender Response( ) Set Response on Event Generate 406 Response No Controller returns either: No 1. prepare() helper Calculate title if not provided by main content render() main content Add OpenModalDialogCmd new AjaxResponse() f or mat = ‘ aj ax’f or mat = ‘ modal ’ Mai nCont ent Vi ewSubscr i ber Response Response Response main content renderers f or mat = ‘ ht ml ’ f or mat = ‘ di al og’ DsCSystemController::systemAdminMenuBlockPage is called, it returns a render array with no #type. MainContentViewSubscriber only handles render arrays! If it’s anything else (e.g. an object), then it’s up to another VIEW event subscriber to turn it into a Response. MainContentViewSubscriber looks at the available main content renderer services. These are tagged with render.main_content_renderer. Contributed modules can additional renderers. Example: GET /admin. HTTP request 1. The ker nel . r equest event Determine route & controller HTTP response Negotiate format Request handling and rendering flow Example: /admin path → system.admin route That route has the following attributes: _content: 'DsCSystemController::systemAdminMenuBlockPage' _title: ‘Administration’ Explanation KernelEvents::REQUEST 2. Resolve the Controller 3. The ker nel . cont r ol l er event See http_kernel/introduction.html for more details — this shows the same steps. 4. Getting the Controller Arguments Yes i ndex. php 3. Call Response: : pr epar e( ) 2. Call Ht t pKer nel : : handl e( ) 1. Create Request from globals 4. Call Response: : send( ) 5. Call Ht t pKer nel : : t er mi nat e( ) Ht t pKer nel : : handl e( ) Symfony (HttpKernel) Drupal (Controller, MainContentViewSubscriber, main content renderers)Event handling KernelEvents::CONTROLLER Some parts are greyed out because they’re of lesser importance: they do happen, but are not crucial for understanding the flow. Typically, the format will be html, but it could also be ajax, dialog, modal, json, hal_json, and more. See: Symfony’s Request::setFormat(). _controller is set to 'DsCSystemController::systemAdminMenuBlockPage' KernelEvents::VIEW SELECT_PAGE_DISPLAY_VARIANT KernelEvents::TERMINATE Response Is Response? render array object with associated kernel.view event subscriber Colored overlaid arrows: showing the flow of the different types of Controller return values. Typical flow Note that HtmlResponse (as well as AjaxResponse) at this point still contains attachments: asset libraries, headers … but also placeholders. … Return placeholders unchanged Si ngl eFl ush ( cor e) Bi gPi pe ( cont r i b) … Transform placeholders to BigPipe placeholders placeholder strategies (HTML responses only) Is HTML response and has placeholders attached? : : pr ocessPl acehol der s( ) Set final pl acehol der s on Ht ml Response Yes Ht ml Res’ Pl acehol der St r at ’ Sub Placeholder strategies can transform standard Drupal placeholders (designed for non-deferred rendering) into other placeholders, which may be replaced with the final markup using any way possible. For example: BigPipe, ESI … ChainedPlaceholderStrategy looks at the available placeholder strategies. These are tagged with placeholder_strategy. Contributed modules can additional renderers. BigPipe is a contrib module during 8.0.x, will likely be in 8.1.x core. See KernelEvents::RESPONSE No Ht ml Res’ At t achment sPr ocessor Render pl acehol der s Update HTML to load final assets, set attached headers on response … Process all other HtmlResponse attachments: asset libraries, HTML <head> elements, headers … BigPipe placeholders rendered at the end of::send(), after most is sent.
  3. 3. HttpKernal Component • The HttpKernel component provides a structured process for converting a Request into a Response by making use of the EventDispatcher component. • Every HTTP web interaction begins with a request and ends with a response. Your job as a developer is to create PHP code that reads the request information (e.g. the URL) and creates and returns a response (e.g. an HTML page or JSON string).
  4. 4. Routes • Routes whose controllers return a Response object bypass the pipeline. They rely directly on the Symfony render pipeline. • Route whose controllers return the "main content" as a render array automatically has the ability to be requested in multiple ways: it can be rendered in a certain format (HTML, JSON …) and/or in a certain decorated manner (e.g. with blocks around the main content).
  5. 5. Events • The glue between Symfony and Drupal. • Triggered by HttpKernel::handle()
  6. 6. Controllers • Business Logic to send response object or render array or an Object associated with Event Subscriber.
  7. 7. Content Renderer • After the controller returned a render array, the VIEW will be triggered by the HttpKernel, because the controller result is not a Response, but a render array. • MainContentViewSubscriber checks whether the negotiated request format is supported. • All classes that implement MainContentRendererInterface • AjaxRenderer • DialogRenderer • HtmlRenderer • ModalRenderer
  8. 8. Placeholder strategies (HTML responses only) • SingleFlush (Traditional Way) • BigPipe ( • During rendering, the personalized parts are turned into placeholders. • By default, Drupal 8 uses the Single Flush strategy (aka "traditional") for replacing the placeholders. i.e. we don't send a response until we've replaced all placeholders. • The BigPipe module introduces a new strategy, that allows us to flush the initial page first, and then stream the replacements for the placeholders. • This results in hugely improved front-end/perceived performance (watch the 40-second screencast above).
  9. 9. BigPipe • BigPipe first breaks web pages into multiple chunks called pagelets and process into several stages: • Request parsing: web server parses and sanity checks the HTTP request. • Data fetching: web server fetches data from storage tier. • Markup generation: web server generates HTML markup for the response. • Network transport: the response is transferred from web server to browser. • CSS downloading: browser downloads CSS required by the page. • DOM tree construction and CSS styling: browser constructs DOM tree of the document, and then applies CSS rules on it. • JavaScript downloading: browser downloads JavaScript resources referenced by the page. • JavaScript execution: browser executes JavaScript code of the page. Source:
  10. 10. Refrences • •!lib!Drupal!Core!Routing!routi ng.api.php/group/routing • • • pipelining-web-pages-for-high-performance/389414033919
  11. 11. Discussion! Thank you 