Problem/Motivation

Upgrading from a fresh book 2.0.x installation.

Getting this error:
TypeError: Cannot access offset of type string on string in book_post_update_allowed_child_type_default6

Stack of errors:

 [error] TypeError: Cannot access offset of type string on string in book_post_update_allowed_child_type_default6() (line 148 of /www/web/modules/contrib/book/book.post_update.php) #0 /www/vendor/drush/drush/src/Commands/core/UpdateDBCommands.php(291): book_post_update_allowed_child_type_default6(Array)
> #1 /www/vendor/drush/drush/includes/batch.inc(257): Drush\Commands\core\UpdateDBCommands::updateDoOnePostUpdate('book_post_updat...', Array)
> #2 /www/vendor/drush/drush/includes/batch.inc(204): _drush_batch_worker()
> #3 /www/vendor/drush/drush/includes/batch.inc(75): _drush_batch_command('1')
> #4 /www/vendor/drush/drush/src/Commands/core/UpdateDBCommands.php(145): drush_batch_command('1')
> #5 [internal function]: Drush\Commands\core\UpdateDBCommands->process('1', Array)
> #18 {main}.
> TypeError: Cannot access offset of type string on string in /www/web/modules/contrib/book/book.post_update.php on line 148 #0 /www/vendor/drush/drush/src/Commands/core/UpdateDBCommands.php(291): book_post_update_allowed_child_type_default6(Array)
> #1 /www/vendor/drush/drush/includes/batch.inc(257): Drush\Commands\core\UpdateDBCommands::updateDoOnePostUpdate('book_post_updat...', Array)
> [warning] Drush command terminated abnormally.

In ProcessBase.php line 171:

Unable to decode output into JSON: Syntax error

TypeError: Cannot access offset of type string on string in book_post_update_allowed_child_type_default6() (line 148 of /www/html/modules/contrib/book/book.post_update.php).

Steps to reproduce

Install book 2.0.x with this configuration:
book.settings.yml

allowed_type_book: 1
block:
  navigation:
    mode: 'all pages'
child_type_book: book

Then upgrade to book 3.0.x

Then observe bug.

Proposed resolution

See code in merge request

Remaining tasks

Review code in merge request

User interface changes

Allows upgrades without crashing

API changes

See merge request

Data model changes

See merge request

Issue fork book-3563266

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

joseph.olstad created an issue. See original summary.

joseph.olstad’s picture

Assigned: joseph.olstad » Unassigned
Status: Active » Needs review

watch pipelines

smustgrave’s picture

Status: Needs review » Needs work

Will take a look before 3.0.0 but this seems like it’s changing too much. Will take a different approach

smustgrave’s picture

Status: Needs work » Postponed (maintainer needs more info)

Were you using a patch by chance?

I did a fresh install of 2.0.x
Configured the book settings to use Article and Basic page with Basic page as the child type
Switched to 3.0.x
Update hooks ran just fine
Config exported correctly.

joseph.olstad’s picture

@smustgrave
it's been a month now however have a look at the steps to reproduce again that I documented.

smustgrave’s picture

Seems like it may have been a unique issue not sure how you ended up with allowed_type_book: 1

mdranove made their first commit to this issue’s fork.

mdranove changed the visibility of the branch 3.0.x to hidden.

philipnorton42’s picture

I can see the same thing happening on my site.

The error is happening because the `allowed_child_type_default6` hook happens before `convert_allowed_types_to_structured`, and the error is caused because `convert_allowed_types_to_structured` is changing the structure of the config that the `allowed_child_type_default6` hook is then updating.

The order makes sense because the post update hooks are executed alphabetically (see the api docs).

This section here:
"In contrast to hook_update_N() the alphanumeric naming of functions in the file is the only thing which ensures the execution order of those functions. If update order is mandatory, you should add numerical prefix to NAME or make it completely numerical."

It looks like someone has tried to set one of the hooks to fire last by adding the 6 to the end, but as the docs says the order should be determined by the prefix.

Since there is an order to the hooks in this file can I suggest changing the post update hook names to the following:

- book_post_update_convert_allowed_types_to_structured -> book_post_update_1convert_allowed_types_to_structured
- book_post_update_allowed_child_type_default6 -> book_post_update_6allowed_child_type_default

I'll create a PR with the change in now.

mdranove’s picture

Status: Postponed (maintainer needs more info) » Needs work

I was able to repro the error, not sure how I got there but my book.settings looked like

_core:
  default_config_hash: jmBcLtS4JshpXXtB9fsiBehhukIaDTQjvozHpbvVyPo
allowed_types:
  - book
book_sort: weight
use_parent_selector: false
use_alternative_form: false
child_type: book
truncate_label: true

the post update hook tries to update assuming there is an associative array for allowed_types

$allowed_types = $config->get('allowed_types');
  foreach ($allowed_types as &$type) {
    if (empty($type['child_type'])) {
      $type['child_type'] = $type['content_type'];
      $updated = TRUE;
    }
  }

However, as noted above, this is not always the case. Not sure why.

I am confused though about how @joseph.olstad ended up with those configs. I ran git grep -F "allowed_type_book" $(git rev-list --all) on 1.x 2.x and 3.x and couldn't find it anywhere so I don't think this will really work in that case.

I opened a new MR with my fix, but then I noticed a comment just now from @philipnorton42. That fix looks even better, I just tried it and it worked for me.

philipnorton42’s picture

Thanks for the comment mdranove, appreciate you testing that so quickly :)

I've reverted the code on the branch that joseph created since I think it's detracting from the actual issue, where the hooks are just triggering out of order. I've updated the MR with the change to the hook names, which works when I've run some tests on it.

philipnorton42’s picture

Status: Needs work » Needs review
mdranove’s picture

Status: Needs review » Reviewed & tested by the community

smustgrave’s picture

Status: Reviewed & tested by the community » Fixed

Thanks all!

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.

joseph.olstad’s picture

I inherited allowed_type_book from sylus year 2021

commit eebcb6810ab495425a0ba0be63a7be88b332bb45
Author: sylus <sylus1984@gmail.com>
Date:   Wed Dec 22 11:51:03 2021 -0500

    Issue #3253829 by smulvih2: GC Subway not working correctly in Intranet theme

diff --git a/modules/custom/wxt_ext/wxt_ext_book/config/rewrite/book.settings.yml b/modules/custom/wxt_ext/wxt_ext_book/config/rewrite/book.settings.yml
new file mode 100644
index 00000000..457feceb
--- /dev/null
+++ b/modules/custom/wxt_ext/wxt_ext_book/config/rewrite/book.settings.yml
@@ -0,0 +1,5 @@
+allowed_type_book: 1
+block:
+  navigation:
+    mode: 'all pages'
+child_type_book: book

I'm not sure what uses this, no reference in the code base other than this entry in book.settings.yml. I'd have to dig a bit more. I only see one D9 book patch from the 2021 build and it doesn't have any reference to this.

joseph.olstad’s picture

I've pulled/ lightly massaged the book 3.0.x changes into livre 1.0.7

Status: Fixed » Closed (fixed)

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