Creating list of similar nodes in Drupal 7/Views 3 - drupal

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:
IPhone 3 (Apple, Gadget, Phone)
IPad (Apple, Gadget, Tablet)
Mac (Apple, Computer)
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.

Create a block view
Add Contextual filter -> Content: Nid -> Provide default value -> Content ID from URL
Add Relationship -> Content: Taxonomy terms on node -> specify the appropriate vocabulary
Add Relationship -> Taxonomy term: Content using vocabulary as specified above -> Check off Require this relationship
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)
Assuming you are listing title only, edit the title field to use the Relationship you set up in #4 above.
Add new sort criteria of Content: Nid. In aggregation settings, select Count. Use relationship from #4 and sort descending
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. **

guess what - there are modules for that too :D
i can recommend similar by terms giving you that view without configuration hassle.

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.

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
Create your view as you normally would, but do not create it as a block or page. Uncheck both.
In views configuration page create a relation to taxonomy term on the entity.
Create a contextual filter to filter: Taxonomy term: Term ID.
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.
Now by clicking Add create a new display of type Content pane.
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
Create a panel (or enable the node/%node default panel and add a variant).
In context tab, Create a relation to taxonomy term on the entity.
Click add content in one of panes.
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.
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

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.

Related

Views and Categories...?

it's about this page: http://veagues.com/
Hey guys,
I've made a block-view with drupal that let's me sort my videos by a certain category, in this case "funny","epic","plays", "contest"...
The problem is: When you sort it by the certain category and click on a video and continue to click the "next" button, you will go threw all videos, instead of only the videos of the category...
My Question is: How can I put a filter on the nodes when i apply another category on the bottom block?
Thanks already!
If you're using Views, you should be able to filter the main block by the same taxonomy term.
Try adding a Contextual Filter to that block too. If there's a taxonomy term ID in the URL the block gets filtered by that Term ID and if there's no result, the block shows all the videos.
EDIT -----------------------------------------------------------------------------------------
You may need to try & understand what goes on when you make a selection in Drupal.
1/ The taxonomy term page will list all the content affixed with a specific term. The term tid can be seen in the URL, unless an alias have been created. A block can retrieve that term id, whether there is an alias or not.
2/ The node page will give you access to a specific node, but any block on that page should be able to access the term id of that specific node.
If you know where you are, it becomes easier to retrieve the info you're looking for. On the other hand, you may have to reconfigure a specific page in order to add the necessary extra block...
That can be done with templates but you could also have a look at Panels.
The Panels UI makes it easier to configure the page you're looking for without having to mess with code or templates, or both.
There's a lot of free video tutorials out there, search for Views & Panels.
If you want to have a look at preprocess functions, you will definitely need to download the Devel module...
...but have a look at these sites first:
http://codekarate.com/
http://drupalize.me/
http://buildamodule.com

Drupal Views & URL Alias Override Issue

I have a taxonomy vocabulary called 'catalogue'. Within this, I have levels of terms. Some have sub-terms, some have nodes.
I have two views to handle these: One to show sub categories, one to show nodes, each with a filter applied based on the URL. If I go to sub-category/category-name, it would show me all the sub-terms in category name. If I go to product-category/category-name, I get a list of related nodes.
With no aliases, the view works properly on sub-category/%. The problem with this is that the generated links for the sub-terms aren't to the view, they're to the term page.
If I create aliases, it overrides the view and shows no results on sub-category/% pages and default node teasers on product-category/%.
Can I use aliases with views in this way?
Sounds like you want to re-write the output of the Field instead of creating aliases. If you're not familiar with that particular feature in Views, see the screenshot below from http://highrockmedia.com/blog/rewriting-drupal-views-output-custom-theming-css
Note that instead of creating a <div> wrapper, you can create your own link as needed. Hopefully this will help you get started in the right direction.

drupal replacing default nodes with views page

