Problem/Motivation

When using hook_options_list_alter, the most natural thing is to use widget third party settings.
Example: In the contrib module betternone, i use widget TPS to move and/or rename the "- None -" option.
Not having that TPS makes the hook lame. Using other hooks makes the implementation brittle and needs a lot of boilerplate code.
Therefor i consider this as a bug.

Proposed resolution

Trivially add widget object to hook context.

Remaining tasks

Code, commit.

User interface changes

None.

API changes

Pure Addition.

Data model changes

None.

Release notes snippet

hook_options_list_alter now containt the widget object in its context.

Issue fork drupal-3134618

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

geek-merlin created an issue. See original summary.

geek-merlin’s picture

Status: Active » Needs review
StatusFileSize
new656 bytes

Trivial patch flying in.

geek-merlin’s picture

Title: hook_options_list_alter context lacks widget » hook_options_list_alter lacks $context['widget']
kristen pol’s picture

Thanks for the patch.

IMO it would be good to point out any other places in core where a similar thing is done. Also, this sounds like a feature request rather than a bug report but I'll leave that as is for now.

kristen pol’s picture

Category: Bug report » Feature request

I did a quick search and only see 'widget' => $this, in core/lib/Drupal/Core/Field/WidgetBase.php in below examples. Moving to "Feature request" as I think that is more appropriate.

form:

    // Allow modules to alter the field multi-value widget form element.
    // This hook can also be used for single-value fields.
    $context = [
      'form' => $form,
      'widget' => $this,
      'items' => $items,
      'default' => $this->isDefaultValueWidget($form_state),
    ];

formSingleElement:

    if ($element) {
      // Allow modules to alter the field widget form element.
      $context = [
        'form' => $form,
        'widget' => $this,
        'items' => $items,
        'delta' => $delta,
        'default' => $this->isDefaultValueWidget($form_state),
      ];
      \Drupal::moduleHandler()->alter(['field_widget_form', 'field_widget_' . $this->getPluginId() . '_form'], $element, $form_state, $context);
    }
kristen pol’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Moving back to "Needs work" as this would need a test.

geek-merlin’s picture

Category: Feature request » Bug report
Issue tags: -Needs tests

