You are here

public function ImageHotspotsController::updateAction in Image Hotspots 8

Update hotspot with $hid.

Parameters

string $hid: Hotspot id.

\Symfony\Component\HttpFoundation\Request $request: Request from user to change hotspot.

Return value

\Drupal\Core\Ajax\AjaxResponse AjaxResponse

1 string reference to 'ImageHotspotsController::updateAction'
image_hotspots.routing.yml in ./image_hotspots.routing.yml
image_hotspots.routing.yml

File

src/Controller/ImageHotspotsController.php, line 100

Class

ImageHotspotsController
Class ImageHotspotsController.

Namespace

Drupal\image_hotspots\Controller

Code

public function updateAction($hid, Request $request) {
  if (!$this
    ->accessCallback()) {
    $code = 403;
    $parameters['error'] = $this
      ->t('You did a lot actions with hotspots and can not update this hotspot right now. Wait some seconds before you can do it again.');
  }
  else {

    /** @var \Drupal\image_hotspots\Entity\ImageHotspot $hotspot */
    $hotspot = ImageHotspot::load($hid);
    if (is_null($hotspot)) {
      $code = 404;
      $parameters['error'] = $this
        ->t('Can not find hotspot with hid @hid', [
        '@hid' => $hid,
      ]);
    }
    else {
      $parameters = $request->request
        ->all();
      $parameters['title'] = preg_replace('/(javascript)+(\\s)*:+/', "", Xss::filter($parameters['title']));
      $parameters['description'] = preg_replace('/(javascript)+(\\s)*:+/', "", Xss::filter($parameters['description']));
      $parameters['link'] = preg_replace('/(javascript)+(\\s)*:+/', "", Xss::filter($parameters['link']));
      $parameters['target'] = preg_replace('/(javascript)+(\\s)*:+/', "", Xss::filter($parameters['target']));
      $hotspot
        ->setTitle($parameters['title']);
      $hotspot
        ->setDescription($parameters['description']);
      $hotspot
        ->setLink($parameters['link']);
      $hotspot
        ->setTargetLink($parameters['target']);
      $hotspot
        ->setCoordinates([
        'x' => $parameters['x'],
        'y' => $parameters['y'],
        'x2' => $parameters['x2'],
        'y2' => $parameters['y2'],
      ]);
      try {
        $hotspot
          ->save();
        $this
          ->disableCache($hotspot
          ->getTarget());
        $code = 200;
        $parameters['hid'] = $hotspot
          ->id();
      } catch (EntityStorageException $e) {
        $code = 500;
        $parameters['error'] = $e
          ->getMessage();
      }
    }
  }
  return new AjaxResponse($parameters, $code);
}