0

I have content type pick_topic, containing an entity ref taxonomy field topic. There is one node of this type. It defines the topic of the month. Several views blocks and pages need to only display content nodes with this topic value. Example: taxonomy topics: dogs, cats, rabbits.

The user (site manager) edits this node and selects a topic from dropdown. Say it is dogs. My view of another content type, articles, contains topic field. How can I filter the view based on the pick_topic topic value equal to the article topic field value, in this case dogs?

0

This is a very common request, to display lists of related content that share the same Taxonomy term as the node where the Views Block is displayed.

You can do that by adding a Contextual Filter Content: Has taxonomy term ID to your articles Views with these settings (only choose topic Vocabulary in your case):

Contextual Filter Has Taxonomy Term

Contextual Filter Default Provided settings

Since your Views list a different content type, it's not necessary to also add another contextual filter to remove the current node from the Views block.

If your articles Views don't have a Block display, create one now, by duplicating the one you've been editing above, duplicate as Block. Edit the Block name to something meaningful so you can easily find it on the Block list.

Now you add that Block to a region the usual way. Besides selecting a region you'll also have to choose the Content: ID Node from URL on Block settings (this comes from our Contextual filter), and on the Visibility settings (in Vertical tabs) under Content types choose Pick topic content type (I have Article ticked in image below, but you have to choose the Content type where you want the block to display, which is Pick topic in your case).

Block settings Contextual

This means that the Views Block will list article titles, but the Block will be visible on pick_topic node pages.

Without the Contextual filter the Views Block will list all articles.

With the Contextual filter (set as above) will restrict that list to only those articles that have the same term as the node where the Block is being displayed.

  • prkos, can you please explain how to "add your Block listing articles to show only on pick_topic content type. Views block lists articles content type. How can it can filter to just articles that contain the taxonomy term stored in the pick_topic content type? – dkrockville Jun 8 '18 at 18:00
  • I edited the answer with the Block clarification. Yes your Views Block lists articles, but that Block needs to be displayed on Pick topic nodes. The Block will know through the Contextual filter what Taxonomy terms are active on the page where the Block is displayed. The Contextual filter then influences the results (articles list) to restrict them to only those that share the same term. – prkos Jun 8 '18 at 18:27
  • This worked perfectly! Thanks for your help and clarification. – dkrockville Jun 8 '18 at 19:18
0

I don't know of a way to do this all in the interface, but if you add a contextual filter in the view for the topic field on the article content type, you can programmatically set the contextual argument in hook_views_pre_view.

First, load your pick_topic node like:

\Drupal::service('entity_type.manager')->getStorage('node')->load(PICK_TOPIC_NID);

Then load the field value. See How do I access a field value for an entity (e.g. node) object?

Then set the view argument.

function MY_MODULE_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  if ($view->id() == 'MY_VIEW_ID' && $view->current_display == 'MY_VIEW_DISPLAY_ID') {
    // Load your pick topic node.
    // Load your pick topic field value.
    // Set the view argument, term id.
    args[] = $my_pick_topic_field_value_tid;
  }
}

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.