tmgmt_extension_suit-8.x-3.4/src/Plugin/Action/BaseJobAction.php
src/Plugin/Action/BaseJobAction.php
<?php
namespace Drupal\tmgmt_extension_suit\Plugin\Action;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Class BaseJobAction.
*
* @package Drupal\tmgmt_extension_suit\Plugin\Action
*/
abstract class BaseJobAction extends ActionBase implements ContainerFactoryPluginInterface {
/**
* The tempstore factory.
*
* @var \Drupal\Core\TempStore\PrivateTempStoreFactory
*/
protected $tempStoreFactory;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Creates TranslateNode action.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
* The tempstore factory.
* @param \Drupal\Core\Session\AccountInterface $current_user
* Current user.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
PrivateTempStoreFactory $temp_store_factory,
AccountInterface $current_user
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $current_user;
$this->tempStoreFactory = $temp_store_factory;
}
/**
* {@inheritdoc}
*/
public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE) {
// ** @var \Drupal\node\NodeInterface $object */
// @TODO: Move the check to own service.
$result = content_translation_translate_access($object);
// Return $return_as_object ? $result : $result->isAllowed();
return TRUE;
}
/**
* Returns temp storage name.
*
* @param string $entity_type
* Entity type.
*
* @return mixed
* Returns storage name.
*/
abstract protected function getTempStoreName($entity_type = '');
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition,
$container->get('tempstore.private'),
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*/
public function executeMultiple(array $entities) {
$ids = [];
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
foreach ($entities as $entity) {
$ids[$entity->id()] = $entity->getEntityTypeId();
}
$entity = reset($entities);
if ($entity instanceof EntityInterface) {
$entity_type = $entity->getEntityTypeId();
}
$this->tempStoreFactory->get($this->getTempStoreName($entity_type))
->set($this->currentUser->id(), $ids);
}
/**
* {@inheritdoc}
*/
public function execute(?ContentEntityInterface $entity = NULL) {
$this->executeMultiple([$entity]);
}
}
