Undefined array key "pid" in BookManager::bookTreeOutput() when synchronizing child nodes

Created on 6 January 2025, 17 days ago

Steps to reproduce:

  1. Create a book node.
  2. Create a child node with different content type (eg: chapter) associated with the book.
  3. Attempt to synchronize the moderation state between the book and its children using a hook or custom code.
  4. Observe PHP warnings about the missing pid key and inconsistent behavior in child nodes.

Observed behavior:

  • PHP warning:
Warning: Undefined array key "pid" in BookManager.php on line 286.
  • The hierarchy of book nodes is broken, leading to issues when iterating through child nodes.
  • Expected behavior:

    • The pid key should always be present in the array returned by loadBookChildren() or any other relevant method.
    • The module should handle cases where pid is missing more gracefully, without throwing warnings.

    Debugging notes:

    1. The issue occurs in the method BookManager::bookTreeOutput() when accessing the pid key in the $book_link array.
    2. The pid key seems to be missing for certain child nodes under specific conditions (e.g., if the book hierarchy is not fully initialized or if a node is incorrectly associated with a book).

    Proposed solution:

    • Add validation to ensure that pid is present before using it.
    • Provide fallback behavior or better error handling when pid is missing.

    Example error log:

    Warning: Undefined array key "pid" in /modules/contrib/book/src/BookManager.php on line 286
    

    Supporting information:

    • Environment: PHP 8.3, Drupal 11.1.x
    • Module version: 2.0.0
    • Test code: Custom hooks for synchronizing book moderation states.

    Proposed resolution:

    Provide a patch or fallback behavior to address the missing pid key in BookManager.

    🐛 Bug report
    Status

    Active

    Version

    2.0

    Component

    Code

    Created by

    🇫🇷France estoclet

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

    Comments & Activities

    Production build 0.71.5 2024