Problem/Motivation

It was reported over in Authcache (#2885086: Check to make sure element is an array before checking to see if it needs to be flagged), that the ubercart attributes module adds a form element 'attributes' => NULL when in the add-to-cart form for products which do not have any product attributes.

This is a violation of the form API contract, since elements (i.e. those array entries whose key does not start with a #-character) are expected to contain arrays. As a result any code which uses the core element_children function on the add-to-cart form might produce notices.

Example of an affected form structure:

Array
(
    [nid] => Array
    [qty] => Array
    [actions] => Array
    [node] => Array
    [attributes] => NULL
    [#form_id] => uc_product_add_to_cart_form_39
    [#type] => form
    [#build_id] => form-pQth...
    [form_build_id] => Array
    [#token] => uc_product_add_to_cart_form_39
    [form_token] => Array
    [form_id] => Array
    [#id] => uc-product-add-to-cart-form-39
    [#method] => post
    [#action] => /page-path
    [#theme_wrappers] => Array
    [#tree] => NULL
    [#parents] => Array
    [#validate] => Array
    [#submit] => Array
    [#theme] => Array
    [#after_build] => Array
)

Proposed resolution

Do not add the attributes element if there are no product-attributes in the first place.

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

None.

CommentFileSizeAuthor
#4 2885388-4-fix-NULL-element.diff1.42 KBznerol

Comments

znerol created an issue. See original summary.

tr’s picture

Status: Active » Postponed (maintainer needs more info)

This is a violation of the form API contract, since elements (i.e. those array entries whose key does not start with a #-character) are expected to contain arrays

Exactly where is this documented?

znerol’s picture

drupal_render():

Renderable arrays have two kinds of key/value pairs: properties and children. Properties have keys starting with '#' and their values influence how the array will be rendered. Children are all elements whose keys do not start with a '#'. Their values should be renderable arrays themselves, [...]

znerol’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new1.42 KB
tr’s picture

Version: 7.x-3.x-dev » 8.x-4.x-dev
Status: Needs review » Needs work
tr’s picture

Status: Needs work » Closed (duplicate)

Although this issue has a good explanation of why the fix is needed, I'm marking this as a duplicate of the earlier issue #2750151: Return empty array instead of null for attributes form. I like the fix there better.