comparison core/modules/media/src/Controller/OEmbedIframeController.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
2 2
3 namespace Drupal\media\Controller; 3 namespace Drupal\media\Controller;
4 4
5 use Drupal\Component\Utility\Crypt; 5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Core\Cache\CacheableMetadata; 6 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\Core\Cache\CacheableResponse;
8 use Drupal\Core\DependencyInjection\ContainerInjectionInterface; 7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use Drupal\Core\Render\HtmlResponse;
9 use Drupal\Core\Render\RenderContext; 9 use Drupal\Core\Render\RenderContext;
10 use Drupal\Core\Render\RendererInterface; 10 use Drupal\Core\Render\RendererInterface;
11 use Drupal\Core\Url; 11 use Drupal\Core\Url;
12 use Drupal\media\IFrameMarkup; 12 use Drupal\media\IFrameMarkup;
13 use Drupal\media\IFrameUrlHelper; 13 use Drupal\media\IFrameUrlHelper;
130 } 130 }
131 131
132 // Return a response instead of a render array so that the frame content 132 // Return a response instead of a render array so that the frame content
133 // will not have all the blocks and page elements normally rendered by 133 // will not have all the blocks and page elements normally rendered by
134 // Drupal. 134 // Drupal.
135 $response = new CacheableResponse(); 135 $response = new HtmlResponse();
136 $response->addCacheableDependency(Url::createFromRequest($request)); 136 $response->addCacheableDependency(Url::createFromRequest($request));
137 137
138 try { 138 try {
139 $resource_url = $this->urlResolver->getResourceUrl($url, $max_width, $max_height); 139 $resource_url = $this->urlResolver->getResourceUrl($url, $max_width, $max_height);
140 $resource = $this->resourceFetcher->fetchResource($resource_url); 140 $resource = $this->resourceFetcher->fetchResource($resource_url);
141
142 $placeholder_token = Crypt::randomBytesBase64(55);
141 143
142 // Render the content in a new render context so that the cacheability 144 // Render the content in a new render context so that the cacheability
143 // metadata of the rendered HTML will be captured correctly. 145 // metadata of the rendered HTML will be captured correctly.
144 $element = [ 146 $element = [
145 '#theme' => 'media_oembed_iframe', 147 '#theme' => 'media_oembed_iframe',
151 '#cache' => [ 153 '#cache' => [
152 // Add the 'rendered' cache tag as this response is not processed by 154 // Add the 'rendered' cache tag as this response is not processed by
153 // \Drupal\Core\Render\MainContent\HtmlRenderer::renderResponse(). 155 // \Drupal\Core\Render\MainContent\HtmlRenderer::renderResponse().
154 'tags' => ['rendered'], 156 'tags' => ['rendered'],
155 ], 157 ],
158 '#attached' => [
159 'html_response_attachment_placeholders' => [
160 'styles' => '<css-placeholder token="' . $placeholder_token . '">',
161 ],
162 'library' => [
163 'media/oembed.frame',
164 ],
165 ],
166 '#placeholder_token' => $placeholder_token,
156 ]; 167 ];
157 $content = $this->renderer->executeInRenderContext(new RenderContext(), function () use ($resource, $element) { 168 $content = $this->renderer->executeInRenderContext(new RenderContext(), function () use ($resource, $element) {
158 return $this->renderer->render($element); 169 return $this->renderer->render($element);
159 }); 170 });
160 $response 171 $response
161 ->setContent($content) 172 ->setContent($content)
173 ->setAttachments($element['#attached'])
162 ->addCacheableDependency($resource) 174 ->addCacheableDependency($resource)
163 ->addCacheableDependency(CacheableMetadata::createFromRenderArray($element)); 175 ->addCacheableDependency(CacheableMetadata::createFromRenderArray($element));
164 } 176 }
165 catch (ResourceException $e) { 177 catch (ResourceException $e) {
166 // Prevent the response from being cached. 178 // Prevent the response from being cached.