getTableOfContents example

$this->drupalGet($nodes[0]->toUrl('edit-form'));
    // Since Node 0 has children 2 levels deep, nodes 10 and 11 should not     // appear in the selector.     $this->assertSession()->optionNotExists('edit-book-pid', $nodes[10]->id());
    $this->assertSession()->optionNotExists('edit-book-pid', $nodes[11]->id());
    // Node 9 should be available as an option.     $this->assertSession()->optionExists('edit-book-pid', $nodes[9]->id());

    // Get a shallow set of options.     /** @var \Drupal\book\BookManagerInterface $manager */
    $manager = $this->container->get('book.manager');
    $options = $manager->getTableOfContents($book->id(), 3);
    // Verify that all expected option keys are present.     $expected_nids = [$book->id()$nodes[0]->id()$nodes[1]->id()$nodes[2]->id()$nodes[3]->id()$nodes[6]->id()$nodes[4]->id()];
    $this->assertEquals($expected_nidsarray_keys($options));
    // Exclude Node 3.     $options = $manager->getTableOfContents($book->id(), 3, [$nodes[3]->id()]);
    // Verify that expected option keys are present after excluding Node 3.     $expected_nids = [$book->id()$nodes[0]->id()$nodes[1]->id()$nodes[2]->id()$nodes[4]->id()];
    $this->assertEquals($expected_nidsarray_keys($options));
  }

  /** * Tests the book navigation block when an access module is installed. */

    }
    elseif (!$book_link['bid']) {
      $form['#prefix'] .= '<em>' . $this->t('No book selected.') . '</em>';
    }
    else {
      $form = [
        '#type' => 'select',
        '#title' => $this->t('Parent item'),
        '#default_value' => $book_link['pid'],
        '#description' => $this->t('The parent page in the book. The maximum depth for a book and all child pages is @maxdepth. Some pages in the selected book may not be available as parents if selecting them would exceed this limit.', ['@maxdepth' => static::BOOK_MAX_DEPTH]),
        '#options' => $this->getTableOfContents($book_link['bid']$book_link['parent_depth_limit'][$book_link['nid']]),
        '#attributes' => ['class' => ['book-title-select']],
        '#prefix' => '<div id="edit-book-plid-wrapper">',
        '#suffix' => '</div>',
      ];
    }
    $this->renderer->addCacheableDependency($form$config);

    return $form;
  }

  /** * Recursively processes and formats book links for getTableOfContents(). * * This helper function recursively modifies the table of contents array for * each item in the book tree, ignoring items in the exclude array or at a * depth greater than the limit. Truncates titles over thirty characters and * appends an indentation string incremented by depth. * * @param array $tree * The data structure of the book's outline tree. Includes hidden links. * @param string $indent * A string appended to each node title. Increments by '--' per depth * level. * @param array $toc * Reference to the table of contents array. This is modified in place, so * the function does not have a return value. * @param array $exclude * Optional array of Node ID values. Any link whose node ID is in this * array will be excluded (along with its children). * @param int $depth_limit * Any link deeper than this value will be excluded (along with its * children). */
foreach ($books as $book) {
      $bid = $book['bid'];
      $build = $bm->bookTreeOutput($bm->bookTreeAllData($bid));
      $items = $build['#items'];
      $this->assertBookItemIsCorrectlyTranslated($items[$bid]$langcode);
      $this->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 1]$langcode);
      $this->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 1]['below'][$bid + 3]$langcode);
      $this->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 1]['below'][$bid + 4]$langcode);
      $this->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 2]$langcode);
      $this->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 2]['below'][$bid + 5]$langcode);
      $this->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 2]['below'][$bid + 6]$langcode);
      $toc = $bm->getTableOfContents($bid, 4);
      // Root entry does not have an indent.       $this->assertToCEntryIsCorrectlyTranslated($toc$langcode$bid, '');
      // The direct children of the root have one indent.       $this->assertToCEntryIsCorrectlyTranslated($toc$langcode$bid + 1, '--');
      $this->assertToCEntryIsCorrectlyTranslated($toc$langcode$bid + 2, '--');
      // Their children have two indents.       $this->assertToCEntryIsCorrectlyTranslated($toc$langcode$bid + 3, '----');
      $this->assertToCEntryIsCorrectlyTranslated($toc$langcode$bid + 4, '----');
      $this->assertToCEntryIsCorrectlyTranslated($toc$langcode$bid + 5, '----');
      $this->assertToCEntryIsCorrectlyTranslated($toc$langcode$bid + 6, '----');
      // $bid might be a string.
Home | Imprint | This part of the site doesn't use cookies.