'Negate' form value for condition plugins should be cast to boolean in validation.

Created on 18 February 2020, almost 5 years ago
Updated 20 December 2023, about 1 year ago

Problem/Motivation

#2450637: Block page visibility option "Show for the listed pages" does not appear to save and #2811519: Blocks do not appear after being placed with the Rules module enabled (or other missing schemata for Condition plugins) both deal with having to cast the form element value for condition plugins' negate property to a boolean when submitting block forms. The typecasting is handled in \Drupal\block\BlockForm::validateVisibility(). However, since validateVisibility() calls each condition plugin's validateConfigurationForm() method, it seems to make more sense to do the typecast in that method.

This also makes integrating condition plugins for other purposes easier, so that other forms do not also have to handle typecasting negate.

Proposed resolution

Update \Drupal\Core\Condition\ConditionPluginBase::validateForm():

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    // In the config schema, 'negate' is a boolean. However, some form elements
    // such as checkbox return the value as 0/1. Cast here to ensure the data is
    // in the expected type.
    $form_state->setValue('negate', (bool) $form_state->getValue('negate'));
  }

Separately, the typecasting in \Drupal\block\BlockForm::validateVisibility() can probably be removed.

Remaining tasks

Needs tests and reviews

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

🐛 Bug report
Status

Closed: duplicate

Version

9.3

Component
Plugin 

Last updated 4 days ago

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024