FlagAction.php in Flag 8.4
File
src/Plugin/Action/FlagAction.php
View source
<?php
namespace Drupal\flag\Plugin\Action;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Entity\DependencyTrait;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\flag\FlagServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FlagAction extends ActionBase implements ContainerFactoryPluginInterface, DependentPluginInterface {
use DependencyTrait;
protected $flagOperation;
protected $flag;
protected $flagService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, FlagServiceInterface $flag_service) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->flagService = $flag_service;
$this->flag = $this->flagService
->getFlagById($configuration['flag_id']);
$this->flagOperation = $configuration['flag_action'];
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('flag'));
}
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
$result = $this->flag
->actionAccess($this->flagOperation, $account, $object);
return $return_as_object ? $result : $result
->isAllowed();
}
public function execute($entity = NULL) {
if ($entity) {
try {
if ($this->flagOperation === 'unflag') {
$this->flagService
->unflag($this->flag, $entity);
}
else {
$this->flagService
->flag($this->flag, $entity);
}
} catch (\LogicException $e) {
}
}
}
public function calculateDependencies() {
if ($this->flag) {
$this
->addDependency('config', $this->flag
->getConfigDependencyName());
}
return $this->dependencies;
}
}
Classes
Name |
Description |
FlagAction |
Flag action for flagging/unflagging entities for a given flag. |