0

I have a custom block with some values I can access in the template of that block, for example the product_type variable. In the same template file I'm adding another view block with {{ drupal_block('views_block:my-view-block_1', {product_type: 'clothes'}) }}.

For that block I implemented this hook.

function myModule_views_pre_view(&$view, &$display_id, &$args) {
// I want to access that product_type variable in this hook
// because I want to add a filter.
$view->display_handler->display['display_options']['filters']['field_product_type_target_id']['default_value'] = $product_type;
}

The hook is invoked, but I cannot get the value of product_type. I'm not sure that is the correct way to achieve it, but I really need to pass that value from first block to second block to filter out results. It's been 2 days I'm trying to figure this out. Any help would be highly appreciated.

1 Answer 1

2

Variables are passed from PHP/preprocess to Twig, but never vice versa.

Instead of preprocessesing the filter of the second block, replace the product type filter in the 2nd views block with a "contextual filter" , and pass product_type as contextual argument to the view. And finally - since you are already using Twig Tweak module - use drupal_view to render the 2nd views block:

{{ drupal_view('MY_VIEW_ID', 'MY_VIEWDISPLAY_ID', product_type) }}

1
  • Thanks a lot man! It worked like a charm.
    – Asad ullah
    Jun 24 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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