65df04fc9b5071fb44d6f68afc5b602578c96235
[yaffs-website] / web / modules / contrib / embed / src / Controller / EmbedController.php
1 <?php
2
3 namespace Drupal\embed\Controller;
4
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\editor\EditorInterface;
8 use Drupal\embed\Ajax\EmbedInsertCommand;
9 use Drupal\embed\EmbedButtonInterface;
10 use Drupal\filter\FilterFormatInterface;
11 use Symfony\Component\HttpFoundation\Request;
12 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
14 /**
15  * Returns responses for Embed module routes.
16  */
17 class EmbedController extends ControllerBase {
18
19   /**
20    * Returns an Ajax response to generate preview of embedded items.
21    *
22    * Expects the the HTML element as GET parameter.
23    *
24    * @param \Symfony\Component\HttpFoundation\Request $request
25    *   The request object.
26    * @param \Drupal\filter\FilterFormatInterface $filter_format
27    *   The filter format.
28    *
29    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
30    *   Throws an exception if 'value' parameter is not found in the request.
31    *
32    * @return \Symfony\Component\HttpFoundation\Response
33    *   The preview of the embedded item specified by the data attributes.
34    */
35   public function preview(Request $request, FilterFormatInterface $filter_format) {
36     $text = $request->get('value');
37     if ($text == '') {
38       throw new NotFoundHttpException();
39     }
40
41     $build = [
42       '#type' => 'processed_text',
43       '#text' => $text,
44       '#format' => $filter_format->id(),
45     ];
46
47     $response = new AjaxResponse();
48     $response->addCommand(new EmbedInsertCommand($build));
49     return $response;
50   }
51
52   /**
53    * Returns an Ajax response to generate preview of an entity.
54    *
55    * Expects the the HTML element as GET parameter.
56    *
57    * @param \Symfony\Component\HttpFoundation\Request $request
58    *   The request object.
59    * @param \Drupal\editor\EditorInterface $editor
60    *   The editor.
61    * @param \Drupal\embed\EmbedButtonInterface $embed_button
62    *   The embed button.
63    *
64    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
65    *   Throws an exception if 'value' parameter is not found in the request.
66    *
67    * @return \Symfony\Component\HttpFoundation\Response
68    *   The preview of the embedded item specified by the data attributes.
69    */
70   public function previewEditor(Request $request, EditorInterface $editor, EmbedButtonInterface $embed_button) {
71     return $this->preview($request, $editor->getFilterFormat());
72   }
73
74 }