/**
  * A route returning a CacheableResponse object.
  *
  * @return \Drupal\Core\Cache\CacheableResponseInterface
  *   A CacheableResponseInterface object.
  */
 public function cacheableResponse()
 {
     $user = User::load(1);
     $response = new CacheableResponse($user->label());
     $response->addCacheableDependency($user);
     return $response;
 }
 /**
  * Returns the whole sitemap, a requested sitemap chunk, or the sitemap index file.
  *
  * @param int $chunk_id
  *  Optional ID of the sitemap chunk. If none provided, the first chunk or
  *  the sitemap index is fetched.
  *
  * @throws NotFoundHttpException
  *
  * @return object
  *  Returns an XML response.
  */
 public function getSitemap($chunk_id = NULL)
 {
     $output = $this->generator->getSitemap($chunk_id);
     if (!$output) {
         throw new NotFoundHttpException();
     }
     // Display sitemap with correct xml header.
     $response = new CacheableResponse($output, Response::HTTP_OK, ['content-type' => 'application/xml']);
     $meta_data = $response->getCacheableMetadata();
     $meta_data->addCacheTags(['simple_sitemap']);
     return $response;
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public static function buildResponse($view_id, $display_id, array $args = [])
 {
     $build = static::buildBasicRenderable($view_id, $display_id, $args);
     // Set up an empty response, so for example RSS can set the proper
     // Content-Type header.
     $response = new CacheableResponse('', 200);
     $build['#response'] = $response;
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = \Drupal::service('renderer');
     $output = (string) $renderer->renderRoot($build);
     if (empty($output)) {
         throw new NotFoundHttpException();
     }
     $response->setContent($output);
     $cache_metadata = CacheableMetadata::createFromRenderArray($build);
     $response->addCacheableDependency($cache_metadata);
     return $response;
 }
 /**
  * Sets a header.
  */
 public function setHeader(Request $request)
 {
     $query = $request->query->all();
     $response = new CacheableResponse();
     $response->headers->set($query['name'], $query['value']);
     $response->getCacheableMetadata()->addCacheContexts(['url.query_args:name', 'url.query_args:value']);
     $response->setContent($this->t('The following header was set: %name: %value', array('%name' => $query['name'], '%value' => $query['value'])));
     return $response;
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public static function buildResponse($view_id, $display_id, array $args = [])
 {
     $build = static::buildBasicRenderable($view_id, $display_id, $args);
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = \Drupal::service('renderer');
     $output = $renderer->renderRoot($build);
     $response = new CacheableResponse($output, 200);
     $cache_metadata = CacheableMetadata::createFromRenderArray($build);
     $response->addCacheableDependency($cache_metadata);
     $response->headers->set('Content-type', $build['#content_type']);
     return $response;
 }
Exemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function execute()
 {
     parent::execute();
     $output = $this->view->render();
     $header = [];
     $header['Content-type'] = $this->getMimeType();
     $response = new CacheableResponse($this->renderer->renderRoot($output), 200);
     $cache_metadata = CacheableMetadata::createFromRenderArray($output);
     $response->addCacheableDependency($cache_metadata);
     return $response;
 }
Exemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public static function buildResponse($view_id, $display_id, array $args = [])
 {
     $build = static::buildBasicRenderable($view_id, $display_id, $args);
     $build['#cache']['contexts'][] = "url.query_args:callback";
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = \Drupal::service('renderer');
     $output = $renderer->renderRoot($build);
     if (isset($build['#jsonp_callback'])) {
         $response = new CacheableJsonResponse($output, 200);
         $response->setCallback($build['#jsonp_callback']);
     } else {
         $response = new CacheableResponse($output, 200);
     }
     $cache_metadata = CacheableMetadata::createFromRenderArray($build);
     $response->addCacheableDependency($cache_metadata);
     $response->headers->set('Content-type', $build['#content_type']);
     return $response;
 }
Exemplo n.º 8
0
 /**
  * {@inheritdoc}
  *
  * The entire HTML: takes a #type 'page' and wraps it in a #type 'html'.
  */
 public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match)
 {
     list($page, $title) = $this->prepare($main_content, $request, $route_match);
     if (!isset($page['#type']) || $page['#type'] !== 'page') {
         throw new \LogicException('Must be #type page');
     }
     $page['#title'] = $title;
     // Now render the rendered page.html.twig template inside the html.html.twig
     // template, and use the bubbled #attached metadata from $page to ensure we
     // load all attached assets.
     $html = ['#type' => 'html', 'page' => $page];
     // The special page regions will appear directly in html.html.twig, not in
     // page.html.twig, hence add them here, just before rendering html.html.twig.
     $this->buildPageTopAndBottom($html);
     // The three parts of rendered markup in html.html.twig (page_top, page and
     // page_bottom) must be rendered with drupal_render_root(), so that their
     // #post_render_cache callbacks are executed (which may attach additional
     // assets).
     // html.html.twig must be able to render the final list of attached assets,
     // and hence may not execute any #post_render_cache_callbacks (because they
     // might add yet more assets to be attached), and therefore it must be
     // rendered with drupal_render(), not drupal_render_root().
     $this->renderer->render($html['page'], TRUE);
     if (isset($html['page_top'])) {
         $this->renderer->render($html['page_top'], TRUE);
     }
     if (isset($html['page_bottom'])) {
         $this->renderer->render($html['page_bottom'], TRUE);
     }
     $content = $this->renderer->render($html);
     // Set the generator in the HTTP header.
     list($version) = explode('.', \Drupal::VERSION, 2);
     $response = new CacheableResponse($content, 200, ['Content-Type' => 'text/html; charset=UTF-8', 'X-Generator' => 'Drupal ' . $version . ' (https://www.drupal.org)']);
     // Bubble the cacheability metadata associated with the rendered render
     // arrays to the response.
     foreach (['page_top', 'page', 'page_bottom'] as $region) {
         if (isset($html[$region])) {
             $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($html[$region]));
         }
     }
     // Also associate the "rendered" cache tag. This allows us to invalidate the
     // entire render cache, regardless of the cache bin.
     $default = new CacheableMetadata();
     $default->setCacheTags(['rendered']);
     $response->addCacheableDependency($default);
     return $response;
 }
