0

I created a classified ad website with different content type like vehicle, clothing, mobile phone, computers, etc. Within this content type has a field called location. A location field is an address of the item posted on my website like cities.

My question is on how can I filter location plus the content type. Currently, when I click the location, all of the content type article shows.

Here's an example of what I want:

Select article about vehicle, within the vehicle article is a field called location. When I click the location of the item, the result should include only items related to the previous content type, that is, vehicle content type. Location field is connected to Location taxonomy I created.

BTW, I already installed views and it is working fine.

0

I think you need to edit the location links to include a second "parameter" so it could look like:

yoursite.com/yourview?location=yourLocation&articletype=yourArticle

For this you would need to implement some hooks. Maybe you can start with hook_preprocess_node.

What I would do is to:

  1. Use the said hook
  2. Get the location terms available for the current viewed node
  3. Construct the URL based on the term data and content type the way I want it to look.

Then in your view, you need to add just two contextual filters that would be those fields. The location term reference field and the content type.

EDITED

Node view and term link update

  1. Created a new content type name Article 2 (Machine name: article_2) to follow your structure of content types having Vehicle, Clothing and etc. So mine is just Article and Article 2. My content types have the Term reference field of the Tags vocabulary w/c in your setup is the Location.
  2. Added terms into the Tags vocabulary: Term 1 and Term 2.
  3. Created a simple custom module using hook_preprocess_node() w/c looks like:

function yourmodulename_preprocess_node(&$vars) {
  // Check if your term field is being rendered in the node view
  if (isset($vars['content']['field_tags']['#items'])) {
    // Loop through all terms present in the node
    foreach ($vars['content']['field_tags']['#items'] as $index => $info) {
      $node_type = '/' . $vars['type'];
      // We append the node type to the URL of a term so that 
      // it can be used as a contextual filter in the term view.
      $vars['content']['field_tags'][$index]['#href'] .= $node_type;
    }
  }
}

Remember to do a Flush Cache since were adding a new hook function into the system. Now when you view a node, check the term link(s) listed and you will see that the URL structure is not like http://yoursite.com/taxonomy/term/2/article

Term View

  1. Clone the Taxonomy Term view (So that we wont be changing the original)
  2. Add a second wild card argument in the Path w/c looked like taxonomy/term/% to taxonomy/term/%/%. The second argument would be for the content type.
  3. Under contextual filter add the Content: Type field following the settings in the image.

enter image description here

  1. And then place the Content: Type contextual filter at the second row to match the URL structure w/c would look like:

enter image description here

And that's it. A simple sample result based on this URL http://localhost/drupal/taxonomy/term/2/article would look like:

enter image description here

So it's filtering the right node that has Term 2 as its Tag(my setup)/Location(your setup) and only the Article content type.

My test data are:

  1. Article content type: Article 1, Term 1
  2. Article content type: Article 2, Term 2 (This is the result in the latest screenshot)
  3. Article 2 content type: Article 3, Term 1
  4. Article 2 content type: Article 4, Term 2

Issue

So far I haven't figured out how to use this style with Pathauto so the URL would look better for example, www.mysite.com/illinois/chicago/article.

  • Thanks for the suggestion. But I'm still cracking my head on how to do this. In my template file, I have the following code to display the value of location field <?php print render($content['field_location']); ?>. I don't know what to do next. The value of that field will output something like www.mysite.com/illinois/chicago. So are you suggesting I will add the value of the node type to something like www.mysite.com/vehicle/illinois/chicago? Where vehicle is the name of the content type. – jaypabs Apr 4 '15 at 16:13
  • Oh! If i may ask, what is the type you're using for the location field? Is is just taxonomies? For the link, yes, it could also be just a parameter though like using the "?field=value" but your structure may do also. – Leolando Tan Apr 5 '15 at 7:51
  • It's a Location field connected with from a Location taxonomy. Can you give me an idea on how to do this? How can I add a parameter like ?type=vehicle and pass it to Views contextual filter? – jaypabs Apr 5 '15 at 10:31
  • I updated my answer of the rough work I did. – Leolando Tan Apr 5 '15 at 16:24
  • Thank you very much. I created the module and it works perfectly except for the simple hierarchy module. If I use "simple hierarchy" under the manage display >> format, the module does not work. But if I don't use it, it works great. Without simple hierarchy format the url turns into mysite/taxonomy/term/1181/vehicle. But if I use simple hierarchy format, the url output to something like www.mysite.com/illinois/chicago. And it doesn't turn to www.mysite.com/illinois/chicago/vehicle. Any idea? – jaypabs Apr 6 '15 at 1:07

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.