I created a new node which is using basic_cart.
On display I get the PHP notice

Undefined offset: 0
in Drupal\basic_cart\Plugin\Field\FieldFormatter\AddToCartFormatter->viewElements()
line 42 of basic_cart/src/Plugin/Field/FieldFormatter/AddToCartFormatter.php

Line 42 checks if add_to_cart is set to 1:

  if ($entity->get('add_to_cart')->getValue()[0]['value'] == 1) {
    [...]

I do not know exactly which circumstances lead to $entity->get('add_to_cart')->getValue() returning an empty array (and thus generating the notice Undefined offset: 0).

But it could happen and I think we should explicitly check if the value is empty:

@@ -39,7 +39,9 @@ public static function defaultSettings() {
    */
   public function viewElements(FieldItemListInterface $items, $langcode) {
     $entity = $items->getEntity();
-    if ($entity->get('add_to_cart')->getValue()[0]['value'] == 1) {
+    if (!($entity->get('add_to_cart')->isEmpty())
+      && $entity->get('add_to_cart')->getValue()[0]['value'] == 1
+    ) {
       $addtocart_wrapper_container_class = SafeMarkup::checkPlain($this->getSetting('addtocart_wrapper_container_class'))->__toString();
       $addtocart_button_container_class = SafeMarkup::checkPlain($this->getSetting('addtocart_button_container_class'))->__toString();
       $addtocart_button_class = SafeMarkup::checkPlain($this->getSetting('addtocart_button_class'))->__toString();

Comments

gngn created an issue. See original summary.

gngn’s picture

Status: Active » Needs review
StatusFileSize
new1.02 KB

Attached patch like proposed in #1.

aurelien_m’s picture

Can anyone review this? I'm getting the same error, Thanks!

gngn’s picture

@aurelien_m: Did you try the patch #2 ?

eugene bocharov’s picture

I do not know exactly which circumstances lead to $entity->get('add_to_cart')->getValue() returning an empty array (and thus generating the notice Undefined offset: 0).

I found this happening if node was created before enabling Basic cart functionallity for it's content type.

To reproduce

  1. Install Drupal and basic_cart
  2. Go to /admin/config/basic_cart/settings, ensure that all checkboxes in "CONTENT TYPE SELECTION" unchecked.
  3. Add node of type "Basic page", for example.
  4. Go to /admin/config/basic_cart/settings and enable basic cart functionallity for the "Basic page" content type.
  5. Open in browser node, added at one of previous step.

The node was added before enabling basic cart for it's content type, so it doesn't contains any value in add_to_cart field.
If you reload the node page, notice may be disappared, cause of caching. After clearing render cache notice shows up again.

Patch #2 works fine. Thanks @gngn. But I would suggest some improvements.

$items is instance of FieldItemList and has magic method __isset(), which calls __isset() method for the first item. Also it has magic method __get.
On the other hand field add_to_cart is checkbox, it may contains only 1, 0, or may be empty, so we just could check it with empty().

if (!empty($items->value)) {

}

Also, in that case there is no need initialize $entity variable outside "if" block, so I put it inside.
Additionally I propose to move ajax library attaching inside if block, but place $elements array initializing - outside, so we can return empty array if add_to_cart field is empty or equals '0'

rauch’s picture

Is there any chance to get this in code or to get a new patch for current version 8.x-6.2-beta4?

eugene bocharov’s picture

I updated the patch to make it compatible with current dev version. It should works with 8.x-6.2-beta4 as well. Please, test.

rauch’s picture

The patch works for current version 8.x-6.2-beta4. Thank you.

norman.lol’s picture

Version: 8.x-6.x-dev » 8.x-8.x-dev