Skip to main content
added 279 characters in body
Source Link
Tim
  • 1.4k
  • 1
  • 18
  • 32

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?

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?

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?

Source Link
Tim
  • 1.4k
  • 1
  • 18
  • 32

Pass variables to template using hook_block_view_alter()

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?