Drupal view to list nodes and comments - drupal

I want to create a view that shows the latest posts in a forum and also any latest comments. The comments and the posts would all show in the same view. Is it possible for me to do this?
Thanks in advance,
Ben

In essence: no. Views requires you to choose one main resource in the first step: you there (amongst others) choose to go with either nodes, or comments.
However, with some (ugly) configuration, you can load the comments that go with nodes. Each result would look like, Node - Comment, e.g.:
Can I have cheesburgers - First!
Can I have cheesburgers - No, I was first!!!111oneone
Can I have cheesburgers - LAME.
and so on. With some styling, you can then get it to show comments and nodes in separate rows.
However, this is ugly and hackish. My advise: write a simple module that either exposes a block, or a menu+page, and do two simple (and light) queries on the database: SELECT nid, title, ... FROM {nodes} LIMIT 10 and SELECT nid, name AS title, ... FROM {comments} LIMIT 10 then mix these two up. Or, with some (more complex) SQL magic, you could even join the two tables and create fancy results that e.g. order by created date of either nodes or comments.

With Drupal 7 you can add a relationship Last comment and then add that field to the view.

One idea I've seen is to use Views Custom Field to "attach" a wholly separate view with PHP code. See comment #4 for the code (in a request for this feature in Views. for a code example).

Related

Drupal related articles view with list of nodes to exclude from results

I'm trying to create a list of related articles in Drupal, based on shared tags. I've used this solution and it works great:
Creating list of similar nodes in Drupal 7/Views 3
However, I wish to expand on this and exclude certain nodes from the related view which are not relevant. I have a node reference field (field_exclude) for the article which allows the user to select which nodes to exclude.
I just need to update this related view to exclude any node ids found in field_exclude. Any idea on how I update the view to add this functionality?
Thanks.
Add a Filter Criteria for the field_exclude and there set the "Not empty" value or any other value that wil do the job.
Another solution here is to use the NodeQueue module where users can add nodes in lists/views.

Drupal 6 & Views 2 - DISTINCT field

