You are here

public function AlterSinglePageSiteOutput::alterOutput in Single Page Site 8

Same name and namespace in other branches
  1. 2.x modules/single_page_site_next_page/src/EventSubscriber/AlterSinglePageSiteOutput.php \Drupal\single_page_site_next_page\EventSubscriber\AlterSinglePageSiteOutput::alterOutput()
  2. 2.0.x modules/single_page_site_next_page/src/EventSubscriber/AlterSinglePageSiteOutput.php \Drupal\single_page_site_next_page\EventSubscriber\AlterSinglePageSiteOutput::alterOutput()

Alters the output of the single page item.

Parameters

\Drupal\single_page_site\Event\EventSinglePageSiteAlterOutput $event: Event value.

File

modules/single_page_site_next_page/src/EventSubscriber/AlterSinglePageSiteOutput.php, line 36

Class

AlterSinglePageSiteOutput
Class AlterSinglePageSiteOutput.

Namespace

Drupal\single_page_site_next_page\EventSubscriber

Code

public function alterOutput(EventSinglePageSiteAlterOutput $event) {
  $output = $event
    ->getOutput();
  $current_item = $event
    ->getCurrentItemCount();
  $count_menu_items = count($this->menuItems);

  // If item is not last item.
  if ($current_item < $count_menu_items) {
    $menu_item = $this->menuItems[$current_item];

    // Get route params.
    $params = $menu_item['route_parameters'];

    // Fetch href.
    $href = Url::fromRoute($menu_item['route_name'], $params)
      ->toString();

    // Generate valid anchor.
    $anchor = $this->manager
      ->generateAnchor($href);

    // Generate next url.
    // (I know this is bad practice, but I haven't figured out yet how to
    // render a link with only a fragment and no URL).
    $next_page_link = '<a href="#' . $anchor . '" class="to-next-page">' . $menu_item['title'] . '</a>';

    // Attach link to output by creating new markup object.
    $event
      ->setOutput(Markup::create($output . $next_page_link));
  }
}