The following error happens:

Warning:  htmlspecialchars() expects parameter 1 to be string, array given in includes/bootstrap.inc on line 1567

on AJAX when clicking wheel settings icon on Manage Display (on Product display and 'Product Type'/Add to Cart Form).

Affected code in commerce_cart.module:

      $summary[] = t('Add to Cart line item type: @type', array('@type' => commerce_line_item_type_get_name($settings['line_item_type'])));

It seems that the commerce_line_item_type_get_name() is returning array instead of the string causing issues in check_plain().

Input:

  [2]=>
  array(4) {
    ["file"]=>
    string(113) "modules/cart/commerce_cart.module"
    ["line"]=>
    int(2541)
    ["function"]=>
    string(1) "t"
    ["args"]=>
    array(2) {
      [0]=>
      &string(33) "Add to Cart line item type: @type"
      [1]=>
      &array(1) {
        ["@type"]=>
        array(5) {
          ["commerce_coupon"]=>
          string(6) "Coupon"
          ["training_bundle"]=>
          string(15) "Training bundle"
          ["bundle"]=>
          string(11) "Bundle Item"
          ["product"]=>
          string(7) "Product"
          ["commerce_webform"]=>
          string(16) "Commerce webform"
        }
      }
    }
  }

It seems 0 value is passed into commerce_line_item_type_get_name() causing to return array instead of string.

$settings = array(5) {
  ["show_quantity"]=>
  int(0)
  ["default_quantity"]=>
  int(1)
  ["combine"]=>
  int(0)
  ["show_single_product_attributes"]=>
  int(0)
  ["line_item_type"]=>
  int(0)
}

Comments

kenorb’s picture

Project: Commerce » Commerce Core
Version: » 7.x-1.9
Component: Code » Documentation
kenorb’s picture

.

kenorb’s picture

Issue summary: View changes
kenorb’s picture

Here is $display value before generating the $settings via:

  $settings = array_merge(field_info_formatter_settings($display['type']), $display['settings']);
$display = array(5) {
  ["label"]=>
  string(5) "above"
  ["module"]=>
  string(13) "commerce_cart"
  ["settings"]=>
  array(5) {
    ["combine"]=>
    int(0)
    ["default_quantity"]=>
    int(1)
    ["line_item_type"]=>
    int(0)
    ["show_quantity"]=>
    int(0)
    ["show_single_product_attributes"]=>
    int(0)
  }
  ["type"]=>
  string(30) "commerce_cart_add_to_cart_form"
  ["weight"]=>
  int(4)
}

field_info_formatter_settings($display['type']) = array(5) {
  ["show_quantity"]=>
  bool(false)
  ["default_quantity"]=>
  int(1)
  ["combine"]=>
  bool(true)
  ["show_single_product_attributes"]=>
  bool(false)
  ["line_item_type"]=>
  string(7) "product"
}

$settings = array(5) {
  ["show_quantity"]=>
  int(0)
  ["default_quantity"]=>
  int(1)
  ["combine"]=>
  int(0)
  ["show_single_product_attributes"]=>
  int(0)
  ["line_item_type"]=>
  int(0)
}
$view_mode = 'default';
$instance['display'] = array(3) {
  ["checkout_pane"]=>
  array(4) {
    ["label"]=>
    string(5) "above"
    ["settings"]=>
    array(0) {
    }
    ["type"]=>
    string(6) "hidden"
    ["weight"]=>
    int(0)
  }
  ["default"]=>
  array(5) {
    ["label"]=>
    string(5) "above"
    ["module"]=>
    string(13) "commerce_cart"
    ["settings"]=>
    array(5) {
      ["combine"]=>
      int(0)
      ["default_quantity"]=>
      int(1)
      ["line_item_type"]=>
      int(0)
      ["show_quantity"]=>
      int(0)
      ["show_single_product_attributes"]=>
      int(0)
    }
    ["type"]=>
    string(30) "commerce_cart_add_to_cart_form"
    ["weight"]=>
    int(4)
  }
  ["teaser"]=>
  array(5) {
    ["label"]=>
    string(5) "above"
    ["module"]=>
    string(23) "commerce_product_bundle"
    ["settings"]=>
    array(3) {
      ["default_quantity"]=>
      int(1)
      ["show_fieldset"]=>
      bool(true)
      ["show_quantity"]=>
      bool(false)
    }
    ["type"]=>
    string(40) "commerce_bundle_product_add_to_cart_form"
    ["weight"]=>
    int(0)
  }
}

Whole $instance variable:

