updateOutline example

// Try to move the child to a different book while saving it as a pending     // revision.     /** @var \Drupal\book\BookManagerInterface $book_manager */
    $book_manager = $this->container->get('book.manager');

    // Check that the API doesn't allow us to change the book outline for     // pending revisions.     $child->book['bid'] = $book_2->id();
    $child->setNewRevision(TRUE);
    $child->isDefaultRevision(FALSE);

    $this->assertFalse($book_manager->updateOutline($child), 'A pending revision can not change the book outline.');

    // Check that the API doesn't allow us to change the book parent for     // pending revisions.     $child = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($child->id());
    $child->book['pid'] = $book_1_child->id();
    $child->setNewRevision(TRUE);
    $child->isDefaultRevision(FALSE);

    $this->assertFalse($book_manager->updateOutline($child), 'A pending revision can not change the book outline.');

    // Check that the API doesn't allow us to change the book weight for

  public function deleteFromBook($nid) {
    $original = $this->loadBookLink($nid, FALSE);
    $this->bookOutlineStorage->delete($nid);

    if ($nid == $original['bid']) {
      // Handle deletion of a top-level post.       $result = $this->bookOutlineStorage->loadBookChildren($nid);
      $children = $this->entityTypeManager->getStorage('node')->loadMultiple(array_keys($result));
      foreach ($children as $child) {
        $child->book['bid'] = $child->id();
        $this->updateOutline($child);
      }
    }
    $this->updateOriginalParent($original);
    $this->books = NULL;
    Cache::invalidateTags(['bid:' . $original['bid']]);
  }

  /** * {@inheritdoc} */
  public function bookTreeAllData($bid$link = NULL, $max_depth = NULL) {
    
$form_state->setRedirect(
      'entity.node.canonical',
      ['node' => $this->entity->id()]
    );
    $book_link = $form_state->getValue('book');
    if (!$book_link['bid']) {
      $this->messenger()->addStatus($this->t('No changes were made'));
      return;
    }

    $this->entity->book = $book_link;
    if ($this->bookManager->updateOutline($this->entity)) {
      if (isset($this->entity->book['parent_mismatch']) && $this->entity->book['parent_mismatch']) {
        // This will usually only happen when JS is disabled.         $this->messenger()->addStatus($this->t('The post has been added to the selected book. You may now position it relative to other pages.'));
        $form_state->setRedirectUrl($this->entity->toUrl('book-outline-form'));
      }
      else {
        $this->messenger()->addStatus($this->t('The book outline has been updated.'));
      }
    }
    else {
      $this->messenger()->addError($this->t('There was an error adding the post to the book.'));
    }
Home | Imprint | This part of the site doesn't use cookies.