Skip to content
Snippets Groups Projects

Added the MR Form the Patch as per the #9 still needs tests .

Open Prem Suthar requested to merge issue/flag-2983605:2983605-when-entity-is into 8.x-4.x
2 files
+ 80
45
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -4,14 +4,18 @@ namespace Drupal\flag\Controller;
use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\flag\Ajax\ActionLinkFlashCommand;
use Drupal\flag\FlagInterface;
use Drupal\flag\FlagServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
@@ -68,7 +72,7 @@ class ActionLinkController implements ContainerInjectionInterface {
* @param string $view_mode
* The flaggable entity view mode.
*
* @return \Drupal\Core\Ajax\AjaxResponse|null
* @return \Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse|null
* The response object, only if successful.
*
* @see \Drupal\flag\Plugin\Reload
@@ -76,20 +80,21 @@ class ActionLinkController implements ContainerInjectionInterface {
public function flag(FlagInterface $flag, $entity_id, ?string $view_mode = NULL) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $this->flagService->getFlaggableById($flag, $entity_id);
if ($entity != NULL && $entity instanceof EntityInterface) {
if ($entity === NULL) {
throw new NotFoundHttpException();
}
try {
$this->flagService->flag($flag, $entity);
}
catch (\LogicException $e) {
// Fail silently so we return to the entity, which will show an updated
// link for the existing state of the flag.
}
try {
$this->flagService->flag($flag, $entity);
} catch (\LogicException $e) {
// Fail silently so we return to the entity, which will show an updated
// link for the existing state of the flag.
}
return $this->generateResponse($flag, $entity, $flag->getMessage('flag'), $view_mode);
return $this->generateResponse($flag, $entity, $flag->getMessage('flag'));
}
return $this->refreshAjaxReferer();
}
/**
@@ -110,20 +115,21 @@ class ActionLinkController implements ContainerInjectionInterface {
public function unflag(FlagInterface $flag, $entity_id, ?string $view_mode = NULL) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $this->flagService->getFlaggableById($flag, $entity_id);
if ($entity === NULL) {
throw new NotFoundHttpException();
}
try {
$this->flagService->unflag($flag, $entity);
if ($entity != NULL && $entity instanceof EntityInterface) {
if ($entity === NULL) {
throw new NotFoundHttpException();
}
try {
$this->flagService->unflag($flag, $entity);
} catch (\LogicException $e) {
// Fail silently so we return to the entity, which will show an updated
// link for the existing state of the flag.
}
return $this->generateResponse($flag, $entity, $flag->getMessage('unflag'));
}
catch (\LogicException $e) {
// Fail silently so we return to the entity, which will show an updated
// link for the existing state of the flag.
}
return $this->generateResponse($flag, $entity, $flag->getMessage('unflag'), $view_mode);
return $this->refreshAjaxReferer();
}
/**
@@ -165,4 +171,19 @@ class ActionLinkController implements ContainerInjectionInterface {
return $response;
}
/**
* Catching the type error if entity is deleted.
*
* @return \Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse
*/
private function refreshAjaxReferer(){
$referer = \Drupal::request()->server->get('HTTP_REFERER');
$fake_request = Request::create($referer);
$uri = $fake_request->getRequestUri();
$response = new AjaxResponse();
$response->addCommand(new RedirectCommand($uri));
return $response;
}
}
Loading