drupaldrupal-8

What is the equivalent of hook_page_alter()?


I am trying to append a template on every page.
In Drupal 7, we basically append it using hook_page_alter().

function hook_page_alter(&$page) { 
  $page['page_bottom']['devel']= array(
    '#type' => 'markup',
    '#markup' => '<div style="clear:both;">' . theme('TEST') . '</div>',
  ); // add test template on every page at bottom position.
}

In Drupal 8 there is no hook_page_alter().

Which hook should I use with Drupal 8?


Solution

  • You can use hook_preprocess_page(&$variables) in Drupal 8 to alter page content.

    Example:

    function bartik_preprocess_page(&$variables){
    
       $variables['page']['footer_fourt']['test']= array( 
            '#type' => 'markup', 
            '#markup' => '<div style="clear:both;">hello test</div>', );
       kint($variables['page']['footer_fourt']['test']);        
    }