You are here

function redirect_page_build in Redirect 8

Same name and namespace in other branches
  1. 7.2 redirect.module \redirect_page_build()
  2. 7 redirect.module \redirect_page_build()

Implements hook_page_build().

Adds an action on 404 pages to create a redirect.

@todo hook_page_build() can no longer be used for this. Find a different way.

File

./redirect.module, line 129
The redirect module.

Code

function redirect_page_build(&$page) {
  if (redirect_is_current_page_404() && \Drupal::currentUser()
    ->hasPermission('administer redirects')) {
    if (!isset($page['content']['system_main']['actions'])) {
      $page['content']['system_main']['actions'] = [
        '#theme' => 'links',
        '#links' => [],
        '#attributes' => [
          'class' => [
            'action-links',
          ],
        ],
        '#weight' => -100,
      ];
    }

    // We cannot simply use current_path() because if a 404 path is set, then
    // that value overrides whatever is in $_GET['q']. The
    // drupal_deliver_html_page() function thankfully puts the original current
    // path into $_GET['destination'].
    $destination = \Drupal::destination()
      ->getAsArray();
    $page['content']['system_main']['actions']['#links']['add_redirect'] = [
      'title' => t('Add URL redirect from this page to another location'),
      'href' => 'admin/config/search/redirect/add',
      'query' => [
        'source' => $destination['destination'],
      ] + \Drupal::destination()
        ->getAsArray(),
    ];
  }
}