wbrowar / craft-grid

A field that lets you content manage CSS Grid in Craft CMS.
Other
18 stars 2 forks source link

Unused matrix block error. #11

Closed Geestig closed 4 years ago

Geestig commented 4 years ago

When adding a Matrix block but not using it in the grid field, I get an 'Undefined offset' error on save.

image

I'll see if I can find a fix for this when I got the time.

EDIT: Found a fix: Simply check that an item is not empty.

PART 1: LINE 234 -> 245 in Grid.php

if (!empty($targetIds[$i])) {
   $targetId = intval($targetIds[$i]);
   $valueId = $value['target']['items'][$i]['id'];

    if ($targetId != $valueId) {
       $unmatchedIds[] = [
             'target' => $targetId,
             'value' => $valueId,
           ];
      $value['target']['items'][$i]['id'] = $targetId;
   }
}

PART 2: LINE 315 -> 319

 if (!empty($fieldValue['target']['items'][$i])) {
  if ($breakpoint['id' . $fieldValue['target']['items'][$i]['id']] ?? false) {
      $breakpoint['id' . $currentIds[$i]] = $breakpoint['id' . $fieldValue['target']['items'][$i]['id']];
      unset($breakpoint['id' . $fieldValue['target']['items'][$i]['id']]);
  }
}
wbrowar commented 4 years ago

Thanks for both reporting and helping to figure this out, @Geestig! I'll have an update out in just a little bit with this fix in it.

Geestig commented 4 years ago

EDIT: New error. When adding a matrix block inbetween other blocks in the data matrix it returns an error because of the id not being found. @wbrowar