abstract class AccessControlHierarchyBase in Workbench Access 8
Defines a base hierarchy class that others may extend.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\workbench_access\AccessControlHierarchyBase implements ContainerFactoryPluginInterface, AccessControlHierarchyInterface uses PluginWithFormsTrait, StringTranslationTrait
Expanded class hierarchy of AccessControlHierarchyBase
4 files declare their use of AccessControlHierarchyBase
- DerivedAccessControlHierarchy.php in tests/
modules/ workbench_access_test/ src/ Plugin/ AccessControlHierarchy/ DerivedAccessControlHierarchy.php - FilterAccess.php in tests/
modules/ workbench_access_filter_test/ src/ Plugin/ AccessControlHierarchy/ FilterAccess.php - Menu.php in src/
Plugin/ AccessControlHierarchy/ Menu.php - Taxonomy.php in src/
Plugin/ AccessControlHierarchy/ Taxonomy.php
File
- src/
AccessControlHierarchyBase.php, line 24
Namespace
Drupal\workbench_accessView source
abstract class AccessControlHierarchyBase extends PluginBase implements AccessControlHierarchyInterface, ContainerFactoryPluginInterface {
use PluginWithFormsTrait;
use StringTranslationTrait;
/**
* A configuration factory object to store configuration.
*
* @var ConfigFactory
*/
protected $configFactory;
/**
* The access tree array.
*
* @var array
*/
protected $tree;
/**
* User section storage.
*
* @var \Drupal\workbench_access\UserSectionStorageInterface
*/
protected $userSectionStorage;
/**
* Config for module.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new AccessControlHierarchyBase object.
*
* @param array $configuration
* Configuration.
* @param string $plugin_id
* Plugin ID.
* @param mixed $plugin_definition
* Plugin definition.
* @param \Drupal\workbench_access\UserSectionStorageInterface $user_section_storage
* User section storage.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, UserSectionStorageInterface $user_section_storage, ConfigFactoryInterface $configFactory, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->config = $configFactory
->get('workbench_access.settings');
$this->userSectionStorage = $user_section_storage;
$this->entityTypeManager = $entityTypeManager;
$this
->setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('workbench_access.user_section_storage'), $container
->get('config.factory'), $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = $configuration + $this
->defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function id() {
return $this->pluginDefinition['id'];
}
/**
* {@inheritdoc}
*/
public function label() {
return $this->pluginDefinition['label'];
}
/**
* Gets the entity type id controlled by the scheme.
*
* Note that this is an API change and not in the Interface.
* @TODO: add to interface in version 2.0.
*
* @return string
* The entity type id of entities controlled by the scheme.
*/
public function entityType() {
return $this->pluginDefinition['entity'];
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return [];
}
/**
* {@inheritdoc}
*/
public function getTree() {
return [];
}
/**
* {@inheritdoc}
*/
public function resetTree() {
unset($this->tree);
}
/**
* {@inheritdoc}
*/
public function load($id) {
// This cache is specific to the object being called.
// e.g. Drupal\workbench_access\Plugin\AccessControlHierarchy\Menu
if (!isset($this->tree)) {
$this->tree = $this
->getTree();
}
foreach ($this->tree as $parent => $data) {
if (isset($data[$id])) {
return $data[$id];
}
}
}
/**
* {@inheritdoc}
*/
public function checkEntityAccess(AccessSchemeInterface $scheme, EntityInterface $entity, $op, AccountInterface $account) {
if (!$this
->applies($entity
->getEntityTypeId(), $entity
->bundle())) {
return AccessResult::neutral();
}
// Discover the field and check status.
$entity_sections = $this
->getEntityValues($entity);
// If no value is set on the entity, ignore.
// @TODO: Is this the correct logic? It is helpful for new installs.
$deny_on_empty = $this->config
->get('deny_on_empty');
if (!$deny_on_empty && empty($entity_sections)) {
return AccessResult::neutral();
}
$user_sections = $this->userSectionStorage
->getUserSections($scheme, $account);
if (empty($user_sections)) {
return AccessResult::forbidden();
}
// Check the tree status of the $entity against the $user.
// Return neutral if in tree, forbidden if not.
if (WorkbenchAccessManager::checkTree($scheme, $entity_sections, $user_sections)) {
return AccessResult::neutral();
}
return AccessResult::forbidden();
}
/**
* {@inheritdoc}
*/
public function disallowedOptions($field) {
$options = [];
if (isset($field['widget']['#default_value']) && isset($field['widget']['#options'])) {
// Default value may be an array or a string.
if (is_array($field['widget']['#default_value'])) {
$default_value_array = array_flip($field['widget']['#default_value']);
}
else {
$default_value_array = [
$field['widget']['#default_value'] => $field['widget']['#default_value'],
];
}
$options = array_diff_key($default_value_array, $field['widget']['#options']);
}
return array_keys($options);
}
/**
* @inheritdoc
*/
public function getApplicableFields($entity_type, $bundle) {
// Extending classes are expected to provide their own implementation.
return [];
}
/**
* {@inheritdoc}
*/
public static function submitEntity(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\workbench_access\Entity\AccessSchemeInterface $access_scheme */
foreach (\Drupal::entityTypeManager()
->getStorage('access_scheme')
->loadMultiple() as $access_scheme) {
$scheme = $access_scheme
->getAccessScheme();
$hidden_values = $form_state
->getValue('workbench_access_disallowed');
if (!empty($hidden_values)) {
$entity = $form_state
->getFormObject()
->getEntity();
$scheme
->massageFormValues($entity, $form_state, $hidden_values);
}
}
}
/**
* {@inheritdoc}
*/
public function getViewsJoin($entity_type, $key, $alias = NULL) {
return [];
}
/**
* {@inheritdoc}
*/
public function addWhere(Section $filter, array $values) {
// The JOIN data tells us if we have multiple tables to deal with.
$join_data = $this
->getViewsJoin($filter
->getEntityType(), $filter->realField);
if (count($join_data) == 1) {
$filter->query
->addWhere($filter->options['group'], "{$filter->tableAlias}.{$filter->realField}", array_values($values), $filter->operator);
}
else {
$or = new Condition('OR');
foreach ($join_data as $field => $data) {
$alias = $data['table_alias'] . '.' . $data['real_field'];
$or
->condition($alias, array_values($values), $filter->operator);
}
$filter->query
->addWhere($filter->options['group'], $or);
}
}
/**
* {@inheritdoc}
*/
public function viewsData(array &$data, AccessSchemeInterface $scheme) {
// Null op.
}
/**
* {@inheritdoc}
*/
public function massageFormValues(ContentEntityInterface $entity, FormStateInterface $form_state, array $hidden_values) {
// Null op.
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return [];
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
// Default implementation is empty.
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
// Default implementation is empty.
}
/**
* {@inheritdoc}
*/
public function onDependencyRemoval(array $dependencies) {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function alterForm(AccessSchemeInterface $scheme, array &$form, FormStateInterface &$form_state, ContentEntityInterface $entity) {
// Default implementation is empty.
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AccessControlHierarchyBase:: |
protected | property | Config for module. | |
AccessControlHierarchyBase:: |
protected | property | A configuration factory object to store configuration. | |
AccessControlHierarchyBase:: |
protected | property | Entity type manager. | |
AccessControlHierarchyBase:: |
protected | property | The access tree array. | |
AccessControlHierarchyBase:: |
protected | property | User section storage. | |
AccessControlHierarchyBase:: |
public | function |
Adds a where clause to a view when using a section filter. Overrides AccessControlHierarchyInterface:: |
|
AccessControlHierarchyBase:: |
public | function |
Alters the selection options provided for an access control field. Overrides AccessControlHierarchyInterface:: |
2 |
AccessControlHierarchyBase:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
2 |
AccessControlHierarchyBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
2 |
AccessControlHierarchyBase:: |
public | function |
Responds to request for node access. Overrides AccessControlHierarchyInterface:: |
|
AccessControlHierarchyBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
3 |
AccessControlHierarchyBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
2 |
AccessControlHierarchyBase:: |
public | function |
Gets any options that are set but cannot be changed by the editor. Overrides AccessControlHierarchyInterface:: |
1 |
AccessControlHierarchyBase:: |
public | function | Gets the entity type id controlled by the scheme. | |
AccessControlHierarchyBase:: |
public | function |
@inheritdoc Overrides AccessControlHierarchyInterface:: |
1 |
AccessControlHierarchyBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
AccessControlHierarchyBase:: |
public | function |
Gets the entire hierarchy tree. Overrides AccessControlHierarchyInterface:: |
3 |
AccessControlHierarchyBase:: |
public | function |
Returns information on how to join this section data to a base view table. Overrides AccessControlHierarchyInterface:: |
2 |
AccessControlHierarchyBase:: |
public | function |
Returns the id for a hierarchy. Overrides AccessControlHierarchyInterface:: |
|
AccessControlHierarchyBase:: |
public | function |
Returns the label for a hierarchy. Overrides AccessControlHierarchyInterface:: |
|
AccessControlHierarchyBase:: |
public | function |
Loads a hierarchy definition for a single item in the tree. Overrides AccessControlHierarchyInterface:: |
|
AccessControlHierarchyBase:: |
public | function |
Massage form values as appropriate during entity submit. Overrides AccessControlHierarchyInterface:: |
1 |
AccessControlHierarchyBase:: |
public | function |
Informs the plugin that a dependency of the scheme will be deleted. Overrides AccessControlHierarchyInterface:: |
2 |
AccessControlHierarchyBase:: |
public | function |
Resets the internal cache of the tree. Overrides AccessControlHierarchyInterface:: |
|
AccessControlHierarchyBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
AccessControlHierarchyBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
2 |
AccessControlHierarchyBase:: |
public static | function |
Responds to the submission of an entity form. Overrides AccessControlHierarchyInterface:: |
|
AccessControlHierarchyBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
1 |
AccessControlHierarchyBase:: |
public | function |
Adds views data for the plugin. Overrides AccessControlHierarchyInterface:: |
2 |
AccessControlHierarchyBase:: |
public | function |
Constructs a new AccessControlHierarchyBase object. Overrides PluginBase:: |
1 |
AccessControlHierarchyInterface:: |
public | function | Check if this access scheme applies to the given entity. | 4 |
AccessControlHierarchyInterface:: |
public | function | Retrieves the access control values from an entity. | 4 |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginWithFormsTrait:: |
public | function | ||
PluginWithFormsTrait:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |