You are here

public function ImageHotspotsController::translateAction in Image Hotspots 8

Translate hotspot with $hid.

Parameters

$hid: Hotspot id.

$langcode: Language ID.

\Symfony\Component\HttpFoundation\Request $request:

Return value

\Drupal\Core\Ajax\AjaxResponse

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

File

src/Controller/ImageHotspotsController.php, line 156

Class

ImageHotspotsController
Class ImageHotspotsController.

Namespace

Drupal\image_hotspots\Controller

Code

public function translateAction($hid, $langcode, 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 ImageHotspotInterface $hotspot */
    $hotspot = ImageHotspot::load($hid);
    if (is_null($hotspot)) {
      $code = 404;
      $parameters['error'] = $this
        ->t('Can not find hotspot with hid ') + $hid;
    }
    else {
      if ($hotspot
        ->hasTranslation($langcode)) {
        $hotspot = $hotspot
          ->getTranslation($langcode);
      }
      else {
        $hotspot = $hotspot
          ->addTranslation($langcode);
      }
      $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']));
      $hotspot
        ->setTitle($parameters['title']);
      $hotspot
        ->setDescription($parameters['description']);
      $hotspot
        ->setLink($parameters['link']);
      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);
}