comparison core/modules/editor/src/EditorController.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children 12f9dff5fda9
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\editor;
4
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\editor\Ajax\GetUntransformedTextCommand;
9 use Drupal\filter\Plugin\FilterInterface;
10 use Drupal\filter\FilterFormatInterface;
11 use Symfony\Component\HttpFoundation\JsonResponse;
12 use Symfony\Component\HttpFoundation\Request;
13 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
15 /**
16 * Returns responses for Editor module routes.
17 */
18 class EditorController extends ControllerBase {
19
20 /**
21 * Returns an Ajax response to render a text field without transformation filters.
22 *
23 * @param \Drupal\Core\Entity\EntityInterface $entity
24 * The entity of which a formatted text field is being rerendered.
25 * @param string $field_name
26 * The name of the (formatted text) field that is being rerendered
27 * @param string $langcode
28 * The name of the language for which the formatted text field is being
29 * rerendered.
30 * @param string $view_mode_id
31 * The view mode the formatted text field should be rerendered in.
32 *
33 * @return \Drupal\Core\Ajax\AjaxResponse
34 * The Ajax response.
35 */
36 public function getUntransformedText(EntityInterface $entity, $field_name, $langcode, $view_mode_id) {
37 $response = new AjaxResponse();
38
39 // Direct text editing is only supported for single-valued fields.
40 $field = $entity->getTranslation($langcode)->$field_name;
41 $editable_text = check_markup($field->value, $field->format, $langcode, [FilterInterface::TYPE_TRANSFORM_REVERSIBLE, FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE]);
42 $response->addCommand(new GetUntransformedTextCommand($editable_text));
43
44 return $response;
45 }
46
47 /**
48 * Apply the necessary XSS filtering for using a certain text format's editor.
49 *
50 * @param \Symfony\Component\HttpFoundation\Request $request
51 * The current request object.
52 * @param \Drupal\filter\FilterFormatInterface $filter_format
53 * The text format whose text editor (if any) will be used.
54 *
55 * @return \Symfony\Component\HttpFoundation\JsonResponse
56 * A JSON response containing the XSS-filtered value.
57 *
58 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
59 * Thrown if no value to filter is specified.
60 *
61 * @see editor_filter_xss()
62 */
63 public function filterXss(Request $request, FilterFormatInterface $filter_format) {
64 $value = $request->request->get('value');
65 if (!isset($value)) {
66 throw new NotFoundHttpException();
67 }
68
69 // The original_format parameter will only exist when switching text format.
70 $original_format_id = $request->request->get('original_format_id');
71 $original_format = NULL;
72 if (isset($original_format_id)) {
73 $original_format = $this->entityManager()
74 ->getStorage('filter_format')
75 ->load($original_format_id);
76 }
77
78 return new JsonResponse(editor_filter_xss($value, $filter_format, $original_format));
79 }
80
81 }