View source
<?php
namespace Drupal\redhen_connection\Plugin\Action;
use Drupal\Core\Entity\DependencyTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\redhen_connection\Entity\ConnectionRole;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ChangeConnectionRole extends ViewsBulkOperationsActionBase implements ContainerFactoryPluginInterface {
use DependencyTrait;
protected $entityType;
protected $mismatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeInterface $entity_type) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityType = $entity_type;
$this->mismatch = 0;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager')
->getDefinition('redhen_connection'));
}
public function defaultConfiguration() {
return [
'role' => '',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$rids = \Drupal::entityQuery('redhen_connection_role')
->execute();
$role_objects = ConnectionRole::loadMultiple($rids);
$roles = [];
foreach ($role_objects as $rid => $role_object) {
$roles[$rid] = $role_object
->label();
}
$form['role'] = [
'#type' => 'radios',
'#title' => t('Role'),
'#options' => $roles,
'#default_value' => '',
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['role'] = $form_state
->getValue('role');
}
public function calculateDependencies() {
if (!empty($this->configuration['role'])) {
$prefix = $this->entityType
->getConfigPrefix() . '.';
$this
->addDependency('config', $prefix . $this->configuration['role']);
}
return $this->dependencies;
}
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
$access = $object
->access('update', $account, TRUE);
return $return_as_object ? $access : $access
->isAllowed();
}
public function executeMultiple(array $objects) {
foreach ($objects as $entity) {
$this
->execute($entity);
}
if ($this->mismatch) {
$mismatch_plural = "@count connections could not be updated because the selected role was not associated with the connection type.";
$mismatch_singular = "@count connection could not be updated because the selected role was not associated with the connection type.";
$message = \Drupal::translation()
->formatPlural($this->mismatch, $mismatch_singular, $mismatch_plural, [
'@count' => $this->mismatch,
]);
\Drupal::messenger()
->addWarning($message);
}
}
public function execute($connection = NULL) {
$role = $this->configuration['role'];
$role_storage = \Drupal::service('entity_type.manager')
->getStorage('redhen_connection_role');
$roles = array_keys($role_storage
->loadByProperties([
'connection_type' => $connection
->getType(),
]));
if ($connection !== FALSE && $connection
->get('role')
->getString() != $role) {
if (in_array($role, $roles)) {
$connection->original = clone $connection;
$connection
->set('role', $role);
$connection
->save();
}
else {
$this->mismatch++;
}
}
}
}