Issue by Chi, rpayanm, quietone, tobiasb, Ankit.Gupta, smustgrave, larowlan, borisson_, alexpott, ameymudras: Missing configuration schema for current_theme condition plugin

(cherry picked from commit c9e52a8c49)
merge-requests/3090/head
catch 2022-12-09 11:23:25 +00:00
parent c26201e48b
commit 2c0f59196c
5 changed files with 73 additions and 8 deletions
core/modules/system
config/schema
tests
src/Functional/Condition

View File

@ -350,3 +350,10 @@ condition.plugin.request_path:
mapping:
pages:
type: string
condition.plugin.current_theme:
type: condition.plugin
mapping:
theme:
type: string
label: Theme

View File

@ -0,0 +1,13 @@
# This setting is only used to check if configuration schema exists for these
# condition plugins.
visibility:
current_theme:
id: current_theme
theme: stable
negate: false
context_mapping: { }
request_path:
id: request_path
pages: /user
negate: false
context_mapping: { }

View File

@ -0,0 +1,10 @@
condition_test.settings:
type: config_object
label: 'Settings'
mapping:
visibility:
type: sequence
label: Visibility conditions
sequence:
type: condition.plugin.[id]
label: Visibility condition

View File

@ -5,12 +5,15 @@ namespace Drupal\condition_test;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Condition\ConditionManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\Entity\Node;
/**
* Routing controller class for condition_test testing of condition forms.
*/
class FormController implements FormInterface {
use StringTranslationTrait;
/**
* The condition plugin we will be working with.
@ -19,6 +22,13 @@ class FormController implements FormInterface {
*/
protected $condition;
/**
* The condition plugin current_theme.
*
* @var \Drupal\Core\Condition\ConditionInterface
*/
protected $condition_current_theme;
/**
* {@inheritdoc}
*/
@ -32,16 +42,25 @@ class FormController implements FormInterface {
public function __construct() {
$manager = new ConditionManager(\Drupal::service('container.namespaces'), \Drupal::cache('discovery'), \Drupal::moduleHandler());
$this->condition = $manager->createInstance('entity_bundle:node');
$this->condition_current_theme = $manager->createInstance('current_theme');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = $this->condition->buildConfigurationForm($form, $form_state);
$form['#tree'] = TRUE;
$form['entity_bundle'] = [];
$subformState = SubformState::createForSubform($form['entity_bundle'], $form, $form_state);
$form['entity_bundle'] = $this->condition->buildConfigurationForm($form['entity_bundle'], $subformState);
$form['current_theme'] = [];
$subformState = SubformState::createForSubform($form['current_theme'], $form, $form_state);
$form['current_theme'] = $this->condition_current_theme->buildConfigurationForm($form['current_theme'], $subformState);
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Submit'),
'#value' => $this->t('Submit'),
];
return $form;
}
@ -50,14 +69,19 @@ class FormController implements FormInterface {
* Implements \Drupal\Core\Form\FormInterface::validateForm().
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->condition->validateConfigurationForm($form, $form_state);
$subformState = SubformState::createForSubform($form['entity_bundle'], $form, $form_state);
$this->condition->validateConfigurationForm($form['entity_bundle'], $subformState);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->condition->submitConfigurationForm($form, $form_state);
$subformState = SubformState::createForSubform($form['entity_bundle'], $form, $form_state);
$this->condition->submitConfigurationForm($form['entity_bundle'], $subformState);
$subformState = SubformState::createForSubform($form['current_theme'], $form, $form_state);
$this->condition_current_theme->submitConfigurationForm($form['current_theme'], $subformState);
$config = $this->condition->getConfig();
foreach ($config['bundles'] as $bundle) {
\Drupal::messenger()->addStatus('Bundle: ' . $bundle);
@ -66,7 +90,10 @@ class FormController implements FormInterface {
$article = Node::load(1);
$this->condition->setContextValue('node', $article);
if ($this->condition->execute()) {
\Drupal::messenger()->addStatus(t('Executed successfully.'));
\Drupal::messenger()->addStatus($this->t('Executed successfully.'));
}
if ($this->condition_current_theme->execute()) {
\Drupal::messenger()->addStatus($this->condition_current_theme->summary());
}
}

View File

@ -37,13 +37,21 @@ class ConditionFormTest extends BrowserTestBase {
$article->save();
$this->drupalGet('condition_test');
$this->assertSession()->fieldExists('bundles[article]');
$this->assertSession()->fieldExists('bundles[page]');
$this->submitForm(['bundles[page]' => 'page', 'bundles[article]' => 'article'], 'Submit');
$this->assertSession()->fieldExists('entity_bundle[bundles][article]');
$this->assertSession()->fieldExists('entity_bundle[bundles][page]');
$this->submitForm(['entity_bundle[bundles][page]' => 'page', 'entity_bundle[bundles][article]' => 'article'], 'Submit');
// @see \Drupal\condition_test\FormController::submitForm()
$this->assertSession()->pageTextContains('Bundle: page');
$this->assertSession()->pageTextContains('Bundle: article');
$this->assertSession()->pageTextContains('Executed successfully.');
$this->assertSession()->pageTextContains('The current theme is stark');
/** @var \Drupal\Core\Extension\ThemeInstallerInterface $theme_installer */
$theme_installer = $this->container->get('theme_installer');
$theme_installer->install(['olivero']);
$this->drupalGet('condition_test');
$this->submitForm(['current_theme[theme]' => 'olivero', 'current_theme[negate]' => TRUE], 'Submit');
$this->assertSession()->pageTextContains('The current theme is not olivero');
}
}