ChangeSingleOgMembershipRoleBase.php in Organic groups 8
File
src/Plugin/Action/ChangeSingleOgMembershipRoleBase.php
View source
<?php
declare (strict_types=1);
namespace Drupal\og\Plugin\Action;
use Drupal\Core\Action\ConfigurableActionBase;
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\og\Entity\OgRole;
use Drupal\og\OgAccessInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ChangeSingleOgMembershipRoleBase extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
use DependencyTrait;
protected $entityType;
protected $ogAccess;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeInterface $entity_type, OgAccessInterface $og_access) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityType = $entity_type;
$this->ogAccess = $og_access;
}
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('og_role'), $container
->get('og.access'));
}
public function defaultConfiguration() {
$role_names = $this
->getOgRoleLabels();
reset($role_names);
return [
'role_name' => key($role_names),
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$options = $this
->getOgRoleLabels();
reset($options);
$form['role_name'] = [
'#type' => 'radios',
'#title' => $this
->t('Role'),
'#options' => $options,
'#default_value' => $this->configuration['role_name'],
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['role_name'] = $form_state
->getValue('role_name');
}
public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE) {
$access = $this->ogAccess
->userAccess($object
->getGroup(), 'manage members', $account);
return $return_as_object ? $access : $access
->isAllowed();
}
protected function getOgRoleLabels() {
$roles = OgRole::loadMultiple();
$role_names = [];
foreach ($roles as $role) {
if (!$role
->isRequired()) {
$role_names[$role
->getName()] = $role
->label();
}
}
return $role_names;
}
}