Problem/Motivation

From what I can tell it's best practice to use massageFormValues()

The original code appears to have been added in 2013 #1751234: Convert option widgets to Plugin system

Steps to reproduce

NA

Proposed resolution

use massageFormValues(), in this case also involved extractFormValues()

Remaining tasks

Review

User interface changes

NA

Introduced terminology

NA

API changes

NA

Data model changes

NA

Release notes snippet

NA

Original post

... instead of massaging values in a validateElement() #validate callback

Issue fork drupal-2171395

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

swentel’s picture

Status: Active » Needs review
StatusFileSize
new2.35 KB
yched’s picture

Thanks for the patch @swentel.

Actually #2170979: remove the getValue() / setValue() dance in WidgetBase::sortItems() might be a better base for this cleanup. Would allow us to move everything into massageFormValues(), including the "make sure $element['#value'] is an array" part the patch still keeps in the validate.

swentel’s picture

Yep, that's what I was thinking too. Let's wait until that one gets in for further cleanup.

yched’s picture

swentel’s picture

Yep, I'll reroll tonight

swentel’s picture

StatusFileSize
new1.48 KB

Completely untested - let's see what the bot thinks :)

swentel’s picture

StatusFileSize
new1.76 KB

trying without the setValue() in validate - but I'm afraid it won't work

Status: Needs review » Needs work

The last submitted patch, 7: 2171395-7.patch, failed testing.

yched’s picture

Hm - right. The $elements generated by the various formElement() implementations can return a string if the field is single-valued (radios / select in single mode).
So we still need to keep an

if (!is_array($element['#value'])) {
  \Drupal::formBuilder()->setValue($element, array($element['#value']));
}

in validateElement() (with the above explanation as a comment so we don't scratch our heads again ?)

+++ b/core/modules/options/lib/Drupal/options/Plugin/Field/FieldWidget/OptionsWidgetBase.php
@@ -82,19 +82,16 @@ public static function validateElement(array $element, array &$form_state) {
     // Drupal\Core\Field\WidgetBase::submit() expects values as
     // an array of values keyed by delta first, then by column, while our
     // widgets return the opposite.

That comment would need to move down and be merged with the "Transpose selections..." code block below.

swentel’s picture

Status: Needs work » Needs review
StatusFileSize
new2.5 KB

rerolled, should be good now I think.

Status: Needs review » Needs work

The last submitted patch, 10: 2171395-10.patch, failed testing.

swentel’s picture

Status: Needs work » Needs review
StatusFileSize
new2.59 KB
yched’s picture

Status: Needs review » Reviewed & tested by the community

Looks good. Thanks !

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 12: 2171395-12.patch, failed testing.

swentel’s picture

Status: Needs work » Needs review
StatusFileSize
new2.36 KB
new1.17 KB

So that didn't fly apparently :)

wim leers’s picture

anavarre queued 15: 2171395-15.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 15: 2171395-15.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

psf_’s picture

Version: 8.6.x-dev » 8.8.x-dev

The file

/core/modules/options/lib/Drupal/options/Plugin/Field/FieldWidget/OptionsWidgetBase.php 

was moved to

/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php

And the patch is not applicable.

But I think that the problem persists.

psf_’s picture

StatusFileSize
new2.7 KB

I built a sample module with a new widget over text (plain) field, with a checkbox to clear it, and a content type with two new fields using this widget, one simple valued and the other multivalued, but massageFormValues is never called.

I attached the module here with the hope that it will is successful.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

smustgrave made their first commit to this issue’s fork.

smustgrave’s picture

Issue summary: View changes
smustgrave’s picture

Status: Needs work » Needs review

Disclosure used Claude Code to find my way to extractFormValues() and cleanup my comments for needing to filter out 0 as that was breaking checkbox tests.

dcam’s picture

Status: Needs review » Needs work

I left two comments. Setting the status to Needs Work to get answers to the questions.

Otherwise, I think the changes are probably OK. But I haven't done a really deep dive yet, like setting up fields and watching the values in a debugger.

smustgrave’s picture

Status: Needs work » Needs review

Tried my best to answer

dcam’s picture

Status: Needs review » Reviewed & tested by the community

I think this is OK to go. I performed the manual testing of Options widgets that I said I hadn't done yet in #40. I tried to test as many scenarios as I could think of, including single and multiple options, required and not required, and the two subclass widgets of the base (select list and checkbox/radio). After each save I checked the field's table contents in the database to verify that the resulting saved data matched what was expected from the main branch. I didn't notice any issues.

quietone’s picture

I read the issue and the MR and updated credit. I didn't find anything amiss.

  • catch committed eff71394 on 11.x
    task: #2171395 OptionsWidgetBase should use WidgetInterface::...

  • catch committed b5abc5d7 on main
    task: #2171395 OptionsWidgetBase should use WidgetInterface::...
catch’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Came here expecting this to remove some code, while it's a net addition, however other widgets in core do indeed implement this method and it's on the interface, so makes sense to do it for consistency.

Committed/pushed to main and 11.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

joelpittet’s picture

I am currently trying to get D12 for conditional_fields working and apparently we were relying on/working around this side-effect. Good to be consistent though, I think we can fix it there! #3596497: Automated Drupal 12 compatibility fixes for conditional_fields 4.x-dev. If anybody else in contrib is doing the same, that might help them (assuming I succeed), at least a cautionary tale either way... 🐉

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.