I needed to "convert" a text field to a select and this module become useful and better than select_or_other, but I did need Other to be optional, as I don't want free addition to the text, but rather only elements within the range (when possible).

The attached solution makes the Other option optional, but only when possible.

The rationale behind this is that allowed values list can change over time and also be added on sites where there's already content there, so if the field data is not currently present on the allowed values list, the other field will still be available on that case.

Comments

hanoii created an issue. See original summary.

hanoii’s picture

Status: Active » Needs review
StatusFileSize
new3.31 KB

Attached is the patch for this functionality.

dagmar’s picture

Issue summary: View changes
Status: Needs review » Needs work
StatusFileSize
new9.45 KB
new6.29 KB

Thanks for this patch. I have a few comments.

I don't think the description of the checkbox is accurate.

  1. +++ b/src/WidgetHelper.php
    @@ -86,6 +87,13 @@ class WidgetHelper {
    +      '#title' => $this->t('Hide custom value option'),
    +      '#description' => $this->t('Do not use the custom value unless there is content on field not currently available on the allowed values list.'),
    

    I think the title is could be better. What about. "Do not use custom value label."?

  2. +++ b/src/WidgetHelper.php
    @@ -86,6 +87,13 @@ class WidgetHelper {
    +      '#default_value' => $settings['custom_value_optional']
    

    We could use drupal #states to hide the 'custom_value_label' field to enforce the meaning of this setting.

  3. +++ b/src/WidgetHelper.php
    @@ -103,7 +111,11 @@ class WidgetHelper {
    +      $text .= ' ' . $this->t('(Hidden when possible)');
    

    I'm not sure if hidden when possible reflects the reality of this code. I mean, there is no check to determine if is possible to hide it or not. So what about replace the $summary with. No "custom label enabled".

hanoii’s picture

I guess I attempted, agree on poorly, to convey that the "Other" option will still show in some circumstances, that is when there is content on the node (on edit) that's not currently on the settings for the select of this widget.

Otherwise it's confusing that you chose to hide it, but you still have to provide a Label for it. And this is because I didn't want to disable the label setting, as I do need it for this circumstances, although maybe for those cases it's best to simply default to t('Other') and call it a day.

Maybe what I just wrote makes more sense. What if I make the Value field optional, as if empty it's not going to be used, with the exception of the above.

dagmar’s picture

Issue summary: View changes
StatusFileSize
new7.42 KB

Maybe what I just wrote makes more sense. What if I make the Value field optional, as if empty it's not going to be used, with the exception of the above.

+1

Something like?

hanoii’s picture

Status: Needs work » Needs review
StatusFileSize
new3.19 KB

now?

PS: Hola!

hanoii’s picture

hanoii’s picture

StatusFileSize
new3.17 KB

Actually changed back one of the lines.

daggerhart’s picture

Status: Needs review » Reviewed & tested by the community

This looks great and is a solid feature. Marking RTBC and will get it committed shortly.

Thanks!

  • daggerhart committed a4db9f8 on 8.x-1.x authored by hanoii
    Issue #3035569 by hanoii, dagmar: Allow other to be optional
    
daggerhart’s picture

Status: Reviewed & tested by the community » Fixed

Commited, thanks again!

Status: Fixed » Closed (fixed)

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