Problem/Motivation

It would be useful to have a select list widget.

Proposed resolution

OptionsSelectWidget could be extended to add a widget doing this.
Some of the code could be shared with #3314975: Add radio buttons widget.

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

prudloff created an issue. See original summary.

prudloff’s picture

prudloff’s picture

Status: Active » Needs review
eli-t’s picture

Status: Needs review » Needs work

As #3314975: Add radio buttons widget, I think this is a good addition but we need to fix the coding standards in the new file.

Also

public function settingsForm(array $form, FormStateInterface $form_state): array {
    $element['label_attribute'] = [
.
.
      '#description' => $this->t('The attribute in the JSON:API resource object to use as radio button label.'),

has a copy/pasted reference to radio buttons, presumably from #3314975: Add radio buttons widget.

If we fix those, then we should commit this to both branches.

eli-t’s picture

Version: 8.x-1.0-alpha3 » 2.0.x-dev

No new features will be added to the 8.x-1.x branch so moving this to 2.0.x

eli-t’s picture

Status: Needs work » Needs review
eli-t’s picture

Just spun this up on the 2.0.x branch to make sure it works, and it seems to work fine.

I think we should add the JSON:API ID to the select list as well as the title though; this info is available when using the autocomplete so that the user can distinguish between multiple pieces of content with the same name.

eli-t’s picture

This has also spurred the thought - at the moment in the autocomplete, and also in this MR for the select list, there is configuration for what JSON:API attribute should be considered the title - to either autocomplete against, or display in the select list.

This will always be the same regardless of which widget you are using - it seems to be a fundamental property of the field itself rather than the widget you use to edit it. We should move that to field configuration instead of widget configuration. This also means you wouldn't need to change this setting multiple times if you switched between widgets for a given field.

This doesn't have to be done in this issue but should be done before this issue is merged so we don't have to rework.

eli-t’s picture

eli-t’s picture

Status: Needs review » Postponed

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

arousseau’s picture

Using this in one of our project, we ran into a small issue. The order along which the options are displayed by the select list seems to be a bit unpredictable.

This could be easily solved by forcing a sort order on the search attribute. Added a modification to the MR.

eli-t’s picture

Status: Postponed » Needs review

Thanks for contributing! I should have time to take a look at this next week.

eli-t’s picture

Note the issue raised in #10 is now resolved in 2.0.x.

arousseau’s picture

I reworked this in order to make use of OptionsProviderInterface as explained in \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase

I tried to stick to the way things are done in core entity_reference field type.

This should allow the use of hook_options_list_alter to sort the display order of the options too.

arousseau’s picture

I created #3474619: Use OptionsProviderInterface to allow options widgets usage to handle adding the OptionsProviderInterface implementation to the field type.

Once it is merged, we'll only need to add the following to the existing jsonapi_reference_field_widget_info_alter, to allow for the use of a select list :

$info['options_select']['field_types'][] = 'typed_resource_object';
eli-t’s picture

@arousseau ❤️

arousseau’s picture

Status: Needs review » Needs work

arousseau changed the visibility of the branch 3315187-Add_select_list_widget_2_0_x to hidden.

arousseau’s picture

I created a new branch + MR for this simple addition. It should be good to go!

eli-t’s picture

Status: Needs work » Reviewed & tested by the community

  • eli-t committed cca8e3f7 on 2.0.x authored by arousseau
    Issue #3315187: Added options_select widget support.
    
eli-t’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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