0

I have News content type on my Drupal 7. With views I want to show latest news within specific taxonomy. So each News content type has taxonomy field where I can set news for example into "Cars" category.

Now I have News Lift content type where I have entity reference field called news_type that is linked to same taxonomy as is my News content type. When I make a new news lift and I set I want to show post from "Cars" category. On this just created page I want to now show that latest post in that category using views. In Views my current set up is this:

Filters (published & News -content type) Relationships (Entitry referecing news_type field).

But at the moment it just shows the latest post. It ignores the relationship about the category. What's wrong. Do I need contextual filter too?

  • if your plan is to attach the view to the node (in a block for example) then yes, you need a contextual filter or some other method of telling the view what data to pull. do you use the pathauto module, or something similar? if not, it would quite easy to integrate a contextual filter using the nodeID already present in the url – Geoff May 4 '15 at 2:25
  • Yep, I'm using path auto. – user995317 May 4 '15 at 3:11
0

Download the Views PHP module:

This module allows the admin account (user 1) to add fields, filters and sorts to views which use PHP code. It also supports additional accounts when the core PHP Filter module is enabled.

In Views:

  • create a View of News Content Type, as a block and set it up to show all entries for now in the format and layout you desire
  • add a contextual filter for the Category field
  • Provide a default value, PHP code:

    $node = menu_get_object();
    return $node->field_category['und'][0]['tid'];
    

The first line retrieves the page object (which should only ever be a News Lift node if the block is set up properly), then returns the term ID associated to the current node as a contextual filter to the view. This assumes a single language site.

  • Under /admin/structure/block set the view-block to show where you want on the page, limit it to show only on the content type News Lift

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.