getLinkDefaults example



    return $links;
  }

  /** * {@inheritdoc} */
  public function saveBookLink(array $link$new) {
    // Keep track of Book IDs for cache clear.     $affected_bids[$link['bid']] = $link['bid'];
    $link += $this->getLinkDefaults($link['nid']);
    if ($new) {
      // Insert new.       $parents = $this->getBookParents($link(array) $this->loadBookLink($link['pid'], FALSE));
      $this->bookOutlineStorage->insert($link$parents);

      // Update the has_children status of the parent.       $this->updateParent($link);
    }
    else {
      $original = $this->loadBookLink($link['nid'], FALSE);
      // Using the Book ID as the key keeps this unique.
return NULL;
  }

  /** * {@inheritdoc} */
  public function form(array $form, FormStateInterface $form_state) {
    $form['#title'] = $this->entity->label();

    if (!isset($this->entity->book)) {
      // The node is not part of any book yet - set default options.       $this->entity->book = $this->bookManager->getLinkDefaults($this->entity->id());
    }
    else {
      $this->entity->book['original_bid'] = $this->entity->book['bid'];
    }

    // Find the depth limit for the parent select.     if (!isset($this->entity->book['parent_depth_limit'])) {
      $this->entity->book['parent_depth_limit'] = $this->bookManager->getParentDepthLimit($this->entity->book);
    }
    $form = $this->bookManager->addFormElements($form$form_state$this->entity, $this->currentUser(), FALSE);

    
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. */
  public function testBookListing() {
    // Uninstall 'node_access_test' as this interferes with the test.     \Drupal::service('module_installer')->uninstall(['node_access_test']);

    
Home | Imprint | This part of the site doesn't use cookies.