renderResponse example

else {
      // At this point we know callback returned a render element. If the       // element is part of the group (#group is set on it) it won't be rendered       // unless we remove #group from it. This is caused by       // \Drupal\Core\Render\Element\RenderElement::preRenderGroup(), which       // prevents all members of groups from being rendered directly.       if (!empty($result['#group'])) {
        unset($result['#group']);
      }

      /** @var \Drupal\Core\Ajax\AjaxResponse $response */
      $response = $this->ajaxRenderer->renderResponse($result$request$this->routeMatch);
    }

    foreach ($commands as $command) {
      $response->addCommand($command, TRUE);
    }
    return $response;
  }

}

  public function attachments() {
    \Drupal::state()->set('common_test.hook_page_attachments.early_rendering', TRUE);
    $build = [
      '#title' => 'A title',
      'content' => ['#markup' => 'Some content'],
    ];
    return \Drupal::service('main_content_renderer.html')->renderResponse($build, \Drupal::requestStack()->getCurrentRequest(), \Drupal::routeMatch());
  }

}
/** * Tests the content method. * * @covers ::renderResponse */
  public function testRenderWithFragmentObject() {
    $main_content = ['#markup' => 'example content'];
    $request = new Request();
    $route_match = $this->createMock('Drupal\Core\Routing\RouteMatchInterface');
    /** @var \Drupal\Core\Ajax\AjaxResponse $result */
    $result = $this->ajaxRenderer->renderResponse($main_content$request$route_match);

    $this->assertInstanceOf('Drupal\Core\Ajax\AjaxResponse', $result);

    $commands = $result->getCommands();
    $this->assertEquals('insert', $commands[0]['command']);
    $this->assertEquals('example content', $commands[0]['data']);

    $this->assertEquals('insert', $commands[1]['command']);
    $this->assertEquals('status_messages', $commands[1]['data']);
  }

}
$request = $event->getRequest();
    $result = $event->getControllerResult();

    // Render the controller result into a response if it's a render array.     if (is_array($result) && ($request->query->has(static::WRAPPER_FORMAT) || $request->getRequestFormat() == 'html')) {
      $wrapper = $request->query->get(static::WRAPPER_FORMAT, 'html');

      // Fall back to HTML if the requested wrapper envelope is not available.       $wrapper = isset($this->mainContentRenderers[$wrapper]) ? $wrapper : 'html';

      $renderer = $this->classResolver->getInstanceFromDefinition($this->mainContentRenderers[$wrapper]);
      $response = $renderer->renderResponse($result$request$this->routeMatch);
      // The main content render array is rendered into a different Response       // object, depending on the specified wrapper format.       if ($response instanceof CacheableResponseInterface) {
        $main_content_view_subscriber_cacheability = (new CacheableMetadata())->setCacheContexts(['url.query_args:' . static::WRAPPER_FORMAT]);
        $response->addCacheableDependency($main_content_view_subscriber_cacheability);
      }
      $event->setResponse($response);
    }
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.