You are here

public function CourseObjectBook::overrideNavigation in Course 8.3

Same name and namespace in other branches
  1. 8.2 modules/course_book/src/Plugin/course/CourseObject/CourseObjectBook.php \Drupal\course_book\Plugin\course\CourseObject\CourseObjectBook::overrideNavigation()
  2. 3.x modules/course_book/src/Plugin/course/CourseObject/CourseObjectBook.php \Drupal\course_book\Plugin\course\CourseObject\CourseObjectBook::overrideNavigation()

Overrides navigation links.

Overrides CourseObject::overrideNavigation

File

modules/course_book/src/Plugin/course/CourseObject/CourseObjectBook.php, line 119

Class

CourseObjectBook
Plugin annotation @CourseObject( id = "book", label = "Book", handlers = { "fulfillment" = "\Drupal\course_book\Plugin\course\CourseObject\CourseObjectBookFulfillment" } )

Namespace

Drupal\course_book\Plugin\course\CourseObject

Code

public function overrideNavigation() {
  $links = parent::overrideNavigation();
  $route_match = Drupal::routeMatch();
  if ($route_match
    ->getRouteName() == 'entity.node.canonical') {
    $node = $route_match
      ->getParameter('node');

    /* @var $book_outline Drupal\book\BookOutline */
    $book_outline = \Drupal::service('book.outline');
    if (isset($node->book)) {
      $book_link = $node->book;
      if ($prev = $book_outline
        ->prevLink($book_link)) {
        $links['prev'] = \Drupal\Core\Link::createFromRoute(t('Previous'), 'entity.node.canonical', [
          'node' => $prev['nid'],
        ]);
      }
      if ($next = $book_outline
        ->nextLink($book_link)) {
        $links['next'] = \Drupal\Core\Link::createFromRoute(t('Next'), 'entity.node.canonical', [
          'node' => $next['nid'],
        ]);
      }
    }
    return $links;
  }
}