Creating list of similar nodes in Drupal 7/Views 3

[亡魂溺海] 提交于 2019-12-17 21:26:58

问题


Copy from drupal.stackexchange.com hoping to get more answers here:

I face a rather common problem of displaying a list of nodes similar to current one. Similar node is defined as one having at least one taxonomy term that current node has. There are few solutions available as modules, however the modules I've found (specifically Similar By Terms) are in beta stage (as in "not working properly") for Drupal 7.

I've also found a couple of solutions based on Views and contextual filters (such as http://scito.ch/content/show-related-nodes-block-based-taxonomy-terms-views-3-and-drupal-7) or relations. I quite like this way as it saves me installing extra modules and with Views being actively developed and supported I'd prefer to use them rather than any custom solution. The problem I can't get around is how to sort "similar nodes" by the number of terms they share. For example, if I have "IPhone 4S" Node with Terms "Gadget", "Apple", and "Phone", I want similar nodes in the following order:

  1. IPhone 3 (Apple, Gadget, Phone)
  2. IPad (Apple, Gadget, Tablet)
  3. Mac (Apple, Computer)
  4. Bramley (Apple, Fruit, Tree)

How can I sort nodes in this view by the number of terms they share?

This question is about building a view, please do not suggest module or custom php-based solutions. Thank you.


回答1:


  1. Create a block view
  2. Add Contextual filter -> Content: Nid -> Provide default value -> Content ID from URL
  3. Add Relationship -> Content: Taxonomy terms on node -> specify the appropriate vocabulary
  4. Add Relationship -> Taxonomy term: Content using vocabulary as specified above -> Check off Require this relationship
  5. Turn on Views aggregation (NOTE: the arrow in the screenshot below is pointing to the wrong spot and should be pointing to 2 rows below where it is)
  6. Assuming you are listing title only, edit the title field to use the Relationship you set up in #4 above.
  7. Add new sort criteria of Content: Nid. In aggregation settings, select Count. Use relationship from #4 and sort descending
  8. Add Contextual filter -> Content: Nid -> Use relationship from #4 -> Provide default value - Content ID from URL -> Scroll down and expand "More" then check "Exclude" to remove current node from the view

** See comment by @garret-albright below for testing this in the Views UI preview. **




回答2:


guess what - there are modules for that too :D

i can recommend similar by terms giving you that view without configuration hassle.




回答3:


This is an alternative solution, related to this question. However it might not apply to every case, but if you can use it it's simpler (and safer than aggregation in my opinion).

I am going to use panels. Once you create a panel to show your entity (node or anything else) you can add multiple views and show them beside the content. one of these views is going to be our related content.
Thus panels is the only limitation. Maybe it's possible to use mini-panels and represent it as a block, I haven't tried myself.

Prerequisites

  • Views (d'oh!)
  • Panels
  • Views Content Pane: no need to download, It is in ctools module downloaded with views.
  • Page manager: Same as Views Content Pane (Optional / Highly Recommended).


The view

  1. Create your view as you normally would, but do not create it as a block or page. Uncheck both.

  2. In views configuration page create a relation to taxonomy term on the entity.

  3. Create a contextual filter to filter: Taxonomy term: Term ID.

  4. Create another contextual filter: Entity ID or Content: Nid if it's a node.

    • While creating this one, at the bottom there is a collapsed fieldset which has the option: Exclude. Check it as marked.

  5. Now by clicking Add create a new display of type Content pane.

  6. In the display configuration area (named: Pane settings) open Argument input. You should have 2 options:

    • Content: Nid source : Set it's source as From context. Set it's Required context as Content ID (if you are not using node it can be ID.
    • Taxonomy term: Term ID source : Set it's source as From context. Set it's Required context as Term ID.

Congrats! You are done creating your view. It's time for a panel (I wont explain much about creating a panel as it is beyond the scope of this Q/A).

The Panel

  1. Create a panel (or enable the node/%node default panel and add a variant).

  2. In context tab, Create a relation to taxonomy term on the entity.

  3. Click add content in one of panes.

  4. If you didn't change the view's category, it's in views pane add it. It will ask for an Entity ID and Term ID both are available now in your panels context, set them.

  5. Now when the panel is displayed, All the content with with the same term excluding the content on this page are displayed.

Views Configuration

Contextual filter

Argument input

Important P.S:

This method is tried and it works flawlessly, but at the time of writing this article ctools had a bug. There is a patch for it: https://www.drupal.org/node/2195211




回答4:


When you are configuring complex views like this one, there are some configuration tips that may help you to understand what is really going on each step:

In Structure >> Views >> Settings >> Basic:

  • Check 'Show advanced help warning' (important: requires Advanced Help module installed)
  • Check 'Automatically update preview on changes'
  • Check 'Show information and statistics about the view during live preview'
  • Check 'Show the SQL query'
  • and finally, Save configuration

Now you can preview the results from each configuration step, the SQL query associated and the fields and tables involved, so you will find easier to understand how filters and aggregations work.

Hope it helps.




回答5:


I am not sure if I fully got the gist to your question but I think node queue is the answer. Check out http://drupal.org/project/nodequeue

The module is well developed. I have used it for a client and you can use their smartqueue api too make make your list.

Hope I was helpful.



来源:https://stackoverflow.com/questions/10454507/creating-list-of-similar-nodes-in-drupal-7-views-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!