> (#4) Also, this sounds like a feature request rather than a bug

This irritates me a bit. The IS provides reasoning for "bug". I'm open to a contrary reasoning about this, but i don't see one here.

> (#6) Moving back to "Needs work" as this would need a test.

I doubt this, as ($claim=) our tests cover logic not APIs like hook parameters. At least i'm not aware of any hook $context test in core, or am i missing something?

> NW

Yes.
TODO: Update options.api.php

kristen pol’s picture

Well, I'll leave it up to the core maintainers to decide and argue about it and I'll stop irritating @geek-merlin here. Cheers! Hope your lockdown is going well.

geek-merlin’s picture

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

This adds documentation in options.api.php

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.

matroskeen’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Bug Smash Initiative

I have a similar situation, I have a contrib module that needs to access widget 3rd party settings in order to manipulate the options list.
Currently, I'm using hook_field_widget_form_alter(), but hook_options_list_alter() makes more sense to allow other modules to make some alterations afterwards (hook_field_widget_form_alter() is executed after hook_options_list_alter()).

I wanted to report about the same issue, and current changes look good to me.

I'm not sure this is a bug report, but as long as it's a "Bug report", the "Bug Smash Initiative" tag seems relevant.

Thanks!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

We have no test coverage of this feature at all. No implementation what-so-ever in core :)

Regardless of whether this is a bug we do need some test coverage and adding that will ensure we don't break this in the future. OptionsWidgetTest looks like a good place to add this and the hook implementation can be added to options_test.module.

geek-merlin’s picture

Thx @alexpott for looking into this.
Especially the guidance where tests fit best is appreciated a lot! 🌼

geek-merlin’s picture

Hmm, i don't remember prior art testing a hook, so starting freestyle: Hook implementations throws if $context not as expected. I hope the current tests trigger the hook, and add an invocation if needed.

For now testing commit-after-commit, later cleaning up and doing separate MR for test-only (i guess this is best practice).

Commits and their tests:
- Test existing context keys, should be green - https://www.drupal.org/pift-ci-job/1906744 - is green OK
- Test missing 'widget' context key, should be red - https://www.drupal.org/pift-ci-job/1906745 - is green NOK
- Add missing 'widget' context key and document it, should be green - https://www.drupal.org/pift-ci-job/1906746 - green

Hmm, as after the second commit we had test-without-fix situation but still green, my assumption did not hold, so will add a test-only MR and add a hook invocation, cherry-pick to fix-MR later.

geek-merlin’s picture

OK so it looks like mapping commits to tests does not work if i keep pushing.

And it looks like throwing in a hook gives no useful messages, at least in a Functional BrowserTest:
https://www.drupal.org/pift-ci-job/1906786

Thinking...

matroskeen’s picture

@geek-merlin, thanks for pushing this forward!
I was wondering, should we really test the incoming parameters?

Probably we can just implement a hook, where make some alterations to the existing options (i.e. remove some option) and then verify that option is not visible.

geek-merlin’s picture

Status: Needs work » Needs review

@alexpott:

OK finally found a way to do the test and get it NR.

Following the approach outlined in #3187361: Make DrupalCI generate and run test-only patches:

$ git diff --name-only 3134618-hookoptionslistalter-lacks-contextwidget 3134618-hookoptionslistalter-test-only 
core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php
core/modules/options/options.api.php
matroskeen’s picture

Issue tags: -Needs tests
StatusFileSize
new4.99 KB

I was probably not clear in my comment #18, so I'd like to attach a patch with another testing approach - using functional test instead of kernel. Using OptionsWidgetTest was recommended by @alexpott in #12.

Also, it seems we need a change record for this change, so I created a draft version: https://www.drupal.org/node/3190172

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.

smustgrave’s picture

Status: Needs review » Needs work

Patch or MR doesn't apply anymore
The last patch or MR doesn't apply to the target branch, please reroll the code so that it can be reviewed by the automated testbot.

D10 version needed
At this time we would need a D10.1.x patch or MR for this issue.

Also would be nice to get a tests-only patch for #20 to make sure the issue is being fixed.

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new782 bytes
new3.72 KB
new4.86 KB

The last submitted patch, 26: 3134618-26-tests-only.patch, failed testing. View results

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Even though I submitted the last patch I only changed a space error. So this review is of #20 really

Change record looks good. The example is great addition!

Also like the test coverage as it shows an example of altering it on a live instance.

I think this is good to move on for core committer review.

The last submitted patch, 26: 3134618-26-tests-only.patch, failed testing. View results

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed defcb862d6 and pushed to 10.1.x. Thanks!

diff --git a/core/modules/options/tests/src/Functional/OptionsWidgetsTest.php b/core/modules/options/tests/src/Functional/OptionsWidgetsTest.php
index 679a6620013..5838a06e9ba 100644
--- a/core/modules/options/tests/src/Functional/OptionsWidgetsTest.php
+++ b/core/modules/options/tests/src/Functional/OptionsWidgetsTest.php
@@ -651,8 +651,11 @@ public function testOptionsListAlter() {
 
     // Display form: check that _none options are present.
     $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
-    $this->assertNotEmpty($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', [':id' => 'edit-card-1', ':label' => '- None -']), 'A test select has a "None" choice with default label.');
-    $this->assertNotEmpty($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', [':id' => 'edit-card-4', ':label' => '- Please select something -']), 'A test select has a "None" choice with altered label.');
+    $xpath = '//select[@id=:id]//option[@value="_none" and text()=:label]';
+    $xpath_args = [':id' => 'edit-card-1', ':label' => '- None -'];
+    $this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery($xpath, $xpath_args));
+    $xpath_args = [':id' => 'edit-card-4', ':label' => '- Please select something -'];
+    $this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery($xpath, $xpath_args));
 
     // Display form: check that options are displayed correctly.
     $this->assertSession()->optionExists('card_1', 0);

Given we're asserting that an element exists let's use the correct assertion - you get better and more consistent error messages. Fixed on commit.

  • alexpott committed defcb862 on 10.1.x
    Issue #3134618 by geek-merlin, smustgrave, Matroskeen, Kristen Pol,...

Status: Fixed » Closed (fixed)

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