I have created a page view using field style to display a list of teasers as I want. fields configuration in views allows us to link the field to it's node. but what if we have created view for node detail page and want field to link to it's relevant view.
I had the same question, and after a bit of research this is what I've concluded:
The views module isn't intended to replace a default node view. I say this because of the level of difficulty involved in doing this and the lack of information on how to accomplish this. Ryan Weal has posted a way to accomplish this by editing your node template that doesn't look too difficult to accomplish, and here is the link.
However, it seems that a more popular solution, especially if you are like me and don't like to get into editing theme files. You can use the Display Suite module to effectively reformat the default node content as you would like.
I'm not sure if I understand you properly, but it sounds like you are wanting to display a view in a page view of a node?
In order to do this, you could:
create a small module, using hook_nodeapi() or one of the D7 replacements for this function in order to insert the output of the embedded view into this page's content, by conditionally adding a $content element when the node is of the appropriate id
or (easier, but requires allowing input type PHP) embed the view right on the page.
http://thedrupalblog.com/embedding-view-drupal-6-using-views-embed-view for information on embedding views

Drupal Views of nodes to show node comments

I've encountered a Drupal problem: I'm using the Views module for rendering nodes of a kind, based on the user id of it's author (it is a Content Profile actually). I want the view to show the comments for the node, just like in node/%. I could not find any option in views or any relevant module. Am I in the wrong direction and should reorganize stuff for this...?
Any ideas, how can it be done?
Regards,
Laci
Using views is really not the best plan of action. You should instead create a node template in your theme and customize it. If needed you can put some logic in a preprocess function. It requires more coding but will get you where you want.
If you use view node display type
Check in it's settings show comments
if you use view fields display type
Use relationship to comments and select fields you need and theme them
I know this is old request, but I was just struggling with the same issue and came across this post. I thought it'd be helpful to share my solution.
I'm using Drupal 7, with Views 3 and Display Suite.
In your view, choose the display in question.
Under Format, click the first link to the right of 'Show'.
Choose 'Content' (or 'Display Suite' in my case).
Click 'Apply'.
On the next screen, you'll have the option to 'Display comments'. Check this box and save your view.
You should now see the comments displayed under each item in the view.
You could create a second view (with URL e.g. /comments/% where the placeholder will be the node ID, and not the comment ID) that lists comments for a given node, with a contextual filter to only show them based on the NID in the URL.
Then, add that view to the footer (as a 'view area') of the single-node view you've already got.
There's some tweaking required for layout (inline fields etc.) but the basic structure should work.

attach multiple images to a taxonomy term

I have installed "Taxonomy Image" module.
but i can not find a way to attach multiple images to one taxonomy term
my drupal version is 6
As a side note to what Henrik answered, an alternate way to implement what you want without having to code a single line could be:
Create "image nodes" for each term of your taxonomy.
Associate each image node to the right term of the taxonomy (here's where it get interesting: you can create more than one image node per term and can also use the same image node for different terms).
Create a wiew that takes as arguments the taxonomy term/terms and show all the image nodes tagged with that term.
If you already know the views module you probably realise how easy it is. If you are new to Drupal and do not know it yet, then I recommend you take a close look at it together with the CCK one: they are big and require some time to be "grokked" (neither so much, though!) but every second you spend studying them is worth it!
Using views also gives you a lot of flexibility as you can theme easily theme your output in tables, lists, grids, add or remove associated fields, and even put extra filters or expose them to the user in separate blocks...
HTH!
As far as I can see, the module does not provide the option to attach multiple images to one term. From the features list on the project page:
Allows one-to-one term-to-image relationships.
Allows many-to-one term-to-image relationships.
So one term can have exactly on image, or multiple terms can have the same image (for 'inheritance' of a parent term image), but no 'multiple images per term' option.
I have not delved into the modules code to look if there is an easy way to change this, though. So far, there seems to be no request for this feature either.
The easiest way is to first install the following modules:
Views Bulk Operations
Administration Views
After enabling both modules. Go to Admin >> Structure >> Views and edit the Administration: Files view. Add the desired taxonomy field.
Go back to the Administration >> Content >> File and there you`ll see now operations, select change value and the desired images you wish to have the same taxonomy term. Click Execute >> Your taxonomy field. Select the term or terms and click next. That is it.

Resources