entity_reference_hierarchy_book_nav-1.0.0/src/Plugin/Block/BookContentsBlock.php

src/Plugin/Block/BookContentsBlock.php
<?php

declare(strict_types=1);

namespace Drupal\entity_reference_hierarchy_book_nav\Plugin\Block;

use Drupal\Core\Template\Attribute;

/**
 * Provides a book contents block block.
 *
 * @Block(
 *   id = "entity_reference_hierarchy_book_nav_book_contents_block",
 *   admin_label = @Translation("Book Contents Block"),
 *   category = @Translation("Book"),
*    context_definitions = {
 *     "node" = @ContextDefinition("entity:node")
 *   },
 * )
 */
final class BookContentsBlock extends BookBlockBase {

  /**
   * {@inheritdoc}
   */
  public function bookBuild(&$build, $page, $book): array {
    $items = [];
    $level = 0;
    $parents = [];
    $root = [];

    if (!$book->isNew()) {
      $build['book-title'] = [
        '#prefix' => '<h3>',
        '#suffix' => '</h3>',
        'link' => $book->toLink()->toRenderable(),
      ];
    }

    foreach ($book->get('field_book_structure') as $delta => $structure) {
      $node = $structure->entity;
      $depth = $structure->depth;

      if ($depth > $level) {
        array_push($parents, $delta - 1);
        $level = $depth;
      }
      elseif ($depth < $level) {
        array_pop($parents);
        $level = $depth;
      }

      if ($node->access()) {
        $items[$delta] = [
          'title' => $node->label(),
        ];

        if ($node->getType() != 'book_chapter') {
          $items[$delta]['link'] = $node->toUrl();
        }
      }
      if ($level == 0) {
        $root[] = $delta;
      }
      else {
        $items[end($parents)]['children'][] = $delta;
      }
    }

    $list = [];
    foreach ($root as $delta) {
      $list[] = $this->buildList($items, $delta);
    }

    $build['book-contents'] = [
      '#prefix' => '<nav id="book-contents-menu" class="navigation menu--book-contents" data-menu-options="{\'hoverType\':\'off\'}">',
      '#suffix' => '</nav>',
      '#theme' => 'menu',
      '#attributes' => new Attribute([
        'class' => [
          'book-contents',
        ],
      ]),
      '#items' => $list,
      '#attached' => [
        'library' => [
          'entity_reference_hierarchy_book_nav/book_nav',
        ],
      ],
    ];
    return $build;
  }

  /**
   * Build a list of items.
   */
  protected function buildList(array $items, $delta) {
    $build = [];

    $build['title'] = $items[$delta]['title'];
    $build['url'] = 'route:<button>';
    $build['attributes'] = new Attribute([
      'class' => [
        'book-contents-link',
      ],
    ]);
    if ($items[$delta]['link']) {
      $build['url'] = $items[$delta]['link'];
    }

    if (isset($items[$delta]['children'])) {
      $build['below'] = [];
      $build['is_expanded'] = TRUE;

      foreach ($items[$delta]['children'] as $child) {
        $build['below'][] = $this->buildList($items, $child);
      }
    }

    return $build;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc