The SELECT list for multiple products in the Add to cart form double-escapes the product title, so characters like "&" are displayed to the user incorrectly as "&".

The Form API select list builder already passes the select option values through check_plain(), so we don't need to use check_plain() when building the '#options' array.

Comments

fonant’s picture

The array of '#options' form a Drupal select element gets passed though check_plain() inside the form_select_options() function in includes/form.inc. So the option values should not be passed through check_plain() when defining the select form element.

The code at line 1648 to 1650 in commerce_cart.module should be changed from:

        foreach ($products as $product_id => $product) {
          $options[$product_id] = check_plain($product->title);
        }

to

        foreach ($products as $product_id => $product) {
          $options[$product_id] = $product->title;
        }

and the code from 1676 to 1678 should similarly read as follows, without the check_plain():

        foreach ($products as $product_id => $product) {
          $options[$product_id] = $product->title;
        }
willkaxu’s picture

Version: 7.x-1.1 » 7.x-1.x-dev
Priority: Normal » Major

I followed your suggestion and fixed my display issues. But I tested it in Drupal Commerce 7.x-1.x-dev, so the position of the code should be changed is different from your suggestion. I should change the code from 1731 to 1733 and the code from 1759 to 1761.

willkaxu’s picture

Status: Active » Needs review
StatusFileSize
new964 bytes

It basically does the alter trick from #1, the attached patch removes the escaping in commerce_cart_add_to_cart_form().

mr.baileys’s picture

Status: Needs review » Closed (duplicate)

I think this is a duplicate of #1089328: HTML entities aren't handled correctly when adding a product reference -- if I'm wrong feel free to re-open.

fonant’s picture

Status: Closed (duplicate) » Needs review

They're similar in that they're both double-quoting bugs, but they're quite different places in the code.

That #1089328: HTML entities aren't handled correctly when adding a product reference is for the Product Reference drop-down selector, a field selector.
This #1365202: Add to Cart form select list double-escapes HTML entities is for the Add to Cart drop-down selector on a product display page.

The patch above is all that's needed to fix this problem. Items in a form '#options' array should NOT be passed through check_plain() as the form code does that later.

rszrama’s picture

Priority: Major » Normal
Status: Needs review » Fixed

Just to clarify, values in a select elements #options array should not be sanitized up front, but for other elements like radios and checkboxes, they should be. I've committed the code changes in the patch in #3 but added comments for developers indicating that if they alter the form to change the type of the product selector element, they are also responsible for sanitizing the values in the #options array.

Commit: http://drupalcode.org/project/commerce.git/commitdiff/bee757d

Status: Fixed » Closed (fixed)

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