saveBookLink example

foreach (Element::children($form['table']) as $key) {
        if ($form['table'][$key]['#item']) {
          $row = $form['table'][$key];
          $values = $form_state->getValue(['table', $key]);

          // Update menu item if moved.           if ($row['parent']['pid']['#default_value'] != $values['pid'] || $row['weight']['#default_value'] != $values['weight']) {
            $link = $this->bookManager->loadBookLink($values['nid'], FALSE);
            $link['weight'] = $values['weight'];
            $link['pid'] = $values['pid'];
            $this->bookManager->saveBookLink($link, FALSE);
          }

          // Update the title if changed.           if ($row['title']['#default_value'] != $values['title']) {
            $node = $this->nodeStorage->load($values['nid']);
            $node = $this->entityRepository->getTranslationFromContext($node);
            $node->revision_log = $this->t('Title changed from %original to %current.', ['%original' => $node->label(), '%current' => $values['title']]);
            $node->title = $values['title'];
            $node->book['link_title'] = $values['title'];
            $node->setNewRevision();
            $node->save();
            
/** * Tests that saveBookLink() returns something. */
  public function testSaveBookLink() {
    $book_manager = \Drupal::service('book.manager');

    // Mock a link for a new book.     $link = ['nid' => 1, 'has_children' => 0, 'original_bid' => 0, 'pid' => 0, 'weight' => 0, 'bid' => 0];
    $new = TRUE;

    // Save the link.     $return = $book_manager->saveBookLink($link$new);

    // Add the link defaults to $link so we have something to compare to the return from saveBookLink().     $link = $book_manager->getLinkDefaults($link['nid']);

    // Test the return from saveBookLink.     $this->assertEquals($return$link);
  }

  /** * Tests the listing of all books. */
  
    $updated = FALSE;
    if (!$new) {
      $original = $this->loadBookLink($node->id(), FALSE);
      if ($node->book['bid'] != $original['bid'] || $node->book['pid'] != $original['pid'] || $node->book['weight'] != $original['weight']) {
        $updated = TRUE;
      }
    }
    if (($new || $updated) && !$node->isDefaultRevision()) {
      return FALSE;
    }

    return $this->saveBookLink($node->book, $new);
  }

  /** * {@inheritdoc} */
  public function getBookParents(array $item, array $parent = []) {
    $book = [];
    if ($item['pid'] == 0) {
      $book['p1'] = $item['nid'];
      for ($i = 2; $i <= static::BOOK_MAX_DEPTH; $i++) {
        $parent_property = "p$i";
        
Home | Imprint | This part of the site doesn't use cookies.