class ActionLinkNoJsController in Flag Lists 4.0.x
Same name and namespace in other branches
- 8 src/Controller/ActionLinkNoJsController.php \Drupal\flag_lists\Controller\ActionLinkNoJsController
Returns nojs responses to flag and unflag action links.
"nojs" is when the user agent has javascript disabled the behaviour reverts to that of a normal link.
After an update the response to a valid request is a redirect to the entity with drupal update message.
Hierarchy
- class \Drupal\flag_lists\Controller\ActionLinkNoJsController implements ContainerInjectionInterface
Expanded class hierarchy of ActionLinkNoJsController
File
- src/
Controller/ ActionLinkNoJsController.php, line 24
Namespace
Drupal\flag_lists\ControllerView source
class ActionLinkNoJsController implements ContainerInjectionInterface {
/**
* The flag service.
*
* @var \Drupal\flag\FlagServiceInterface
*/
protected $flagService;
/**
* The flag lists service.
*
* @var \Drupal\flag_lists\FlagListsServiceInterface
*/
protected $flagListsService;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructor.
*
* @param \Drupal\flag\FlagServiceInterface $flag
* The flag service.
* @param \Drupal\flag_lists\FlagListsServiceInterface $flag_lists
* The flag lists service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(FlagServiceInterface $flag, FlagListsServiceInterface $flag_lists, MessengerInterface $messenger) {
$this->flagService = $flag;
$this->flagListsService = $flag_lists;
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('flag'), $container
->get('flaglists'), $container
->get('messenger'));
}
/**
* Performs a flagging when called via a route.
*
* @param \Drupal\flag\FlagInterface $flag
* The flag entity.
* @param int $entity_id
* The flaggable entity ID.
* @param string $flag_list
* The flag list from the link.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|null
* The response object, only if successful.
*
* @see \Drupal\flag\Plugin\Reload
*/
public function flag(FlagInterface $flag, $entity_id, $flag_list) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $this->flagService
->getFlaggableById($flag, $entity_id);
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.
}
// Create the flag list item.
$actionLinkHelper = new ActionLinkHelper($this->flagListsService);
$actionLinkHelper
->flagHelper($flag, $entity_id, $flag_list);
return $this
->generateResponse($entity, $flag
->getMessage('flag'));
}
/**
* Performs a unflagging when called via a route.
*
* @param \Drupal\flag\FlagInterface $flag
* The flag entity.
* @param int $entity_id
* The flaggable entity ID.
* @param string $flag_list
* The flag list from the link.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|null
* The response object, only if successful.
*
* @see \Drupal\flag\Plugin\Reload
*/
public function unflag(FlagInterface $flag, $entity_id, $flag_list) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $this->flagService
->getFlaggableById($flag, $entity_id);
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.
}
// Remove the flag list item.
$actionLinkHelper = new ActionLinkHelper($this->flagListsService);
$actionLinkHelper
->unflagHelper($flag, $entity_id, $flag_list);
return $this
->generateResponse($entity, $flag
->getMessage('unflag'));
}
/**
* Generates a response after the flag has been updated.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
* @param string $message
* The message to display.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* The response object.
*/
private function generateResponse(EntityInterface $entity, $message) {
$this->messenger
->addMessage($message);
if ($entity
->hasLinkTemplate('canonical')) {
// Redirect back to the entity. A passed in destination query parameter
// will automatically override this.
$url_info = $entity
->toUrl();
$options['absolute'] = TRUE;
$url = Url::fromRoute($url_info
->getRouteName(), $url_info
->getRouteParameters(), $options);
$response = new RedirectResponse($url
->toString());
}
else {
// For entities that don't have a canonical URL (like paragraphs),
// redirect to the front page.
$front = Url::fromUri('internal:/');
$response = new RedirectResponse($front);
}
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ActionLinkNoJsController:: |
protected | property | The flag lists service. | |
ActionLinkNoJsController:: |
protected | property | The flag service. | |
ActionLinkNoJsController:: |
protected | property | The messenger service. | |
ActionLinkNoJsController:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
ActionLinkNoJsController:: |
public | function | Performs a flagging when called via a route. | |
ActionLinkNoJsController:: |
private | function | Generates a response after the flag has been updated. | |
ActionLinkNoJsController:: |
public | function | Performs a unflagging when called via a route. | |
ActionLinkNoJsController:: |
public | function | Constructor. |