Commit e27542b4 authored by webchick's avatar webchick

Issue #2268801 by tim.plunkett: Update ConditionInterface to use recently added plugin interfaces.

parent 85f243e2
......@@ -7,9 +7,11 @@
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\Form\FormInterface;
use Drupal\Core\Plugin\PluginFormInterface;
/**
* An interface for condition plugins.
......@@ -29,7 +31,7 @@
* @see \Drupal\Core\TypedData\TypedDataManager::create()
* @see \Drupal\Core\Executable\ExecutableInterface
*/
interface ConditionInterface extends ExecutableInterface, FormInterface {
interface ConditionInterface extends ExecutableInterface, PluginFormInterface, ConfigurablePluginInterface, PluginInspectionInterface {
/**
* Determines whether condition result will be negated.
......
......@@ -17,22 +17,23 @@ abstract class ConditionPluginBase extends ExecutablePluginBase implements Condi
/**
* {@inheritdoc}
*/
public function getFormId() {
$definition = $this->getPluginDefinition();
return implode('_', array($definition['module'], $definition['id'], 'condition'));
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->setConfiguration($configuration);
}
/**
* Implements \Drupal\condition\Plugin\ConditionInterface::isNegated().
* {@inheritdoc}
*/
public function isNegated() {
return !empty($this->configuration['negate']);
}
/**
* Implements \Drupal\Core\Form\FormInterface::buildForm().
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
public function buildConfigurationForm(array $form, array &$form_state) {
$form['negate'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Negate the condition.'),
......@@ -42,22 +43,54 @@ public function buildForm(array $form, array &$form_state) {
}
/**
* Implements \Drupal\Core\Form\FormInterface::validateForm().
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {}
public function validateConfigurationForm(array &$form, array &$form_state) {
}
/**
* Implements \Drupal\Core\Form\FormInterface::submitForm().
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
public function submitConfigurationForm(array &$form, array &$form_state) {
$this->configuration['negate'] = $form_state['values']['negate'];
}
/**
* Implements \Drupal\Core\Executable\ExecutablePluginBase::execute().
* {@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();
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return array();
}
}
......@@ -28,8 +28,8 @@ class Language extends ConditionPluginBase {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$form = parent::buildForm($form, $form_state);
public function buildConfigurationForm(array $form, array &$form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
if (\Drupal::languageManager()->isMultilingual()) {
// Fetch languages.
$languages = language_list(Lang::STATE_ALL);
......@@ -57,9 +57,9 @@ public function buildForm(array $form, array &$form_state) {
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
public function submitConfigurationForm(array &$form, array &$form_state) {
$this->configuration['langcodes'] = array_filter($form_state['values']['langcodes']);
parent::submitForm($form, $form_state);
parent::submitConfigurationForm($form, $form_state);
}
/**
......
......@@ -28,10 +28,10 @@
class NodeType extends ConditionPluginBase {
/**
* Implements \Drupal\Core\Form\FormInterface::buildForm().
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$form = parent::buildForm($form, $form_state);
public function buildConfigurationForm(array $form, array &$form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$options = array();
foreach (node_type_get_types() as $type) {
$options[$type->type] = $type->name;
......@@ -47,9 +47,9 @@ public function buildForm(array $form, array &$form_state) {
}
/**
* Implements \Drupal\Core\Form\FormInterface::validateForm().
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
public function validateConfigurationForm(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', $form_state, t('You have chosen an invalid node bundle, please check your selection and try again.'));
......@@ -58,15 +58,15 @@ public function validateForm(array &$form, array &$form_state) {
}
/**
* Implements \Drupal\Core\Form\FormInterface::submitForm().
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
public function submitConfigurationForm(array &$form, array &$form_state) {
$this->configuration['bundles'] = $form_state['values']['bundles'];
parent::submitForm($form, $form_state);
parent::submitConfigurationForm($form, $form_state);
}
/**
* Implements \Drupal\Core\Executable\ExecutableInterface::summary().
* {@inheritdoc}
*/
public function summary() {
if (count($this->configuration['bundles']) > 1) {
......@@ -80,7 +80,7 @@ public function summary() {
}
/**
* Implements \Drupal\condition\ConditionInterface::evaluate().
* {@inheritdoc}
*/
public function evaluate() {
$node = $this->getContextValue('node');
......
......@@ -41,7 +41,7 @@ public function __construct() {
* Implements \Drupal\Core\Form\FormInterface::buildForm().
*/
public function buildForm(array $form, array &$form_state) {
$form = $this->condition->buildForm($form, $form_state);
$form = $this->condition->buildConfigurationForm($form, $form_state);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
......@@ -53,14 +53,14 @@ public function buildForm(array $form, array &$form_state) {
* Implements \Drupal\Core\Form\FormInterface::validateForm().
*/
public function validateForm(array &$form, array &$form_state) {
$this->condition->validateForm($form, $form_state);
$this->condition->validateConfigurationForm($form, $form_state);
}
/**
* Implements \Drupal\Core\Form\FormInterface::submitForm().
*/
public function submitForm(array &$form, array &$form_state) {
$this->condition->submitForm($form, $form_state);
$this->condition->submitConfigurationForm($form, $form_state);
$config = $this->condition->getConfig();
$bundles = implode(' and ', $config['bundles']);
drupal_set_message(t('The bundles are @bundles', array('@bundles' => $bundles)));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment