Testing the json result for a book I see no child data which is imho necessary to make it useful drill down a book.

In HTML view the children are done by the class BookOutline { which is something different.

I expected the direct children when asking for json on ie http://drupal.d8/node/1

What do I miss?

Issue fork hal-2291811

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

clemens.tolboom’s picture

Component: book.module » hal.module

I consider this now a HAL problem.

Using https://github.com/mikekelly/hal-browser neither an Article as top level book or a book page shows child pages to their _links or _embed

I myself would expect the child pages in _embed

Maybe this should be a view?

clemens.tolboom’s picture

Wim Leers’s picture

Title: Should book module respond differently on Rest call » Book module's prev/up/next link relations missing in HAL output
Component: hal.module » book.module

In template_preprocess_book_navigation():

  if ($book_link['nid']) {
    $variables['tree'] = $book_outline->childrenLinks($book_link);

    $build = array();

    if ($prev = $book_outline->prevLink($book_link)) {
      $prev_href = \Drupal::url('entity.node.canonical', array('node' => $prev['nid']));
      $build['#attached']['html_head_link'][][] = array(
        'rel' => 'prev',
        'href' => $prev_href,
      );
      $variables['prev_url'] = $prev_href;
      $variables['prev_title'] = $prev['title'];
    }

    /** @var \Drupal\book\BookManagerInterface $book_manager */
    $book_manager = \Drupal::service('book.manager');
    if ($book_link['pid'] && $parent = $book_manager->loadBookLink($book_link['pid'])) {
      $parent_href = \Drupal::url('entity.node.canonical', array('node' => $book_link['pid']));
      $build['#attached']['html_head_link'][][] = array(
        'rel' => 'up',
        'href' => $parent_href,
      );
      $variables['parent_url'] = $parent_href;
      $variables['parent_title'] = $parent['title'];
    }

    if ($next = $book_outline->nextLink($book_link)) {
      $next_href = \Drupal::url('entity.node.canonical', array('node' => $next['nid']));
      $build['#attached']['html_head_link'][][] = array(
        'rel' => 'next',
        'href' => $next_href,
      );
      $variables['next_url'] = $next_href;
      $variables['next_title'] = $next['title'];
    }
  }

I think the root cause of the problem here is that the book module does not embed this metadata in anyway in the entity. It's stored outside of the entity, and it is only presented by manipulating render arrays when viewing entities. So I don't think hal module could do this even if it wanted to.

dawehner’s picture

Well, yeah the book module would need to integrate into HAL I would say. I think there is no other way without breaking APIs.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
quietone’s picture

Project: Drupal core » Hypermedia Application Language (HAL)
Version: 9.3.x-dev » 1.0.x-dev
Component: book.module » Code
Category: Bug report » Feature request
Issue tags: +Bug Smash Initiative

Asked about this in #bugsmash, larowlan replied that this does belong in HAL project in contrib and it is a feature request.

Making those changes now.

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

smulvih2’s picture

Status: Active » Needs review

I am using the drupal/default_content_deploy module for content deployment, which uses drupal/hal for normalizing the content into JSON files. I was running into the same issue where the book data was not being included in the output JSON files.

The issue appears to be how the book structure is attached to the node, it is not a field but just a simple array. MR#3 adds a book normalizer and denormalizer class to export/import the book structure with dependencies (_embedded).

I had some trouble merging the book _embedded array into the main _embedded array, so I have included the book dependencies within the book array. I had to modify the ContentEntityNormalizer::denormalize() method to account for this.

Setting this ticket to Needs review to get some eyes on it, but this change will probably require a test, and maybe some dependency injection work.

Note - if using drupal/default_content_deploy, patch is needed (in related ticket).

smulvih2’s picture

smulvih2’s picture

Had to rebase the MR branch but looks good now.

smulvih2’s picture

Title: Book module's prev/up/next link relations missing in HAL output » Support for book structure
smulvih2’s picture

Version: 1.0.x-dev » 2.x-dev
smulvih2’s picture

MR is against the 1.0.x branch and I can't seem to change that, but this branch seems to be aligned with the 2.0.1 release. Adding patch here for 1.0.3 release (1.x branch).

Status: Needs review » Needs work

The last submitted patch, 23: hal-add-book-support-2291811-23.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

smulvih2’s picture

Oops, forgot to account for when the book module is not enabled. Added a check before calling the book.manager service.

smulvih2’s picture

Status: Needs work » Needs review

Ok tests are passing now on 1.x-dev branch for D9. Added this fix to the MR for 2.x-dev.

larowlan’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

This should be doable without needing to add special cases to the content entity normalizer.

Entity pilot module also relied on hal, and it had a single stand alone class for books - https://git.drupalcode.org/project/entity_pilot/-/blob/8.x-1.x/src/Norma...

It also had a test, we should be able to borrow from that too https://git.drupalcode.org/project/entity_pilot/-/blob/8.x-1.x/tests/src...

smulvih2’s picture

@larowlan thanks for the feedback and links! Will take a look at removing the changes to the ContentEntityNormalizer as you suggest and including tests as well.