3 namespace Drupal\editor;
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;
16 * Returns responses for Editor module routes.
18 class EditorController extends ControllerBase {
21 * Returns an Ajax response to render a text field without transformation filters.
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
30 * @param string $view_mode_id
31 * The view mode the formatted text field should be rerendered in.
33 * @return \Drupal\Core\Ajax\AjaxResponse
36 public function getUntransformedText(EntityInterface $entity, $field_name, $langcode, $view_mode_id) {
37 $response = new AjaxResponse();
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));
48 * Apply the necessary XSS filtering for using a certain text format's editor.
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.
55 * @return \Symfony\Component\HttpFoundation\JsonResponse
56 * A JSON response containing the XSS-filtered value.
58 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
59 * Thrown if no value to filter is specified.
61 * @see editor_filter_xss()
63 public function filterXss(Request $request, FilterFormatInterface $filter_format) {
64 $value = $request->request->get('value');
66 throw new NotFoundHttpException();
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);
78 return new JsonResponse(editor_filter_xss($value, $filter_format, $original_format));