I'm trying the change the homepage path based on the current request. The system_main_block block renders the logo which is linked to the homepage. If the current path starts with /foo/..., I want to change the homepage path to point to /foo instead of the default /.

I want to pass a custom variable to a block using hook_block_view_alter().

The hook gives me the build array and the Block itself as arguments.

How can I add a variable to the build array that I can then access in the view (twig template)?

The build array looks like this:

    '#theme' => string 'block'
    '#attributes' => 
        array (size=0)
    '#contextual_links' => 
    '#weight' => int -3
    '#configuration' => 
        array (size=4)
            'id' => string 'system_main_block' (length=17)
            'label' => string 'Main page content' (length=17)
            'provider' => string 'system' (length=6)
            'label_display' => string '0' (length=1)
    '#plugin_id' => string 'system_main_block' (length=17)
    '#base_plugin_id' => string 'system_main_block' (length=17)
    '#derivative_plugin_id' => null
    '#id' => string 'my_id' (length=19)
    '#pre_render' => 
        array (size=1)
            0 => string 'Drupal\block\BlockViewBuilder::preRender' (length=40)
    '#block' => 

I can't see where I can assign an extra variable. Am using the wrong hook?

  • Can you explain what your end goal is?
    – Jaypan
    Mar 30, 2017 at 10:38
  • @Jaypan I updated the description.
    – Tim
    Mar 30, 2017 at 14:55

2 Answers 2


I think a better option is to override PathProcessorFront::processOutbound():

 public function processOutbound($path, &$options = array(), Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
    // The special path '<front>' links to the default front page.
    if ($path === '/<front>') {
      $path = '/';
    return $path;

Overriding in this case means using a similar outbound pathprocessor with a higher priority and replace '/<front>' or a lower priority and replace '/'.


Register the path processor as a service with the tag path_processor_outbound in mymodule.services.yml. This is the core service definition from the example before:

    class: Drupal\Core\PathProcessor\PathProcessorFront
      - { name: path_processor_inbound, priority: 200 }
      - { name: path_processor_outbound, priority: 200 }
    arguments: ['@config.factory']
  • Hi 4k4, I can use PathProcessorManager::addOutbound to register a new Processor, but where should I add the code in my module? Should I add an event subscriber for a KernelEvent?
    – Tim
    Apr 20, 2017 at 11:06
  • @Tim, this is a service with the tag path_processor_outbound, see edit.
    – 4uk4
    Apr 20, 2017 at 11:56

The system_main_block is not the place to be looking. Whilst it's not a block in the traditional sense. Your logo position, and the URL it links to, are probably controlled by your theme. It should be simple to pass an updated URL as a custom variable in the theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.