Commit 2c0f5919 authored by catch's avatar catch
Browse files

Issue #2787529 by Chi, rpayanm, quietone, tobiasb, Ankit.Gupta, smustgrave,...

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

(cherry picked from commit c9e52a8c)
parent c26201e4
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -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
+13 −0
Original line number Diff line number Diff line
# 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: {  }
+10 −0
Original line number Diff line number Diff line
condition_test.settings:
  type: config_object
  label: 'Settings'
  mapping:
    visibility:
      type: sequence
      label: Visibility conditions
      sequence:
        type: condition.plugin.[id]
        label: Visibility condition
+32 −5
Original line number Diff line number Diff line
@@ -5,12 +5,15 @@
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 @@ public function getFormId() {
  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 @@ public function buildForm(array $form, FormStateInterface $form_state) {
   * 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 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    $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());
    }
  }

+11 −3
Original line number Diff line number Diff line
@@ -37,13 +37,21 @@ public function testConfigForm() {
    $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');
  }

}