How to display the total amount of posts? - drupal

What's the best way to display in a block the total amount of posts and comments of my entire drupal website?
thanks

The quick and dirty way:
Ensure that you have PHP filter installed and available to you. Create a block with the php code
<?php
$ncount = db_query("SELECT COUNT(nid) FROM {node} WHERE status=%d", 1);
$ccount = db_query("SELECT COUNT(cid) FROM {comments} WHERE status=%d", 1);
print "Nodes: ".$ncount;
print "Comments: ".$ccount;
?>

One option is to use a View with the block display type. Views Calc can do the summing for you (http://drupal.org/project/views_calc).
Honestly, I think you will find it easier and possibly more performant to create a Statistics content type with CCK integer fields to store the initial values for the amount of each piece of information you need. Then configure the Rules module to increment/decrement the fields when you add or remove content/comments.
A third option I have not personally explored is the Statistics Pro module (http://drupal.org/project/statspro), which says it is Views-compatible.

Use Views GroupBy module ( http://drupal.org/project/views_groupby ). You can specify the filters (e.g. you want to count nodes of particular type only) and so on. It will count the nodes for you.
If your view type is comment then in a similar count can be done on comments.

Related

how to change a drupal views output

I need to create a block with eg the latest comments on the site.
when using views, concegui select the data I wanted, but the problem is that I need to edit the output of view (specify the html). I tried to make a tpl, but the fields when they get to this, are already formatted ([#markup]). also tried to make a block programmatically by accessing the fields of view, via $comments = views_get_view('last_opinions');, but so the fields do not bring content, but for example, ids (for referrals), or integers (in the case of dates), ....
basically, how to change views output?
views->your view->advance(third panel)-> Theme: Information -> click Information ->choose tpl as per your requirement and find $row which actually prints your output

how to extend default search to include the data of plugin?

i want to carry out the search in my custom plugin table .i.e. when i search for some keyword or phrase, it should search in my custom table too.
And if the data is from my custom table then it should carry out my plugin function to display data.
Don't know whether it was a good solution or not but i did it by myself. See below :
i added search.php to my default theme
after the if condition of have_posts and before else part mean in
between i inserted my code
elseif (pdfsearch_result($_GET['s'])):
$search_pdf = mysearch_result($_GET['s']); // my search function to search for my plugin data
and then did formatting and achieved the desired result. Hope this may help someone, but do post if there is better way of doing this.

Drupal - Views. Setting a filter programmatically

I hope this is not a stupid question I have been searching for most of the day!
I have a Content Type (Documents) which simply contains a title, file and a category. The category value is required and is 'powered' by Taxonomy.
I now wish to create a view which will display these documents grouped and titled by the taxonomy term.
Using my limited Drupal knowledge I intent to iterate through the relevant terms IDs (using taxonomy_get_tree($vid)) and then render each view accordingly.
To do this I have been hoping to use this snippet.
view = views_get_view('documents');
$view->set_display($display_id);
$filter = $view->get_item($display_id, 'filter', 'field_dl_category');
$filter['value']['value'] = $filter_value;
$view->set_item($display_id, 'filter', 'field_dl_category', $filter);
$viewsoutput = $view->render();
But this is not working; when I query the value of the $filter ($view->get_item($display_id, 'filter', 'field_dl_category')) I get null returned.
Might this be that my filter name is not the same as the CCK field name?
I am using Drupal 7.
Any help much appreciated, I am running out of ideas (and time).
I finally managed to get this working but I took a slightly different approach.
I changed my view and added the relevant contextual filter and then used this function views_embed_view to get at my required results.
If this helps! this is my solution:
$display_id = 'default';
$vid = 7;
$terms = taxonomy_get_tree($vid);
foreach($terms As $term){
$content = views_embed_view('documents', $display_id, $term->tid);
//now we see if any content has been provided
if(trim($content) != ''){
print "<h3>" . $term->name . "</h3>";
print $content;
}
}
In my case the trim($content) returns '' with no data as the view template has been edited, this might not be the case for all.
I am a very new Drupal developer so I'm sure there are much better ways of doing this, if so please do post.
I am going to go ahead and assume that you want to show, using Views, a list of document nodes grouped by the category that they have been tagged with.
There are two (of maybe more) ways by which you can do this in Views 3:
(a) Choose a display style that allows you to select a Grouping field. (You could try the table style that ships with Views by default). Suppose you have properly related the node table to the taxonomy_term_data table through a Views relationship, you could choose taxonomy_term_data.name as the grouping field.
Note that this grouping is done before the view is just rendered. So, your query would just have to select a flat list of (content, tag) pairs.
(b) You could also make use of the Attachment display type to achieve something similar. Show the used categories first in a list view clicking on which will show a page (attachment) with all documents tagged in that chosen category.
To understand how to do (a) or (b), turn on the advanced_help module (which is not a Views requisite but is recommended) first.
For (a), read the section on Grouping in styles i.e. views/help/style-grouping.html and
For (b), read the section on Attachment display i.e. views/help/display-attachment.html
A couple of things about your approach:
(a) It will show all terms from that vocabulary irrespective of whether or not they were used to tag at least one document.
(b) views_embed_view() will return NULL even if the currently viewing user does not have access to the view. So, ensure that you catch that case.
Here's an alternative:
$view = views_get_view('view_machine_name');
$view->init_display('default');
$view->display_handler->display->display_options['filters']['your_filter_name']['default_value'] = 'your_value';
$view->is_cacheable = FALSE;
$view->execute();
print $view->render();
I know you can probably set this using some convoluted method and obviously that would be better. But if you just want a quick and dirty straight access without messing around this will get you there.

Drupal view to list nodes and comments

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).

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