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

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

declare(strict_types=1);

namespace Drupal\entity_reference_hierarchy_book_nav\Plugin\Block;

/**
 * Provides a book navigation block.
 *
 * @Block(
 *   id = "entity_reference_hierarchy_book_nav_book_navigation",
 *   admin_label = @Translation("Book Navigation"),
 *   category = @Translation("Book"),
 *   context_definitions = {
 *     "node" = @ContextDefinition("entity:node")
 *   },
 * )
 */
final class BookNavigationBlock extends BookBlockBase {

  /**
   * {@inheritdoc}
   */
  public function bookBuild(&$build, $page, $book): array {
    $build['book-nav']['#type'] = 'container';
    $build['book-nav']['#attributes']['class'][] = 'book-navigation';
    $build['book-nav']['#attributes']['class'][] = 'container';
    $build['book-nav']['#attached']['library'][] = 'entity_reference_hierarchy_book_nav/book_nav';

    $previous = $this->book->previousPage($book, $page);
    $previous_button = [];
    if ($previous) {
      $previous_button = $previous->toLink('Previous Section')->toRenderable();
      $previous_button['#attributes']['title'] = $previous->label();
      $previous_button['#attributes']['aria-label'] = $previous->label();
    }

    $build['book-nav']['previous'] = [
      '#type' => 'container',
      '#attributes' => ['class' => ['book-navigation-previous']],
      'button' => $previous_button,
    ];

    $next = $this->book->nextPage($book, $page);
    $next_button = [];
    if ($next) {
      $next_button = $next->toLink('Next Section')->toRenderable();
      $next_button['#attributes']['title'] = $next->label();
      $next_button['#attributes']['aria-label'] = $next->label();
    }

    $build['book-nav']['next'] = [
      '#type' => 'container',
      '#attributes' => ['class' => ['book-navigation-next']],
      'button' => $next_button,
    ];

    return $build;
  }

}

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

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