annotate core/modules/editor/src/EditorController.php @ 19:fa3358dc1485 tip

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