0

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:

array
    '#theme' => string 'block'
    '#attributes' => 
        array (size=0)
        empty
    '#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' => 
        object(Drupal\block\Entity\Block)[3592]

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

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

2 Answers 2

1

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 '/'.

Edit:

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:

path_processor_front:
    class: Drupal\Core\PathProcessor\PathProcessorFront
    tags:
      - { name: path_processor_inbound, priority: 200 }
      - { name: path_processor_outbound, priority: 200 }
    arguments: ['@config.factory']
2
  • 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
    Commented Apr 20, 2017 at 11:06
  • @Tim, this is a service with the tag path_processor_outbound, see edit.
    – 4uk4
    Commented Apr 20, 2017 at 11:56
0

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.