I'm using the Feeds module to import lots of Feed Item nodes. Due to a malformed feed file, I'm getting lots of duplicates. I'm using a View to display these nodes, and need to be able to add a DISTINCT filter on the "Node: Post Date" field, so I only get 1 result for each post-date.
I will also look into tackling the problem at the source so to speak (I don't want to have all those duplicates in the first place), but this is an interesting issue in itself - I can't find a way to add a DISTINCT filter on a field other than the Node ID (which has it's own option in the View's Basic Settings box).
I found a great article on a good way to alter the SQL queries that are generated from views before they get executed: http://echodittolabs.org/blog/2010/06/group-views. I used this to basically suffix a GROUP BY clause to the end of the query (in a really nice, clean and versatile way).
As an aside, I also found a way to tackle the issue of importing lots of duplicate feed items, the details of which are here: http://drupal.org/node/661314#comment-3667228. It adopts quite an extreme approach (deleting all items before each update), but this is the only solution for some nasty malformed feeds.
I was holding out for some undiscovered feature of Views that let you do this, but I don't think there is one - maybe in the next version ;)
There are two option to solve this:\
apply this patch
OR
hook_views_query_alter => just paste
$query->distinct = 1;
$query->no_distinct = 'views_groupby';
I guess you have two options: either put some logic in the view template file to skip the duplicate items or implement hook_views_query_alter() to change the query used by the view, adding the DISTINCT clause.
We found this issue in drupal 6.x view - had 7 of 150 items duplicated one or twice. No idea why. Issue only appeared for anonymous users. Luckily, views 6.x.2.16 provides a 'distinct' setting under the basic settings, I set it to Yes and got rid of the duplicates.

Combine two views into one view

I have two views that I would like to combine into one.
The first view shows all items of X where company ID = Y. This is to give preferential sort to the client first, and then everyone else.
So I created a second view, all items of X, where company ID != Y.
I created it as an Attachment to attach to the first view, but I don't think I got the intended result.
How can I combine these views so the first view results are listed first, and then the second view is too, using the same pager, filters, and arguments?
Is there any way of achieving this without programming it?
From a MySQL point of view, the order-by-field syntax would be the appropriate way to handle this. Example:
SELECT * FROM tickets ORDER BY FIELD(priority, 'High', 'Normal', 'Low');
It would be great if there is a module that add this kind of functionality to Views, but AFAIK, it does not exist.
If you want to solve this without programming, I think you can use the rules module to automatically set the 'sticky' checkbox on nodes where company ID = Y. With that in place, you can order the View on the sticky value.
Along the lines of the 'sticky' idea, if you didn't want to override that, maybe you could add a checkbox field to the company type -- isClient. Make it false for everyone except the client, and sort by that.
I haven't done this, but maybe you need to create both versions as attachments, and attach them both to another display...?
for drupal 5 there was views union. Someone started something for D6, but I don't know how far they got.
http://drupal.org/node/275162
Create the second view as an attachment and attach it to first.
Set all Inherit arguments, Inherit exposed filters and Inherit pager to Yes.
how is the client parameter passed to the view? as an url argument? if so, you can create your second view like i outline here and then select the exclude the argument option on the appropriate location.
usually the easiest way to achieve this is with a small hook_query_alter, but that requires a small amount of programming.
A little bit later... but I've found a better solution using only the Views module:
Create a Block View with that shows the first list that you need ("all items of X where company ID = Y")
Create another View that must be a "Page view" with the second list (all items of X, where company ID != Y)
In the "HEADER" settings of this second view, click "Add" and select "Global: View area".
In the "View to insert" list, select the first you have created (and check "Inherid contextual filters" if you are using it)
And that's it!

Sorting grouped nodes by taxonomy term

Ok, here's the problem:
I have a list of contacts, which i have created in views, that are grouped by taxonomy terms like so:
(term:) Staff:
(node:) John Doe
john#doe.com
(node:) Jane Doe
jane#doe.com
(term:) Management:
Fred Doe
fred#doe.com
and so on...
As it is now, i have no idea what decides the order of the taxonomy terms (ie: why is the 'Staff' nodes coming before the 'Management nodes').
So what i need to do is to be able to sort the order of the terms, and also the order of the nodes in each 'category' (or what you would call it).
I have tried to sort the terms by weight, but the only thing that happens is that i get duplicated nodes output, and nothing happens with the order of the actual terms.
As for the order of the nodes, i was thinking that maybe a hidden CCK-field with some sort of weight, but i dont know. But the biggest problem is still the order of the categories.
If anyone has an answer to this it would be very helpful.
Thank you.
EDIT:
Strange, i tried that before i asked the question, but now it seems to work. However i still get duplicated nodes when i sort by taxonomy weight, for some reason. I really need to get rid of those. Heres how my view setup look, if its any help:
Fields: taxonomy=all terms (limited to one vocabulary)
image attach content
Sort criteria:
Taxonomy weight:descending
Filters: Taxonomy term id(with depth) // to filter out what page it belongs
Node type : contact
node published : yes
dont know if that information helps at all
/Anders
The solution is simple, in views you can sort your result by the taxonomy term. You have 3 options as default.
From the views interface:
Term Taxonomy terms. Note that using this can cause duplicate nodes to appear in views; you must add filters to reduce the result set.
Term ID The taxonomy term ID
Taxonomy Weight The term weight field
The sorting in views, is located in the top right corner, and gives a wealth of options as to how you want to sort your results.
Edit:
Duplicates is a known problem with taxonomy terms. The problem is that if a node has two terms that fit it will be included once for each term. When you use the taxonomy term filter, you can reduce duplicates, which should fix your problem:
http://grab.by/16vw
I seldom have sort problems with views, but I have to admit it's not something I ever really focused on. Here's a short list of things you might wish to check. If that doesn't solve, it would be great if you could provide some more detail on your settings and what appears to be the default sorting in your current configuration.
How did you set the sort criteria in the views UI? You have basic settings available there (top right of the UI panel). See below for some screenshots that should help you finding your way around the configuration.
How did you set your taxonomy term order (accessible from somewhere similar to: http://example.com/admin/content/taxonomy/3 - where the number is the taxonomy ID).
Here is some more information on sorting capabilities of views.
Screenshots on how to configure sorting
NOTE: In this example I show how to sort nodes according to whether they are published or not, but the procedure applies equally for taxonomy terms.
In this view I already set up some sorting, add yours by clicking on the + button
alt text http://img15.yfrog.com/img15/7118/screenshot005vy.png
Select what kind of content you want to sort
link text http://img3.yfrog.com/img3/2341/screenshot006jkz.png
Select the information you want your content to be sorted by
alt text http://img3.yfrog.com/img3/4816/screenshot007nt.png
And finally select the direction of sorting!
alt text http://img37.yfrog.com/img37/9806/screenshot008ah.png
Now you should be good to go! :)
Hope this helps!

Drupal Custom CCK field with multiple child fields

Is there a way of creating a composite field that can have multiple values, with each value having another group of composite values?
E.g. we want to have this structure at the end:
Group 1 (unlimited number of groups)
Child field (unlimited children for each group)
Child field
...
Group 2
Child field
Child field
...
...
Is this possible at all for a custom module that defines a CCK field? If so, can someone push me in the right direction?
this is a very know and debated issue in the drupal world.
this feature is called cck "multigroup" and it looks it's pretty difficult to implement.
there are a lot of posts in the drupal forum about this, i suggest you to start here:
http://drupal.org/node/494100
it's a kind of "hidden" feature in the cck module. looking in the module directory, you will find instructions here:
cck/modules/content_multigroup/README.txt
Edit: Ongoing work on the multigroup module has moved to the experimental
CCK 3.0 branch.
in the meantime you can try to deal with it using the flexifield module
(but it's kinda hackish, i won't use it in production)
I ended up creating my own "Cost/Product" CCK compound field based on this excellent tutorial with example modules: http://poplarware.com/es/articles/cck_field_module
I haven't figured out yet how to pull out a specific sub-field, such as cost, in Views. I only get the entire ": $" compound.
For Drupal 7+, you probably want to check out the Field collection module, or Field group if multiples aren't necessary.
This issue is pretty old, but I just happened across it. I would think the way to handle it for now would be with a second cck type for the children, and the groups as a node_reference field. So you'd have:
MasterType
group field
unlimited per node
each one, a reference to a ChildType node
ChildType
child field
unlimited per node

Resources