Problem/Motivation

Updating db results in
> [notice] Update started: block_class_update_20004
> [error] json_decode(): Argument #1 ($json) must be of type string, array given
> [error] Update failed: block_class_update_20004

Steps to reproduce

Drupal 11.3.8
Block class 4.0.2

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

leontin created an issue. See original summary.

merlin06’s picture

We have a similar problem but the hook is block_class_update_20017()
We have an additional issue where we run feature:import before database updates.

The feature:import adds additional classes in via:

third_party_settings:
  block_class:
    classes: my-class-1

We end up with block_classes.settings in the database that looks like:

block_classes_stored [
  'my-class-1',
  '{my-class-2:my-class-2, my-class-3:my-class-3}',
  'my-class-4',
];

This is my solution that can convert our situation into a proper array of block classes for block_class.settings.yml:

/**
 * Convert block_classes_stored from JSON to a sequence.
 */
function block_class_update_20017() {
  $config = \Drupal::configFactory()->getEditable('block_class.settings');

  $new_classes = [];
  $block_classes_stored = $config->get('block_classes_stored');
  foreach ($block_classes_stored as $block_classes) {
    if (is_string($block_classes)) {
      $decoded = Json::decode($block_classes);
      if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
        // Json decoded successfully with no errors. Add it to the classes.
        $new_classes = array_merge($new_classes, $decoded);
      }
      else {
        // We have a string... but it's not a JSON encoded string. Add it to the classes.
        $new_classes[] = $block_classes;
      }
    }
  }
  $new_classes = array_unique(array_filter(array_values($new_classes)));
  $config->set('block_classes_stored', $new_classes);
  $config->save();
}

dydave’s picture

Apparently this issue is still occuring as it was recently reported by a user in duplicate issue #3586652: Updating problem.

Changes suggested by @umac_de at #2:
Replacing

// Get the array from JSON.
$block_classes_stored = Json::decode($block_classes_stored);

with

// Get the array from JSON if string.
if (is_string($block_classes_stored)) {
  $block_classes_stored = Json::decode($block_classes_stored);
}

Let's trying putting this change and the one above from #3 into a merge request (?!)

Also related to: #3493849: Argument #1 ($array) must be of type array, string given block_class/src/Service/BlockClassHelperService.php.

dydave changed the visibility of the branch 4.0.x to hidden.

  • dydave committed 6353e935 on 4.0.x
    Issue #3513350 by dydave, leontin, umac_de: Fixed database update '...
dydave’s picture

Status: Active » Fixed

Quick follow-up on this issue:

Created MR !75 at #7 and since all the required jobs and tests were still passing 🟢, I went ahead and merged the changes at #8. 🥳

As for the issue encountered at #3 with database update block_class_update_20017:
After second thought, it should probably be the object of a separate ticket, more specifically describing the problem, errors and steps to reproduce.

No additional documentation or tests are necessary for this change, therefore, marking issue as Fixed, for now.

Feel free to let us know if you encounter more issues with db update block_class_update_20004 or any other updates of the module, we would surely be glad to help.
Thanks in advance!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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