BulkEdit.php in Views Bulk Edit 8
File
src/Plugin/Action/BulkEdit.php
View source
<?php
namespace Drupal\views_bulk_edit\Plugin\Action;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BulkEdit extends ActionBase implements ContainerFactoryPluginInterface {
protected $privateTempStoreFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, PrivateTempStoreFactory $privateTempStoreFactory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->privateTempStoreFactory = $privateTempStoreFactory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('user.private_tempstore'));
}
public function execute($entity = NULL) {
return $this
->executeMultiple([
$entity,
]);
}
public function executeMultiple(array $entities) {
$entities_keyed = [];
foreach ($entities as $entity) {
$entities_keyed[$entity
->id()] = $entity;
}
$this
->persistBulkEditEntityIds($entities_keyed);
}
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
$access = $object
->access('update', $account, TRUE);
$access_result = $access
->andIf(AccessResult::allowedIfHasPermission($account, 'use views bulk edit'));
return $return_as_object ? $access_result : $access_result
->isAllowed();
}
protected function persistBulkEditEntityIds(array $entities_keyed) {
$privateStore = $this->privateTempStoreFactory
->get('views_bulk_edit');
$entity_data = [];
foreach ($entities_keyed as $entity_id => $entity) {
$entity_data[$entity
->getEntityTypeId()][$entity
->bundle()][] = $entity
->id();
}
$privateStore
->set('entity_data', $entity_data);
}
}