array(14) {
  ["default_value"]=>
  NULL
  ["description"]=>
  string(0) ""
  ["display"]=>
  array(3) {
    ["checkout_pane"]=>
    array(4) {
      ["label"]=>
      string(5) "above"
      ["settings"]=>
      array(0) {
      }
      ["type"]=>
      string(6) "hidden"
      ["weight"]=>
      int(0)
    }
    ["default"]=>
    array(5) {
      ["label"]=>
      string(5) "above"
      ["module"]=>
      string(13) "commerce_cart"
      ["settings"]=>
      array(5) {
        ["combine"]=>
        int(0)
        ["default_quantity"]=>
        int(1)
        ["line_item_type"]=>
        int(0)
        ["show_quantity"]=>
        int(0)
        ["show_single_product_attributes"]=>
        int(0)
      }
      ["type"]=>
      string(30) "commerce_cart_add_to_cart_form"
      ["weight"]=>
      int(4)
    }
    ["teaser"]=>
    array(5) {
      ["label"]=>
      string(5) "above"
      ["module"]=>
      string(23) "commerce_product_bundle"
      ["settings"]=>
      array(3) {
        ["default_quantity"]=>
        int(1)
        ["show_fieldset"]=>
        bool(true)
        ["show_quantity"]=>
        bool(false)
      }
      ["type"]=>
      string(40) "commerce_bundle_product_add_to_cart_form"
      ["weight"]=>
      int(0)
    }
  }
  ["display_label"]=>
  string(0) ""
  ["label"]=>
  string(14) "Extension pack"
  ["required"]=>
  int(0)
  ["settings"]=>
  array(3) {
    ["field_injection"]=>
    int(1)
    ["referenceable_types"]=>
    array(6) {
      ["consulting_services"]=>
      int(0)
      ["invoice"]=>
      int(0)
      ["foo_pro"]=>
      string(11) "foo_pro"
      ["product"]=>
      int(0)
      ["subscription"]=>
      string(12) "subscription"
      ["training"]=>
      int(0)
    }
    ["user_register_form"]=>
    bool(false)
  }
  ["widget"]=>
  array(5) {
    ["active"]=>
    int(1)
    ["module"]=>
    string(7) "options"
    ["settings"]=>
    array(0) {
    }
    ["type"]=>
    string(15) "options_buttons"
    ["weight"]=>
    int(6)
  }
  ["id"]=>
  string(3) "222"
  ["field_id"]=>
  string(3) "133"
  ["field_name"]=>
  string(29) "field_commerce_extension_pack"
  ["entity_type"]=>
  string(4) "node"
  ["bundle"]=>
  string(20) "product_subscription"
  ["deleted"]=>
  string(1) "0"
}

So the source of the problem is ["line_item_type"]=> int(0) somewhere in field settings. But I'm not sure which settings can be wrong.

My product display field (field_commerce_extension_pack ) is Product reference type displayed as Check boxes.
But the same happening for field_product_type which is Product reference displayed as Select list.
And even for General image (field_general_image) which is Image field.

kenorb’s picture

Status: Active » Needs review

Proposed patch:

--- a/modules/cart/commerce_cart.module
+++ b/modules/cart/commerce_cart.module
@@ -2537,7 +2537,7 @@ function commerce_cart_field_formatter_settings_summary($field, $instance, $view
       t('!visibility attributes on single product forms.', array('!visibility' => !empty($settings['show_single_product_attributes']) ? t('Showing') : t('Hiding'))),
     );
 
-    if (count(commerce_product_line_item_types()) > 1) {
+    if (count(commerce_product_line_item_types()) > 1 && $settings['line_item_type']) {
       $summary[] = t('Add to Cart line item type: @type', array('@type' => commerce_line_item_type_get_name($settings['line_item_type'])));
     }
   }
ordermind’s picture

Patch works for me.

ordermind’s picture

Attaching patch.

ordermind’s picture

StatusFileSize
new660 bytes

Sorry, the above patch file doesn't seem to be working. Here's one that does.

The last submitted patch, 7: commerce-field_formatter_htmlspecialchars_fix-2304383-7.patch, failed testing.

rszrama’s picture

Version: 7.x-1.9 » 7.x-1.x-dev
Status: Needs review » Needs work
Issue tags: +sprint

Tagging for http://contribkanban.com/#/board/commerce/7.x-1.x.

We prefer !empty() checks for array values to catch the case of unset keys.

joelpittet’s picture

Status: Needs work » Needs review
StatusFileSize
new781 bytes

!empty added.

joelpittet’s picture

Component: Documentation » Cart

Since we are working on the cart.

rszrama’s picture

One thought I had here, too, is instead of just not showing the label for the field formatter's line item type, we could default to 'product' as done in commerce_cart_field_formatter_view():

    // Check to ensure products are referenced, before returning results.
    if (!empty($products)) {
      $type = !empty($settings['line_item_type']) ? $settings['line_item_type'] : 'product';
      $line_item = commerce_product_line_item_new(commerce_product_reference_default_product($products), $settings['default_quantity'], 0, array(), $type);
      // ...
    }

So instead of checking !empty($settings['line_item_type']) and bailing in the patch, we could check for it and use 'product' if it isn't set. What do you think?

Sorry, I wish I hadn't made this so hacky to begin with. ; )

joelpittet’s picture

StatusFileSize
new732 bytes
new1.13 KB

Agreed, +1 for consistency

rszrama’s picture

Status: Needs review » Fixed

Stellar. I wish the underlying API was better, but this is probably as good as it can be in the meantime. : )

  • rszrama committed 47f22ba on 7.x-1.x authored by joelpittet
    Issue #2304383 by joelpittet, ordermind, kenorb: Warning on AJAX:...
joelpittet’s picture

Easier to refactor if they are same same;)

Status: Fixed » Closed (fixed)

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