Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,71 @@
<?php
/**
* @file
* Contains \Drupal\Core\Condition\Annotation\Condition.
*/
namespace Drupal\Core\Condition\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a condition plugin annotation object.
*
* Condition plugins provide generalized conditions for use in other
* operations, such as conditional block placement.
*
* Plugin Namespace: Plugin\Condition
*
* For a working example, see \Drupal\user\Plugin\Condition\UserRole.
*
* @see \Drupal\Core\Condition\ConditionManager
* @see \Drupal\Core\Condition\ConditionInterface
* @see \Drupal\Core\Condition\ConditionPluginBase
* @see block_api
*
* @ingroup plugin_api
*
* @Annotation
*/
class Condition extends Plugin {
/**
* The condition plugin ID.
*
* @var string
*/
public $id;
/**
* The human-readable name of the condition.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $label;
/**
* The name of the module providing the type.
*
* @var string
*/
public $module;
/**
* An array of contextual data.
*
* @var array
*/
public $condition = array();
/**
* The category under which the condition should listed in the UI.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $category;
}

View file

@ -0,0 +1,53 @@
<?php
/**
* @file
* Contains \Drupal\Core\Condition\ConditionAccessResolverTrait.
*/
namespace Drupal\Core\Condition;
use Drupal\Component\Plugin\Exception\ContextException;
/**
* Resolves a set of conditions.
*/
trait ConditionAccessResolverTrait {
/**
* Resolves the given conditions based on the condition logic ('and'/'or').
*
* @param \Drupal\Core\Condition\ConditionInterface[] $conditions
* A set of conditions.
* @param string $condition_logic
* The logic used to compute access, either 'and' or 'or'.
*
* @return bool
* Whether these conditions grant or deny access.
*/
protected function resolveConditions($conditions, $condition_logic) {
foreach ($conditions as $condition) {
try {
$pass = $condition->execute();
}
catch (ContextException $e) {
// If a condition is missing context, consider that a fail.
$pass = FALSE;
}
// If a condition fails and all conditions were needed, deny access.
if (!$pass && $condition_logic == 'and') {
return FALSE;
}
// If a condition passes and only one condition was needed, grant access.
elseif ($pass && $condition_logic == 'or') {
return TRUE;
}
}
// Return TRUE if logic was 'and', meaning all rules passed.
// Return FALSE if logic was 'or', meaning no rule passed.
return $condition_logic == 'and';
}
}

View file

@ -0,0 +1,80 @@
<?php
/**
* @file
* Contains \Drupal\Core\Condition\ConditionInterface.
*/
namespace Drupal\Core\Condition;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Executable\ExecutableInterface;
use Drupal\Core\Executable\ExecutableManagerInterface;
use Drupal\Core\Plugin\PluginFormInterface;
/**
* An interface for condition plugins.
*
* Condition plugins are context-aware and configurable. They support the
* following keys in their plugin definitions:
* - context: An array of context definitions, keyed by context name. Each
* context definition is a typed data definition describing the context. Check
* the typed data definition docs for details.
* - configuration: An array of configuration option definitions, keyed by
* option name. Each option definition is a typed data definition describing
* the configuration option. Check the typed data definition docs for details.
*
* @todo Replace the dependency on \Drupal\Core\Form\FormInterface with a new
* interface from https://www.drupal.org/node/2006248.
* @todo WARNING: The condition API is going to receive some additions before release.
* The following additions are likely to happen:
* - The way configuration is handled and configuration forms are built is
* likely to change in order for the plugin to be of use for Rules.
* - Conditions will receive a data processing API that allows for token
* replacements to happen outside of the plugin implementations,
* see https://www.drupal.org/node/2347023.
* - Conditions will have to implement access control for checking who is
* allowed to configure or perform the action at
* https://www.drupal.org/node/2172017.
*
* @see \Drupal\Core\TypedData\TypedDataManager::create()
* @see \Drupal\Core\Executable\ExecutableInterface
* @see \Drupal\Core\Condition\ConditionManager
* @see \Drupal\Core\Condition\Annotation\Condition
* @see \Drupal\Core\Condition\ConditionPluginBase
*
* @ingroup plugin_api
*/
interface ConditionInterface extends ExecutableInterface, PluginFormInterface, ConfigurablePluginInterface, PluginInspectionInterface {
/**
* Determines whether condition result will be negated.
*
* @return boolean
* Whether the condition result will be negated.
*/
public function isNegated();
/**
* Evaluates the condition and returns TRUE or FALSE accordingly.
*
* @return bool
* TRUE if the condition has been met, FALSE otherwise.
*/
public function evaluate();
/**
* Provides a human readable summary of the condition's configuration.
*/
public function summary();
/**
* Sets the executable manager class.
*
* @param \Drupal\Core\Executable\ExecutableManagerInterface $executableManager
* The executable manager.
*/
public function setExecutableManager(ExecutableManagerInterface $executableManager);
}

View file

@ -0,0 +1,75 @@
<?php
/**
* @file
* Contains \Drupal\Core\Condition\ConditionManager.
*/
namespace Drupal\Core\Condition;
use Drupal\Component\Plugin\CategorizingPluginManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Executable\ExecutableManagerInterface;
use Drupal\Core\Executable\ExecutableInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
use Drupal\Core\Plugin\Context\ContextAwarePluginManagerTrait;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* A plugin manager for condition plugins.
*
* @see \Drupal\Core\Condition\Annotation\Condition
* @see \Drupal\Core\Condition\ConditionInterface
* @see \Drupal\Core\Condition\ConditionPluginBase
*
* @ingroup plugin_api
*/
class ConditionManager extends DefaultPluginManager implements ExecutableManagerInterface, CategorizingPluginManagerInterface {
use CategorizingPluginManagerTrait;
use ContextAwarePluginManagerTrait;
/**
* Constructs a ConditionManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
$this->alterInfo('condition_info');
$this->setCacheBackend($cache_backend, 'condition_plugins');
parent::__construct('Plugin/Condition', $namespaces, $module_handler, 'Drupal\Core\Condition\ConditionInterface', 'Drupal\Core\Condition\Annotation\Condition');
}
/**
* Override of Drupal\Component\Plugin\PluginManagerBase::createInstance().
*/
public function createInstance($plugin_id, array $configuration = array()) {
$plugin = $this->getFactory()->createInstance($plugin_id, $configuration);
// If we receive any context values via config set it into the plugin.
if (!empty($configuration['context'])) {
foreach ($configuration['context'] as $name => $context) {
$plugin->setContextValue($name, $context);
}
}
return $plugin->setExecutableManager($this);
}
/**
* Implements Drupal\Core\Executable\ExecutableManagerInterface::execute().
*/
public function execute(ExecutableInterface $condition) {
$result = $condition->evaluate();
return $condition->isNegated() ? !$result : $result;
}
}

View file

@ -0,0 +1,126 @@
<?php
/**
* @file
* Contains \Drupal\Core\Condition\ConditionPluginBase.
*/
namespace Drupal\Core\Condition;
use Drupal\Core\Executable\ExecutableManagerInterface;
use Drupal\Core\Executable\ExecutablePluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
/**
* Provides a basis for fulfilling contexts for condition plugins.
*
* @see \Drupal\Core\Condition\Annotation\Condition
* @see \Drupal\Core\Condition\ConditionInterface
* @see \Drupal\Core\Condition\ConditionManager
*
* @ingroup plugin_api
*/
abstract class ConditionPluginBase extends ExecutablePluginBase implements ConditionInterface {
use ContextAwarePluginAssignmentTrait;
/**
* The condition manager to proxy execute calls through.
*
* @var \Drupal\Core\Executable\ExecutableInterface
*/
protected $executableManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public function isNegated() {
return !empty($this->configuration['negate']);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$contexts = $form_state->getTemporaryValue('gathered_contexts') ?: [];
$form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts);
$form['negate'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Negate the condition'),
'#default_value' => $this->configuration['negate'],
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['negate'] = $form_state->getValue('negate');
}
/**
* {@inheritdoc}
*/
public function execute() {
return $this->executableManager->execute($this);
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return array(
'id' => $this->getPluginId(),
) + $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = $configuration + $this->defaultConfiguration();
return $this;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array(
'negate' => FALSE,
);
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return array();
}
/**
* {@inheritdoc}
*/
public function setExecutableManager(ExecutableManagerInterface $executableManager) {
$this->executableManager = $executableManager;
return $this;
}
}

View file

@ -0,0 +1,82 @@
<?php
/**
* @file
* Contains \Drupal\Core\Condition\ConditionPluginCollection.
*/
namespace Drupal\Core\Condition;
use Drupal\Component\Plugin\Context\ContextInterface;
use Drupal\Core\Plugin\DefaultLazyPluginCollection;
/**
* Provides a collection of condition plugins.
*/
class ConditionPluginCollection extends DefaultLazyPluginCollection {
/**
* An array of collected contexts for conditions.
*
* @var \Drupal\Component\Plugin\Context\ContextInterface[]
*/
protected $conditionContexts = array();
/**
* {@inheritdoc}
*
* @return \Drupal\Core\Condition\ConditionInterface
*/
public function &get($instance_id) {
return parent::get($instance_id);
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
$configuration = parent::getConfiguration();
// Remove configuration if it matches the defaults.
foreach ($configuration as $instance_id => $instance_config) {
$default_config = array();
$default_config['id'] = $instance_id;
$default_config += $this->get($instance_id)->defaultConfiguration();
// In order to determine if a plugin is configured, we must compare it to
// its default configuration. The default configuration of a plugin does
// not contain context_mapping and it is not used when the plugin is not
// configured, so remove the context_mapping from the instance config to
// compare the remaining values.
unset($instance_config['context_mapping']);
if ($default_config === $instance_config) {
unset($configuration[$instance_id]);
}
}
return $configuration;
}
/**
* Sets the condition context for a given name.
*
* @param string $name
* The name of the context.
* @param \Drupal\Component\Plugin\Context\ContextInterface $context
* The context to add.
*
* @return $this
*/
public function addContext($name, ContextInterface $context) {
$this->conditionContexts[$name] = $context;
return $this;
}
/**
* Gets the values for all defined contexts.
*
* @return \Drupal\Component\Plugin\Context\ContextInterface[]
* An array of set contexts, keyed by context name.
*/
public function getConditionContexts() {
return $this->conditionContexts;
}
}