How to display number of nodes with current term? - drupal

I try to display my taxonomy terms through views, from my vocbulary
now it goes like this
term1 name
term1 img
I need to add count of all nodes with that term, and it will look like ->
term1 name
term1 img
count of all nodes with *term1*
so far, my idea was to display term id in additional field, after use preprocess function, and run SQL query to count all nodes with that term,
but i think there must be a easy way, looking for ideas

Create Taxonomy Views.
Add relationship to the node (in case if you want to count number of nodes).
Enable grouping (Group by)
(or "Use aggregation" toggle in D7, then set the "Aggregation Settings" for each field)
In Style Settings/Format set Grouping field to 'Term ID' or 'Node ID' (depends of your query)
Add new field 'Node: NID' and set Group type to: Count
That should give you the count
Ref : http://drupal.org/node/603868#comment-4421144

Here is a simple method available for the same :
$tid = 'Enter tid number say 5';
$nids = taxonomy_select_nodes($tid, FALSE);
// count node here
$count = count($nids);
// you can load node content here
$nodes = node_load_multiple($nids);

Related

wtforms SelectMultipleField returning only 1 selected value

im following this tutorial to implement a multi checkbox field in flask with jinja
https://gist.github.com/doobeh/4668212
It works for the most part but when I go to access the value in my code (products is the field where I used the multicheckbox) like
class CreateBoardForm(Form):
products = MultiCheckboxField(
'Select Products:',
coerce=int,
choices=[(1, 'one'), (2, 'two'), (3, 'three')])
......
if request.method == 'POST':
products=request.form['products']
print name, description, tags
print "Selected products:", products
When I print selected products I only see one value being selected. When I printed the forms object I see this python data structure ImmutableMultiDict([('products', u'1'), ('products', u'3')])
Im not sure how to get all the values for the 'products' key
you will have to use the getlist method in order to return a list of other choices.
request.form.getlist('products')

Symfony order by the content of a specific field

I want to sort data in repository based on the content of a specific field
For example i have an entity person with the fields role,fistName and lastName.
I would like to sort using ->orderBy('p.role', ???) and get a list ordered based on this order : professors then directors then teachers and at last students .
Example of my database:
Wanted result:
Ps: i can not use ASC or DESC since my sorting order is neither ASC nor DESC;
it is a custom order
Very strage scenario, you could use a CASE statement in order to assing a custom value for each fields then sort on him.
You could use the HIDDEN keyword.
As example take a look at this DQL:
SELECT p, CASE
WHEN p.role = "professor" THEN 1
WHEN p.role = "director" THEN 2
WHEN p.role = "teacher" THEN 3
WHEN p.role = "student" THEN 4
ELSE 99 END
AS HIDDEN mySortRule
FROM Bundle\Entity\Person p
ORDER BY mySortRule ASC
Hope this help

Previous/next navigation that uses ordering/sorting different than node id (n.nid)

I am trying to create "previous/next node" navigation on my nodes in order to show 2 previous and 2 following nodes in the term currently being viewed. Here is the code that displays 2 prev and 2 next nodes, but it is not taxonomy aware, i. e. it sorts nodes according to their IDs:
Prev/Next node navigation with a thumbnail in a full node
If I add a node in the term after some time, it will display this node as the last one, not as a "neighbour" of a node uploaded e.g. 3 months ago.
I have tried with "n.title", but it doesn't change anything. Ideally, it should order them either by titles or url aliases.
Thank you in advance!
it's not querying the taxonomy tables in the database query. You probably want to add a variable to the function as such dad_prev_next($current_node = NULL, $op = 'p', $tid) to pass it the term ID and then add that to your query through an inner join
SELECT n.nid, n.title
FROM {node} n
INNER JOIN {taxonomy_index} t
ON n.nid = t.nid
WHERE n.nid $sql_op :nid
AND t.tid = :tid
AND type IN ('photos')
AND status = 1
ORDER BY n.nid $order
LIMIT 1
I think that should be pretty close, then just pass that at the end of db_query db_query($sql, array(':nid' => $current_node -> nid, ':tid' => $tid));
Newer version of Previous/Next module has multiple options for sorting prev/next nodes. You can use the workaround to get thumbnails too:
http://drupal.org/project/prev_next
http://drupal.org/node/1790290

Drupal CCK field select option names, where are they?

I have a custom content type which has a field called "location" which is a long select list (100 or so items). I want to get an array of all locations which have a piece of content associated with them. The numeric value of this field is stored in content_type_[my_content_type], but I can't find anywhere in the database where the name of the values are stored. I hope that's not too confusing - just to be clear, I want to do something like this:
SELECT DISTINCT(field_location_value) FROM content_type_[my_content_type]
and then
SELECT field_location_name_or_something FROM where_on_earth_are_the_names_stored
and then do something with the two arrays to find the names I want.
Can anyone help?
Thanks a lot. Drupal 6, by the way.
If you mean select list field of CCK:
Get all location associated with current node (piece of content?):
$node = node_load('YOUR content ID');
print_r($node->field_location); // $node->field_location - this will array of values.
Get all values of that field (defined in "Allowed values"):
$content_field = content_fields('field_location');
$allowed_values = content_allowed_values($content_field); // array of values
I found this, after much trial and tribulation, in the database table content_node_field_instance, under the field's widget settings field.
This Drupal 6 code snipped will retrieve your cck field value options and put them in the $allowed_values variable as an array. Replace 'YOUR_CCK_FIELD_NAME' with your cck field name (name, not label)
$cck_field_name = 'YOUR_CCK_FIELD_NAME';
$cck_field = db_fetch_object(db_query("SELECT global_settings FROM {content_node_field} WHERE field_name = '%s'", $cck_field_name));
$cck_field_global_settings = unserialize($cck_field->global_settings);
$allowed_values = explode("\r\n", trim($cck_field_global_settings['allowed_values'], "\r\n"));
In Drupal 7 if you have a field of type List (text) then I have written the following function to return an array of options:
function get_drupal_select_options($field_name) {
$options = unserialize(db_query("SELECT c.data
FROM field_config_instance i
INNER JOIN field_config c ON c.id = i.field_id
WHERE i.field_name = :fieldname", array(':fieldname'=>$field_name))->fetchField());
return $options['settings']['allowed_values'];
}

Drupal exposed date filter: remove "-Year" from select

Just what the title says, I can't figure out why "-Year" is being added to the top of my exposed filter select box. What do I need to do to make it go away?
Select a year in the "Absolute value" dropdown when configuring your Date filter. That year will then be displayed instead of -Year. I doubt though you can actually remove -Year from the exposed select box unless you make some source code changes/additions.
Found a solution to this, and it's pretty stupid.
I ended up putting this in a custom module, and ended up both removing the label and also setting the number of years displayed based on the data that's in the database:
function modulename_form_views_exposed_form_alter(&$form, $form_state) {
if($form['#id'] == 'theformid') {
// Remove the label as the first element from the date select
$form['date_filter']['value']['#date_label_position'] = 'none';
// Find the minimum year from all of the published news nodes
$date = db_fetch_array(
db_query("
SELECT YEAR(MIN(d.field_date_value)) as 'min'
FROM {content_field_date} d
INNER JOIN {node} n
ON n.nid = d.nid
WHERE n.type = 'news' AND n.status = 1")
);
// Set the new year range in the filter
$new_min = date('Y') - $date['min'];
$form['date_filter']['value']['#date_year_range'] = "-$new_min:+0";
}
}

Resources