0

I have a content type called 'article_landing' and a content type called 'article'.

They both have an entity reference field called 'field_focus_area'. I'm attempting to create a view that appears as a block on an article landing node and only displays articles that have the same field_focus_area as the current node. I'm having trouble figuring out how to do this though. I thought this would need to be accomplished with contextual filters but I'm not getting any results. Below is the method I was using.

enter image description here

I also tried using regular filters and using tokens like [node:field_focus_area] but that didn't work either.

Is there a way I can use fields of the node that a view block appears on inside filters in the view?

2

Niall Murphy's concept is correct but there are some differences between how it's performed in D8 and D7. Here is the code I ended up using which was successful.

function my_module_views_pre_view(&$view, &$display_id,&$args){
    if($view->id() == 'news'){
        if($view->current_display == 'block_2'){
            $current_node = \Drupal::routeMatch()->getParameter('node'); 
            if(!empty($current_node)){
                $focus_area = $current_node->field_focus_area->entity->id();
                $args[0] = $focus_area;
                $view->setArguments($args);
            }
        }
    }
}
0

EDIT: For correct implementation, OP has added the proper way to do this in D8.

That contextual filter will show nodes that reference the current node, so it's not what you're looking for.

As far as I know, this requires a custom module and this is what I do with D7 but I assume it's the same in D8 since the function didn't change much.

For this example, keep that Contextual filter from the screenshot, but click Hide View instead of Provide default value.

MYMODULE.info

name = Contextual Filter: field_focus_area
description = "Provide Contextual Filter for block."
core = 8.x

MYMODULE.module

<?php

function MYMODULE_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  // Check for view 
  if($view->name == 'MYVIEWNAME') {
    // Explode url to get /node/xxx
    $current_args = explode('/', current_path());
    // Check that the path has xxx
    if (isset($current_args[1])) {
      // Load node
      $current_node = node_load($current_args[1]);
      // Send the node's field_focus_area to the view
      if (isset($current_node->field_focus_area['und'][0]['target_id'])) {
        $args[0] = $current_node->field_focus_area['und'][0]['target_id'];
      }
    } 
  }
}

That will probably work. I haven't worked with D8 much and the function is slightly different. In D7, it's hook_views_pre_view(&$view, &$display_id, &$args){

I assume the ->field_focus_area['und'][0]['target_id'] part is the same but if not, you can find it using the Devel module.

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.