Error: Cannot unset offset in a non-array variable in Drupal\tablefield\Plugin\Field\FieldType\TablefieldItem->setValue() (line 164 of modules/contrib/tablefield/src/Plugin/Field/FieldType/TablefieldItem.php)

Created on 6 November 2023, about 1 year ago

Problem/Motivation

Error: Cannot unset offset in a non-array variable in Drupal\tablefield\Plugin\Field\FieldType\TablefieldItem->setValue() (line 164 of modules/contrib/tablefield/src/Plugin/Field/FieldType/TablefieldItem.php)

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇩🇪Germany DiDebru

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @DiDebru
  • Merge request !163399529: Fix fatal. → (Merged) created by DiDebru
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    2 pass
  • Status changed to Needs review 12 months ago
  • 🇮🇳India SandeepSingh199

    Hi @DiDebru, I got the same issue while trying to add New Rows in table. After your patch, its working fine.

    Marking this ticket to Needs Review.

  • 🇧🇪Belgium tvb

    Similar problem here (version 8.x-2.4):

    The website encountered an unexpected error. Please try again later.
    TypeError: Cannot access offset of type string on string in Drupal\tablefield\Plugin\Field\FieldType\TablefieldItem->setValue() (line 187 of modules/contrib/tablefield/src/Plugin/Field/FieldType/TablefieldItem.php).

    It is resolved after installing the patch.

  • 🇧🇪Belgium tvb

    Ignore my previous comment.

    Apparently the configuration settings at /admin/config/content/tablefield had disappeared.

    The TypeError message is gone after setting a CSV separator and defaults for number of rows and columns.

    No patch required.

  • Pipeline finished with Skipped
    15 days ago
    #358050
  • First commit to issue fork.
  • 🇺🇸United States erutan

    I wasn't able to recreate the issue, but it applies cleanly and fixes typos with rebuild being repeated that would stop it from being applied to earlier levels in the array.

    This should go into 3.0.x

  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    This change is triggering a phpstan error: "Cannot unset offset 'tablefield' on array." in src/Plugin/Field/FieldType/TablefieldItem.php.

    $values['tablefield'] is already unset in the previous line, so unsetting $values['tablefield']['rebuild'] does nothing. This suggests to me that something is wrong with the change. At least, this line should be removed: unset($values['tablefield']['rebuild']);

  • 🇺🇸United States erutan

    Sorry about missing that - I have phpstan running in my dev environment again though that should have been obvious.

    It seems the typo from ten years ago was putting [rebuild] on a new line (as well as duplicating it), rebuild seems to just be there to help restructure the data and then isn't needed after.

    elseif (!empty($values['tablefield'])) {
          $values['rebuild'] = $values['tablefield']['rebuild'];
          $values['value'] = $values['tablefield']['table'];
          unset($values['tablefield']['rebuild']);
        }

    The elseif statement below it only unsets rebuild.

    elseif (!empty($values['value']['tablefield'])) {
          $values['rebuild'] = $values['value']['tablefield']['rebuild'];
          $values['value'] = $values['value']['tablefield']['table'];
          unset($values['value']['tablefield']['rebuild']);
        }

    Does that seem right?

  • 🇬🇧United Kingdom thetwentyseven

    Still with this latest version which has already merge this patch I do have a similar problem:

    An AJAX HTTP error occurred.
    HTTP Result Code: 500
    Debugging information follows.
    Path: /node/add/product_page?ajax_form=1&_wrapper_format=drupal_ajax
    StatusText: error
    ResponseText: The website encountered an unexpected error. Try again later.TypeError: Cannot access offset of type string on string in Drupal\tablefield\Plugin\Field\FieldType\TablefieldItem->setValue() (line 171 of modules/contrib/tablefield/src/Plugin/Field/FieldType/TablefieldItem.php). Drupal\Core\TypedData\Plugin\DataType\ItemList->setValue(Array, 1) (Line: 107)
  • 🇺🇸United States erutan

    @thetwentyseven not seeing that on my side. Do you have steps to reproduce?

  • 🇬🇧United Kingdom thetwentyseven

    Never mind @erutan, I deleted everything and reinstalled the module, and now is working as expected. Thank you

Production build 0.71.5 2024