diff --git a/core/lib/Drupal/Component/Plugin/Context/Context.php b/core/lib/Drupal/Component/Plugin/Context/Context.php index 9f4396b..b117a72 100644 --- a/core/lib/Drupal/Component/Plugin/Context/Context.php +++ b/core/lib/Drupal/Component/Plugin/Context/Context.php @@ -8,6 +8,8 @@ namespace Drupal\Component\Plugin\Context; use Drupal\Component\Plugin\Exception\ContextException; +use Symfony\Component\Validator\Constraints\Type; +use Symfony\Component\Validator\Validation; /** * A generic context class for wrapping data a plugin needs to operate. @@ -39,7 +41,6 @@ public function __construct(array $context_definition) { * Implements \Drupal\Component\Plugin\Context\ContextInterface::setContextValue(). */ public function setContextValue($value) { - $value = $this->validate($value); $this->contextValue = $value; } @@ -65,22 +66,22 @@ public function getContextDefinition() { } /** - * Implements \Drupal\Component\Plugin\Context\ContextInterface::validate(). - * - * The default validation method only supports instance of checks between the - * contextDefintion and the contextValue. Other formats of context - * definitions can be supported through a subclass. + * Implements \Drupal\Component\Plugin\Context\ContextInterface::getConstraints(). */ - public function validate($value) { - // Check to make sure we have a class name, and that the passed context is - // an instance of that class name. - if (!empty($this->contextDefinition['class'])) { - if ($value instanceof $this->contextDefinition['class']) { - return $value; - } - throw new ContextException("The context passed was not an instance of {$this->contextDefinition['class']}."); + public function getConstraints() { + if (empty($this->contextDefinition['class'])) { + throw new ContextException("An error was encountered while trying to validate the context."); } - throw new ContextException("An error was encountered while trying to validate the context."); + return array(new Type($this->contextDefinition['class'])); + } + + /** + * Implements \Drupal\Component\Plugin\Context\ContextInterface::validate(). + */ + public function validate() { + $validator = Validation::createValidatorBuilder() + ->getValidator(); + return $validator->validateValue($this->getContextValue(), $this->getConstraints()); } } diff --git a/core/lib/Drupal/Component/Plugin/Context/ContextInterface.php b/core/lib/Drupal/Component/Plugin/Context/ContextInterface.php index 5c4373d..512605a 100644 --- a/core/lib/Drupal/Component/Plugin/Context/ContextInterface.php +++ b/core/lib/Drupal/Component/Plugin/Context/ContextInterface.php @@ -34,35 +34,38 @@ public function getContextValue(); /** * Sets the definition that the context must conform to. * - * @param mixed $contextDefinition + * @param array $contextDefinition * A defining characteristic representation of the context against which - * that context can be validated. This is typically a class name, but could - * be extended to support other validation notation. + * that context can be validated. This is typically an array having a + * class name set under the 'class' key, but it could be extended to support + * other notations. */ public function setContextDefinition(array $contextDefinition); /** * Gets the provided definition that the context must conform to. * - * @return mixed + * @return array * The defining characteristic representation of the context. */ public function getContextDefinition(); /** - * Validate the provided context value against the provided definition. - * - * @param mixed $value - * The context value that should be validated against the context - * definition. + * Gets a list of validation constraints. * - * @return mixed - * Returns the context value passed to it. If it fails validation, an - * exception will be thrown. + * @return array + * Array of constraints, each being an instance of + * \Symfony\Component\Validator\Constraint. + */ + public function getConstraints(); + + /** + * Validates the set context value. * - * @throws \Drupal\Component\Plugin\Exception\ContextException - * If validation fails. + * @return \Symfony\Component\Validator\ConstraintViolationListInterface + * A list of constraint violations. If the list is empty, validation + * succeeded. */ - public function validate($value); + public function validate(); } diff --git a/core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php b/core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php index a628f99..c6ffc3d 100644 --- a/core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php +++ b/core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php @@ -9,6 +9,7 @@ use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Component\Plugin\Context\Context; +use Symfony\Component\Validator\ConstraintViolationList; /** * Base class for plugins that are context aware. @@ -81,11 +82,12 @@ public function getContext($key) { * Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextValues(). */ public function getContextValues() { - $contexts = array(); - foreach ($this->getContexts() as $key => $context) { - $contexts[$key] = $context->getContextValue(); + $definitions = (array) $this->getContextDefinitions(); + $values = array(); + foreach ($definitions as $key => $definition) { + $values[$key] = isset($this->context[$key]) ? $this->context[$key]->getContextValue() : NULL; } - return $contexts; + return $values; } /** @@ -103,7 +105,32 @@ public function setContextValue($key, $value) { $this->context[$key] = new Context($context_definition); $this->context[$key]->setContextValue($value); + // Verify the provided value validates. + $violations = $this->context[$key]->validate(); + if (count($violations) > 0) { + throw new PluginException("The provided context value does not pass validation."); + } return $this; } + /** + * Implements \Drupal\Core\Executable\ExecutableInterface::valdidate(). + */ + public function validate() { + $violations = new ConstraintViolationList(); + // @todo: Implement symfony validator API to let the validator traverse + // and set property paths accordingly. + + if ($definitions = $this->getContextDefinitions()) { + foreach ($definitions as $key => $definition) { + // Validate any set values. + if (isset($this->context[$key])) { + $violations->addAll($this->context[$key]->validate()); + } + // @todo: If no value is set, make sure any mapping is validated. + } + } + return $violations; + } + } diff --git a/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php b/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php index 68d8012..086a25f 100644 --- a/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php +++ b/core/lib/Drupal/Component/Plugin/ContextAwarePluginInterface.php @@ -29,7 +29,10 @@ public function getContextDefinitions(); * @param string $key * The name of the context in the plugin definition. * - * @return mixed + * @throws \Drupal\Component\Plugin\Exception\PluginException + * If the requested definition is not set. + * + * @return array * The definition against which the context value must validate. */ public function getContextDefinition($key); @@ -37,6 +40,9 @@ public function getContextDefinition($key); /** * Gets the defined contexts. * + * @throws \Drupal\Component\Plugin\Exception\PluginException + * If contexts are defined but not set. + * * @return array * The set context objects. */ @@ -49,6 +55,9 @@ public function getContexts(); * The name of the context in the plugin configuration. This string is * usually identical to the representative string in the plugin definition. * + * @throws \Drupal\Component\Plugin\Exception\PluginException + * If the requested context is not set. + * * @return \Drupal\Component\Plugin\Context\ContextInterface * The context object. */ @@ -58,7 +67,8 @@ public function getContext($key); * Gets the values for all defined contexts. * * @return array - * The set context object values. + * An array of set context values, keyed by context key. If a context is + * unset its value is returned as NULL. */ public function getContextValues(); @@ -69,6 +79,9 @@ public function getContextValues(); * The name of the context in the plugin configuration. This string is * usually identical to the representative string in the plugin definition. * + * @throws \Drupal\Component\Plugin\Exception\PluginException + * If the requested context is not set. + * * @return mixed * The currently set context value. */ @@ -83,9 +96,21 @@ public function getContextValue($key); * The variable to set the context to. This should validate against the * provided context definition. * + * @throws \Drupal\Component\Plugin\Exception\PluginException + * If the value does not pass validation. + * * @return \Drupal\Component\Plugin\ContextAwarePluginInterface. * A context aware plugin object for chaining. */ public function setContextValue($key, $value); + /** + * Validates the set values for the defined contexts. + * + * @return \Symfony\Component\Validator\ConstraintViolationListInterface + * A list of constraint violations. If the list is empty, validation + * succeeded. + */ + public function validate(); + } diff --git a/core/lib/Drupal/Core/Condition/ConditionInterface.php b/core/lib/Drupal/Core/Condition/ConditionInterface.php new file mode 100644 index 0000000..82c220a --- /dev/null +++ b/core/lib/Drupal/Core/Condition/ConditionInterface.php @@ -0,0 +1,32 @@ +discovery = new AnnotatedClassDiscovery('Core', 'Condition'); + $this->discovery = new DerivativeDiscoveryDecorator($this->discovery); + $this->discovery = new AlterDecorator($this->discovery, 'condition_info'); + $this->discovery = new CacheDecorator($this->discovery, 'condition:' . language(LANGUAGE_TYPE_INTERFACE)->langcode); + + $this->factory = new DefaultFactory($this); + } + + /** + * Override of Drupal\Component\Plugin\PluginManagerBase::createInstance(). + */ + public function createInstance($plugin_id, array $configuration = array()) { + $plugin = $this->factory->createInstance($plugin_id, $configuration); + return $plugin->setExecutableManager($this); + } + + /** + * Implements Drupal\Core\Executable\ExecutableManagerInterface::execute(). + */ + public function execute(ExecutableInterface $condition) { + $result = $condition->evaluate(); + return $condition->isNegated() ? !$result : $result; + } + +} diff --git a/core/lib/Drupal/Core/Condition/ConditionPluginBase.php b/core/lib/Drupal/Core/Condition/ConditionPluginBase.php new file mode 100644 index 0000000..043055b --- /dev/null +++ b/core/lib/Drupal/Core/Condition/ConditionPluginBase.php @@ -0,0 +1,58 @@ +getDefinition(); + return implode('_', array($definition['module'], $definition['id'], 'condition')); + } + + /** + * Implements \Drupal\condition\Plugin\ConditionInterface::isNegated(). + */ + public function isNegated() { + return !empty($this->configuration['negate']); + } + + /** + * Implements \Drupal\Core\Form\FormInterface::buildForm(). + */ + public function buildForm(array $form, array &$form_state) { + $form['negate'] = array( + '#type' => 'checkbox', + '#title' => t('Negate the condition.'), + '#default_value' => isset($this->configuration['negate']) ? $this->configuration['negate'] : FALSE, + ); + return $form; + } + + /** + * Implements \Drupal\Core\Form\FormInterface::submitForm(). + */ + public function submitForm(array &$form, array &$form_state) { + $this->configuration['negate'] = $form_state['values']['negate']; + } + + /** + * Implements \Drupal\Core\Executable\ExecutablePluginBase::execute(). + */ + public function execute() { + return $this->executableManager->execute($this); + } + +} diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index 8714fac..a91106b 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -270,7 +270,7 @@ public function build(ContainerBuilder $container) { $container->register('flood', 'Drupal\Core\Flood\DatabaseBackend') ->addArgument(new Reference('database')); - $container->register('plugin.manager.processor', 'Drupal\Core\Processor\ProcessorManager'); + $container->register('plugin.manager.condition', 'Drupal\Core\Condition\ConditionManager'); $container->addCompilerPass(new RegisterMatchersPass()); $container->addCompilerPass(new RegisterRouteFiltersPass()); diff --git a/core/lib/Drupal/Core/Executable/ExecutableInterface.php b/core/lib/Drupal/Core/Executable/ExecutableInterface.php index a952ca6..efb9b86 100644 --- a/core/lib/Drupal/Core/Executable/ExecutableInterface.php +++ b/core/lib/Drupal/Core/Executable/ExecutableInterface.php @@ -9,30 +9,29 @@ use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Processor\ProcessorManager; +use Drupal\Core\Form\FormInterface; /** * An interface for executable and processable plugins. */ -interface ExecutableInterface extends PluginInspectionInterface { +interface ExecutableInterface extends PluginInspectionInterface, FormInterface { /** - * Sets the processor plugin manager. - */ - public function setProcessorManager(ProcessorManager $processorManager); - - /** - * Sets the processor plugin. + * Executes the action. */ - public function setProcessor($plugin_id, array $configuration); + public function execute(); /** - * Gets an array of processors. + * Provides a human readable summary of the executable's configuration. */ - public function getProcessors(); + public function summary(); /** - * Executes the action. + * Sets the manager class for use in certain proxy methods. + * + * @param \Drupal\Core\Condition\ConditionManager $executableManager + * The executable manager. */ - public function execute(); + public function setExecutableManager(ExecutableManagerInterface $executableManager); } diff --git a/core/lib/Drupal/Core/Executable/ExecutableManagerInterface.php b/core/lib/Drupal/Core/Executable/ExecutableManagerInterface.php new file mode 100644 index 0000000..c715f7d --- /dev/null +++ b/core/lib/Drupal/Core/Executable/ExecutableManagerInterface.php @@ -0,0 +1,32 @@ +processorManager = $processorManager; - return $this; + public function __construct(array $configuration, $plugin_id, DiscoveryInterface $discovery) { + $context = array(); + if (isset($configuration['context'])) { + $context = $configuration['context']; + unset($configuration['context']); + } + parent::__construct($configuration, $plugin_id, $discovery); + foreach ($context as $key => $value) { + $context_definition = $this->getContextDefinition($key); + $this->context[$key] = new Context($context_definition); + $this->context[$key]->setContextValue($value); + } } /** - * Implements \Drupal\Core\Executable\ExecutableInterface::setProcessor(). + * Implements \Drupal\Core\Executable\ExecutableInterace::setExecutableManager(). */ - public function setProcessor($plugin_id, array $configuration) { - // @todo Do we want to check this before actually assigning? - $this->configuration['processors'][] = $this->processorManager->createInstance($plugin_id, $configuration); + public function setExecutableManager(ExecutableManagerInterface $executableManager) { + $this->executableManager = $executableManager; return $this; } - /** - * Implements \Drupal\Core\Executable\ExecutableInterface::getProcessor(). - */ - public function getProcessors() { - return $this->configuration['processors']; + public function getConfigDefinitions() { + $definition = $this->getDefinition(); + if (!empty($definition['configuration'])) { + return $definition['configuration']; + } + return array(); + } + + public function getConfigDefinition($key) { + $definition = $this->getDefinition(); + if (!empty($definition['configuration'][$key])) { + return $definition['configuration'][$key]; + } + return NULL; } /** @@ -72,6 +96,16 @@ public function getConfig() { * @see \Drupal\Component\Plugin\PluginBase::$configuration */ public function setConfig($key, $value) { + $definition = $this->getConfigDefinition($key); + if (!is_null($definition)) { + $typed_data = typed_data()->create($definition, $value); + $validator = Validation::createValidatorBuilder() + ->setTranslator(new DrupalTranslator()) + ->getValidator(); + if ($validator->validateValue($typed_data, $typed_data->getConstraints())->count() > 0) { + throw new PluginException("The provided configuration value does not pass validation."); + } + } $this->configuration[$key] = $value; return $this; } diff --git a/core/lib/Drupal/Core/Plugin/Context/Context.php b/core/lib/Drupal/Core/Plugin/Context/Context.php index 9367401..3eb195d 100644 --- a/core/lib/Drupal/Core/Plugin/Context/Context.php +++ b/core/lib/Drupal/Core/Plugin/Context/Context.php @@ -8,9 +8,11 @@ namespace Drupal\Core\Plugin\Context; use Drupal\Component\Plugin\Context\Context as ComponentContext; -use Drupal\Component\Plugin\Exception\ContextException; -use Drupal\Core\TypedData\TypedDataManager; +use Drupal\Core\TypedData\ComplexDataInterface; +use Drupal\Core\TypedData\ListInterface; use Drupal\Core\TypedData\TypedDataInterface; +use Drupal\Core\Validation\DrupalTranslator; +use Symfony\Component\Validator\Validation; /** * A Drupal specific context wrapper class. @@ -26,19 +28,32 @@ class Context extends ComponentContext { */ public function getContextValue() { $typed_value = parent::getContextValue(); - // If the data is of a primitive type, directly return the plain value. - // That way, e.g. a string will be return as plain PHP string. - if ($typed_value instanceof \Drupal\Core\TypedData\TypedDataInterface) { - $type_definition = typed_data()->getDefinition($typed_value->getType()); - // @todo We won't need the getType == entity check once #1868004 lands. - if (!empty($type_definition['primitive type']) || $typed_value->getType() == 'entity') { - return $typed_value->getValue(); - } + // If the typed data is complex, pass it on as typed data. Else pass on its + // plain value, such that e.g. a string will be directly returned as PHP + // string. + $is_complex = $typed_value instanceof ComplexDataInterface; + if (!$is_complex && $typed_value instanceof ListInterface) { + $is_complex = $typed_value[0] instanceof ComplexDataInterface; + } + // @todo We won't need the getType == entity check once #1868004 lands. + if ($typed_value instanceof TypedDataInterface && (!$is_complex || $typed_value->getType() == 'entity')) { + return $typed_value->getValue(); } return $typed_value; } /** + * Implements \Drupal\Component\Plugin\Context\ContextInterface::setContextValue(). + */ + public function setContextValue($value) { + // Make sure the value set is a typed data object. + if (!empty($this->contextDefinition['type']) && !$value instanceof TypedDataInterface) { + $value = typed_data()->create($this->contextDefinition, $value); + } + parent::setContextValue($value); + } + + /** * Gets the context value as typed data object. * * parent::getContextValue() does not do all the processing required to @@ -55,21 +70,31 @@ public function getTypedContext() { } /** - * Override for \Drupal\Component\Plugin\Context\Context::validate(). + * Overrides \Drupal\Component\Plugin\Context\Context::getConstraints(). */ - public function validate($value) { + public function getConstraints() { if (!empty($this->contextDefinition['type'])) { - $typed_data_manager = new TypedDataManager(); - $typed_data = $typed_data_manager->create($this->contextDefinition, $value); - // If we do have a typed data definition, validate it and return the - // typed data instance instead. - $violations = $typed_data->validate(); - if (count($violations) == 0) { - return $typed_data; - } - throw new ContextException("The context passed could not be validated through typed data."); + // If we do have typed data, leverage it for getting constraints. + return $this->getTypedContext()->getConstraints(); } - return parent::validate($value); + return parent::getConstraints(); } + /** + * Overrides \Drupal\Component\Plugin\Context\Context::getConstraints(). + */ + public function validate() { + $validator = Validation::createValidatorBuilder() + ->setTranslator(new DrupalTranslator()) + ->getValidator(); + + // @todo We won't need to special case "entity" here once #1868004 lands. + if (!empty($this->contextDefinition['type']) && $this->contextDefinition['type'] == 'entity') { + $value = $this->getTypedContext(); + } + else { + $value = $this->getContextValue(); + } + return $validator->validateValue($value, $this->getConstraints()); + } } diff --git a/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php index 028befd..e4daeb1 100644 --- a/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php +++ b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Plugin; use Drupal\Component\Plugin\ContextAwarePluginBase as PluginBase; +use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Core\Plugin\Context\Context; /** @@ -27,6 +28,10 @@ public function setContextValue($key, $value) { $this->context[$key] = new Context($context_definition); $this->context[$key]->setContextValue($value); + // Verify the provided value validates. + if ($this->context[$key]->validate()->count() > 0) { + throw new PluginException("The provided context value does not pass validation."); + } return $this; } diff --git a/core/lib/Drupal/Core/Plugin/Validation/Constraint/EntityTypeConstraintValidator.php b/core/lib/Drupal/Core/Plugin/Validation/Constraint/EntityTypeConstraintValidator.php index 21b2ac2..1e1aba4 100644 --- a/core/lib/Drupal/Core/Plugin/Validation/Constraint/EntityTypeConstraintValidator.php +++ b/core/lib/Drupal/Core/Plugin/Validation/Constraint/EntityTypeConstraintValidator.php @@ -22,7 +22,7 @@ public function validate($typed_data, Constraint $constraint) { $entity = isset($typed_data) ? $typed_data->getValue() : FALSE; if (!empty($entity) && $entity->entityType() != $constraint->type) { - $this->context->addViolation($constraint->message, array('%type', $constraint->type)); + $this->context->addViolation($constraint->message, array('%type' => $constraint->type)); } } } diff --git a/core/lib/Drupal/Core/Processor/ProcessorInterface.php b/core/lib/Drupal/Core/Processor/ProcessorInterface.php deleted file mode 100644 index a3e15e7..0000000 --- a/core/lib/Drupal/Core/Processor/ProcessorInterface.php +++ /dev/null @@ -1,15 +0,0 @@ -discovery = new AnnotatedClassDiscovery('Core', 'Processor'); - $this->discovery = new AlterDecorator($this->discovery, 'processor'); - $this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition')); - $this->discovery = new CacheDecorator($this->discovery, 'processor:' . language(LANGUAGE_TYPE_INTERFACE)->langcode); - - $this->factory = new DefaultFactory($this); - } - -} - diff --git a/core/modules/condition/condition.info b/core/modules/condition/condition.info deleted file mode 100644 index 52d7d2a..0000000 --- a/core/modules/condition/condition.info +++ /dev/null @@ -1,6 +0,0 @@ -name = Conditions -description = Perform evaluations of various information to yield TRUE/FALSE statements. -package = Core -version = VERSION -core = 8.x -configure = admin/config/system/conditions diff --git a/core/modules/condition/condition.module b/core/modules/condition/condition.module deleted file mode 100644 index d6ed5b2..0000000 --- a/core/modules/condition/condition.module +++ /dev/null @@ -1,20 +0,0 @@ -get('plugin.manager.condition')->createInstance($plugin_id, $options); -} diff --git a/core/modules/condition/lib/Drupal/condition/ConditionBundle.php b/core/modules/condition/lib/Drupal/condition/ConditionBundle.php deleted file mode 100644 index b41a3db..0000000 --- a/core/modules/condition/lib/Drupal/condition/ConditionBundle.php +++ /dev/null @@ -1,28 +0,0 @@ -register('plugin.manager.condition', 'Drupal\condition\Plugin\Type\ConditionManager')->addArgument(new Reference('plugin.manager.processor')); - } - -} diff --git a/core/modules/condition/lib/Drupal/condition/Plugin/ConditionInterface.php b/core/modules/condition/lib/Drupal/condition/Plugin/ConditionInterface.php deleted file mode 100644 index 1dd7e5f..0000000 --- a/core/modules/condition/lib/Drupal/condition/Plugin/ConditionInterface.php +++ /dev/null @@ -1,74 +0,0 @@ - 'checkbox', - '#title' => t('Negate the condition.'), - '#default_value' => isset($this->configuration['negate']) ? $this->configuration['negate'] : FALSE, - ); - return $form; - } - - /** - * Implements Drupal\condition\ConditionInterface::submit(). - */ - public function submit($form, &$form_state) { - $this->configuration['negate'] = $form_state['values']['negate']; - } - - /** - * Implements ExecutablePluginBase::execute(). - */ - public function execute() { - if (!empty($this->configuration['negate'])) { - return !$this->evaluate(); - } - return $this->evaluate(); - } - -} diff --git a/core/modules/condition/lib/Drupal/condition/Plugin/Type/ConditionManager.php b/core/modules/condition/lib/Drupal/condition/Plugin/Type/ConditionManager.php deleted file mode 100644 index b0da81f..0000000 --- a/core/modules/condition/lib/Drupal/condition/Plugin/Type/ConditionManager.php +++ /dev/null @@ -1,49 +0,0 @@ -processorManager = $process_manager; - $this->discovery = new AnnotatedClassDiscovery('Core', 'Condition'); - $this->discovery = new AlterDecorator($this->discovery, 'condition_info'); - $this->discovery = new CacheDecorator($this->discovery, 'condition:' . language(LANGUAGE_TYPE_INTERFACE)->langcode); - - $this->factory = new DefaultFactory($this); - } - - /** - * Override of Drupal\Component\Plugin\PluginManagerBase::createInstance(). - */ - public function createInstance($plugin_id, array $configuration = array()) { - $plugin = $this->factory->createInstance($plugin_id, $configuration); - return $plugin->setProcessorManager($this->processorManager); - } - -} diff --git a/core/modules/node/lib/Drupal/node/Plugin/Core/Condition/NodeType.php b/core/modules/node/lib/Drupal/node/Plugin/Core/Condition/NodeType.php index 828c524..e7deca2 100644 --- a/core/modules/node/lib/Drupal/node/Plugin/Core/Condition/NodeType.php +++ b/core/modules/node/lib/Drupal/node/Plugin/Core/Condition/NodeType.php @@ -7,7 +7,7 @@ namespace Drupal\node\Plugin\Core\Condition; -use Drupal\condition\Plugin\ConditionPluginBase; +use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Annotation\Plugin; use Drupal\Core\Annotation\Translation; @@ -32,10 +32,10 @@ class NodeType extends ConditionPluginBase { /** - * Implements \Drupal\condition\Plugin\ConditionInterface::form(). + * Implements \Drupal\Core\Form\FormInterface::buildForm(). */ - public function form($form, &$form_state) { - $form = parent::form($form, $form_state); + public function buildForm(array $form, array &$form_state) { + $form = parent::buildForm($form, $form_state); $options = array(); foreach (node_type_get_types() as $type) { $options[$type->type] = $type->name; @@ -52,7 +52,7 @@ public function form($form, &$form_state) { /** * Implements \Drupal\condition\ConditionInterface::validate(). */ - public function validate($form, &$form_state) { + public function validateForm(array &$form, array &$form_state) { foreach ($form_state['values']['bundles'] as $bundle) { if (!in_array($bundle, array_keys(node_type_get_types()))) { form_set_error('bundles', t('You have chosen an invalid node bundle, please check your selection and try again.')); @@ -63,9 +63,9 @@ public function validate($form, &$form_state) { /** * Implements \Drupal\condition\ConditionInterface::submit(). */ - public function submit($form, &$form_state) { + public function submitForm(array &$form, array &$form_state) { $this->configuration['bundles'] = $form_state['values']['bundles']; - parent::submit($form, $form_state); + parent::submitForm($form, $form_state); } /** diff --git a/core/modules/node/lib/Drupal/node/Tests/Condition/NodeConditionTest.php b/core/modules/node/lib/Drupal/node/Tests/Condition/NodeConditionTest.php index e6592b7..addc5c0 100644 --- a/core/modules/node/lib/Drupal/node/Tests/Condition/NodeConditionTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/Condition/NodeConditionTest.php @@ -8,15 +8,14 @@ namespace Drupal\node\Tests\Condition; use Drupal\simpletest\DrupalUnitTestBase; -use Drupal\condition\Plugin\Type\ConditionManager; -use Drupal\Core\Processor\ProcessorManager; +use Drupal\Core\Condition\ConditionManager; /** * Tests the node conditions. */ class NodeConditionTest extends DrupalUnitTestBase { - public static $modules = array('system', 'node', 'field', 'condition'); + public static $modules = array('system', 'node', 'field'); public static function getInfo() { return array( @@ -39,8 +38,7 @@ protected function setUp() { * Tests conditions. */ function testConditions() { - $processor = new ProcessorManager(); - $manager = new ConditionManager($processor); + $manager = new ConditionManager(); // Get some nodes of various types to check against. $page = entity_create('node', array('type' => 'page', 'title' => $this->randomName())); @@ -82,5 +80,9 @@ function testConditions() { // Check a greater than 2 bundles summary scenario. $condition->setConfig('bundles', array('page', 'article', 'test')); $this->assertEqual('The node bundle is page, article or test', $condition->summary()); + + // Test Constructor injection. + $condition = $manager->createInstance('node_type', array('bundles' => array('article'), 'context' => array('node' => $article))); + $this->assertTrue($condition->execute(), 'Constructor injection of context and configuration working as anticipated.'); } }