select_or_other seems to be a constant source of notices "Undefined index #other…". To avoid that we should define default values in hook_element_info() for all properties that are then used in select_or_other_element_process().

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

torotil created an issue. See original summary.

torotil’s picture

Here is a quick patch that defines defaults to fix the two most common notices for my sites.

todo:

  1. Check that really all properties have defaults.
  2. Decide what the defaults should be.
legolasbo’s picture

Issue tags: +Quick fix

Thanks for your patch, let's complete the list of defaults and get this committed.

torotil’s picture

This patch makes the following changes:

First I changed the part of the code that transfers the #properties from $element to $element['select'] to only transfer the properties that are present. After that the following #properties were still used (without checking for existence) in select_or_other_element_process(): #default_value, #options, #other_unknown_defaults, #other_delimiter, #multiple, #other, #attributes, #select_type.

This means that #attributes, #options, #other_unknown_defaults and #other_delimiter were missing. This patch defines them as follows:

array(
      '#options' => array(),
      '#other_unknown_defaults' => 'other',
      '#other_delimiter' => '',
      '#attributes' => array(),
)

Additionally processing related to #disabled can be dropped. It is propagated to child-elements automatically by form_builder(). The special case for #other_size is already handled by the loop a few lines above and can be dropped.

torotil’s picture

Forgot to attach the patch.

torotil’s picture

Status: Active » Needs review
legolasbo’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests
  • +++ b/select_or_other.module
    @@ -87,25 +90,11 @@ function select_or_other_element_process($element, &$form_state) {
    -    '#size' => isset($element['#size']) ? $element['#size'] : NULL,
    

    I don't see a default setting for #size. Doesn't this cause notices if #size isn't set?

  • +++ b/select_or_other.module
    @@ -87,25 +90,11 @@ function select_or_other_element_process($element, &$form_state) {
    +  $pass = array('#title', '#title_display', '#default_value', '#multiple',
    +    '#required', '#size', '#options', '#attributes', '#empty_option',
    +    '#empty_value');
    

    Let's follow core's coding standards here and either put all values on the same line, or one value per line.

  • I would also like to see test coverage for this.

    torotil’s picture

    I don't see a default setting for #size. Doesn't this cause notices if #size isn't set?

    The line with #size is removed in my patch - so no this won't throw notices ;)

    Let's follow core's coding standards here and either put all values on the same line, or one value per line.
    I would also like to see test coverage for this.

    Both true - I'd love to see test coverage too. But it seems that that's the limit of time that I can invest at this point. If you want anything more you'll need to find someone else to do it. BTW: this function seems to be perfectly unit-testable which is rather seldom in the Drupalverse.

    Good luck!

    legolasbo’s picture

    @torotil,

    Thanks for your effort so far! I'm not sure when I have time to complete the work, but your efforts are greatly appreciated.

    rimu’s picture

    Component: Forms API (Developers) » Field widget (non-specific or listed)

    Works for me. :D

    daften’s picture

    Status: Needs work » Closed (outdated)

    Closing as outdated because no activity in a long time and Drupal 7 goes EOL soon. Feel free to re-open if needed.