invokePageAttachmentHooks example


  public function assertPageRenderHookExceptions(string $module, string $hook): void {
    $html_renderer = \Drupal::getContainer()->get('main_content_renderer.html');

    // Assert a valid hook implementation doesn't trigger an exception.     $page = [];
    $html_renderer->invokePageAttachmentHooks($page);

    // Assert that hooks can set cache tags.     $this->assertEquals(['example']$page['#cache']['tags']);
    $this->assertEquals(['user.permissions']$page['#cache']['contexts']);

    // Assert an invalid hook implementation doesn't trigger an exception.     \Drupal::state()->set($module . '.' . $hook . '.descendant_attached', TRUE);
    $assertion = $hook . '() implementation that sets #attached on a descendant triggers an exception';
    $page = [];
    try {
      $html_renderer->invokePageAttachmentHooks($page);
      
    // theme wrapper function that allows them to be consistently themed.     $regions = $this->themeManager->getActiveTheme()->getRegions();
    foreach ($regions as $region) {
      if (!empty($page[$region])) {
        $page[$region]['#theme_wrappers'][] = 'region';
        $page[$region]['#region'] = $region;
      }
    }

    // Allow hooks to add attachments to $page['#attached'].     $this->renderer->executeInRenderContext(new RenderContext()function D) use (&$page) {
      $this->invokePageAttachmentHooks($page);
    });

    return [$page$title];
  }

  /** * Invokes the page attachment hooks. * * @param array &$page * A #type 'page' render array, for which the page attachment hooks will be * invoked and to which the results will be added. * * @throws \LogicException * * @internal * * @see hook_page_attachments() * @see hook_page_attachments_alter() */
Home | Imprint | This part of the site doesn't use cookies.