Exemplo n.º 9
0
 /**
  * Processing the API request.
  */
 public function processRequest(Request $request, RouteMatchInterface $route_match, $service_endpoint_id, $service_definition_id)
 {
     /** @var $service_endpoint \Drupal\services\ServiceEndpointInterface */
     $service_endpoint = $this->entityManager()->getStorage('service_endpoint')->load($service_endpoint_id);
     //TODO - pull in settings from service API and alter response
     /** @var $service_def \Drupal\services\ServiceDefinitionInterface */
     $service_def = $this->serviceDefinitionManager->createInstance($service_definition_id, []);
     /**
      * Iterate over any contexts defined for this plugin and extract them from
      * the request defaults if the naming is identical. This means that a
      * context named 'node' would match to a url parameter {node} or a route
      * default named 'node'.
      */
     foreach ($service_def->getContextDefinitions() as $context_id => $context_definition) {
         if ($request->attributes->has($context_id)) {
             $context = new Context($context_definition, $request->attributes->get($context_id));
             $service_def->setContext($context_id, $context);
         }
     }
     // Get the data from the plugin.
     $data = $service_def->processRequest($request, $route_match, $this->serializer);
     $code = $service_def->getPluginDefinition()['response_code'];
     $headers = [];
     $messages = drupal_get_messages();
     if ($messages) {
         foreach ($messages as $type => $type_message) {
             $headers["X-Drupal-Services-Messages-{$type}"] = implode("; ", $type_message);
         }
     }
     // Find the request format to determin how we're going to serialize this data
     $format = $request->getRequestFormat();
     $data = $this->serializer->serialize($data, $format);
     /**
      * Create a new Cacheable Response object with our serialized data, set its
      * Content-Type to match the format of our Request and add the service
      * definition plugin as a cacheable dependency.
      *
      * This last step will extract the cache context, tags and max-ages from
      * any context the plugin required to operate.
      */
     $response = new CacheableResponse($data, $code, $headers);
     $response->headers->add(['Content-Type' => $request->getMimeType($format)]);
     $response->addCacheableDependency($service_def);
     // Be explicit about the caching needs of this response.
     $response->setVary('Accept');
     $service_def->processResponse($response);
     return $response;
 }
Exemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public static function buildResponse($view_id, $display_id, array $args = [])
 {
     $build = static::buildBasicRenderable($view_id, $display_id, $args);
     // Setup an empty response so headers can be added as needed during views
     // rendering and processing.
     $response = new CacheableResponse('', 200);
     $build['#response'] = $response;
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = \Drupal::service('renderer');
     $output = (string) $renderer->renderRoot($build);
     $response->setContent($output);
     $cache_metadata = CacheableMetadata::createFromRenderArray($build);
     $response->addCacheableDependency($cache_metadata);
     $response->headers->set('Content-type', $build['#content_type']);
     return $response;
 }