I'm using the drupal vote up down module and a module which overrides the node display, leading me to have to put the vote up down into a block (as opposed to having it be automatically rendered).
Here is an issue with a snippit on how to do this in 1.x http://drupal.org/node/544354
Unfortunately 2.x is totally different. Does anyone know what function I should use? Thanks.
It's depend where to you want to show: node, comment, term?
if node, see vote_up_down\vud_node\vud_node.module file:
function vud_node_nodeapi().
Copy to your block one of these themed function, like this:
...
if ((arg(0) == 'node') && is_numeric(arg(1))) {
$tag = variable_get('vud_tag', 'vote');
$widget = variable_get('vud_node_widget', 'plain');
$output .= theme('vud_widget', arg(1), 'node', $tag, $widget);
}
...
print $output;
Related
I've successfully created a custom view template for my Drupal 7 site but am having issues adding attributes to the content which is outputted. I've searched high and low for the answer to this but to no avail.
I have a view called: views-view-fields--homepage-articles.tpl.php
I am printing content like :
$fields['title']->content
This is fine and expected, and outputs:
Title
But I want to add classes to it - how? I'm thinking I need to write a hook, but I cannot find this documented anywhere. At the moment my solution is a string replace:
<?php print str_replace('<a ', '<a class="brand-blue uppercase nodecoration"', $fields['title']->content); ?>
As you can imagine, this is not a satisfactory or long-term solution.
Many thanks!
You should be able to add the classes to the field using template_preprocess_views_view_fields().
Edit: Couldn't do it the way I thought, but you can overwrite the output of the field like so:
function MY_THEME_preprocess_views_view_fields(&$vars) {
$view = $vars['view'];
if ($view->name == 'node_listing') {
foreach ($vars['fields'] as $id => $field) {
if ($id == 'title') {
$field_output = l($view->result[$view->row_index]->node_title, 'node/'. $view->result[$view->row_index]->nid, array('attributes' => array('class' => 'brand-blue uppercase nodecoration')));
$vars['fields'][$id]->content = $field_output;
}
}
}
}
Have you tried using Semantic Views? https://drupal.org/project/semanticviews - that way you can override the classes within the UI instead of template files, may suit your needs better.
I’ve been using Views to selectively returned nodes, but right now I want to return my nodes and use the Taxonomy term as a group header. I can't see anyway to get Views to do this for me, other then create multiple views on one page.
So I thought I'd right a module. I've written the SQL to return the correct nodes, but I can't work out how to send them to the themeing engine properly. I would like some advice on how to go about this, my tutorial book has examples of building a list as shown below.
foreach ($result as $row2) {
$items[] = l($row2->title,'node/'.$row2->nid.'/edit');
}
return array('#markup' => theme('item_list',array('items' => $items)));
now I want to return my nodes attached image file in Teaser mode, and the title of the node, plus (and I dont want to get ahead of myself) I may also want a couple of the addition node fields appended to the title. Should be easy right? I can't work it out at all.
I have wrangled my way around it (a bit) by using what I'm sure is a non drupal method which looks a bit like this, trouble is I can't get my output to work with ColorBox module, so I'm thinking if I can get official Teaser node data out it might work better, and i'd feel better knowing I was doing things in a drupaly way :)
foreach ($result as $row2) {
$items .= '<img title="'.$row2->title.' '.$row2->fielddata.'" alt="'.$row2->title.'" src="http://localhost/theme/sites/default/files/styles/thumbnail/public/field/image/'.$row2->filename .'"></a>';
$items .= '</div></div></div></div>';
}
return array('#markup' => $items);
Really appreciate any time you take to help me out and thanks in advance.
The following code should help. If you don't already have it, install the devel module, it gives you a wonderful function called dpm() which will print the contents of an array/object to the messages area.
// Get some nodes ids
$nids = db_query('SELECT nid FROM {node}')->fetchCol();
// Load up the node objects
$nodes = node_load_multiple($nids);
// This will print the node object out to the messages area so you can inspect it to find the specific fields you're looking for
dpm($nodes);
// I guess you'll want to do something like this:
$terms = array();
foreach ($nodes as $node) {
// Load the taxonomy term associated with this node. This will be found in a field as this is how taxonomy terms and nodes are related in D7
$term = taxonomy_term_load($node->field_vocab_name['und'][0]['tid']);
// Set up the array
if (!isset($terms[$term->name])) {
$terms[$term->name] = array();
}
// Create some markup for this node
$markup = '<h3>' . l($node->title . ' ' . $node->field_other_field['und'][0]['value'], "node/$node->nid") . '</h3>';
// Add an image
$image = theme('image', array('path' => $node->field_image['und'][0]['uri'], 'alt' => $node->title));
$markup.= $image;
// Add the markup for this node to this taxonomy group's list
$terms[$term->name][] = $markup;
}
// Make up the final page markup
$output = '';
foreach ($terms as $term_name => $node_list) {
$output .= '<h2>' . check_plain($term_name) . '</h2>';
$output .= theme('item_list', array('items' => $node_list));
}
return $output;
Hope that helps
You can get views to group the returned nodes by the taxonomy term for you. Assuming you are using a field view type, just add the taxonomy field and then where it says Format:Unformatted list | Settings click on Settings at the right hand side and choose your taxonomy field as the grouping field.
Note: if you are not using a field view type, or if you are not using unformatted list then the instructions will be a variation of the above.
I'm at my wits end with Drupal 6 right now, and I'm hoping a second pair of eyes might be able to point out some syntax sublety I was missing earlier this morning.
Lets say I have a theme called my_theme.
This is a theme that subthemes from Ginkgo, a theme which which in turn subthemes from
Rubik, whith in tun subthemes from Tao, a fairly common CSS reset theme.
I'm trying to understand how to declare my own theme functions, so I can
clean up the mark up on a fee page I'm workign on for site.
Now my understanding of theming is along the lines of ' if register a
theming function with hook_theme, passing it into an array, with the
function's name, you'be able to call it in future with
theme_function_name from within your theme.
For example, if have a module called my, I'd immplent the hook_theme
like below, defining the name of the function, and defining which
argument should be passed into it:
<?php
function my_theme() {
$items = array();
$items['randomtext'] = array('arguments' => array('element' => NULL));
$items['button_links'] = array('arguments' => array( '$links' => NULL, '$attributes' => NULL));
return $items;
};
Now I have the functions registered, I just need to implement them like
this somewhat contrived example:
function theme_randomtext($element) {
$output = ' <h1>This is some very random text with' . ' this text concatenated: ' . $element . '</h1>';
return $output;
}
I can now use this new function by calling
<php
print theme('randomtext', 'some arbitrary words')
?>
And expect the following content to be returned:
<h1>This is some very random text with this text concatenated: some arbitrary words</h1>
I'm getting nada.
And I don't know why. When I look at the theme registry, I see my
function listed as my_randomtext - I've tried calling both of these
options, in case I should have been adding the theme prefix:
theme('my_randomtext', 'getting desparate');
theme('randomtext', 'really losing my rag now');
Still no luck.
Why might these theme functions not be working? And what advantage does
this give you over simply declaring a function like so in a theme?
function manual_random_text($element) {
$output = ' <h1>This is gives me everything I aleady need,
'without relying on the weird themeing sytem. It's perfect for' . $element . '</h1>';
return $output;
}
Ah, after a bit of distance from the problem, I see the issue.
For this to work, I need to add the prefix of the theme I declare the function in, to make it available to me for use.
So if my module name was my, the desired theme function name would be:
function my_random_text($element) {
$output = ' <h1>,
'This is finally outputting content. Now I can: ' . $element . '</h1>';
return $output;
}
The thing is, if I'm creating an override to a theme function that doesn't already exist, like random_text, there are no functions for it to override anyway.
The term theme override suggests there being a default theming function to override - in many themes, we're not making explicit overrides of functions, but creating alternatives to use in specific cases.
I'm now assuming that I'd need to define this in a module if I wanted to define a function as theme_function_name rather than themename_theme_function_name.
This page here about theming on Drupal.org was a useful quide in finding out some of this info.
I want a warning message displayed in the comment form when people try to add comments:
"Please write comments in correct
grammatical English, otherwise they
will not published"
How can I do it?
Here is how you can do it by using hook_form_alter in your own module:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case "comment_form":
$form['#prefix'] .= "<div><p>Show some text before the comment form.</p></div>";
break;
}
}
You can alter the comment form so that your guidelines are added to it. There are a handful of ways to alter forms in Drupal. You can do it in your theme's template.php file (which I prefer for simple changes) or in a custom module. This article describes both methods, in Drupal 5 and 6, however not for the form you're interested in. However, the method used is the same that leads to the solution below. This is how you can make the change via template.php:
The following PHP code can be added to your theme's template.php file:
function YOURTHEME_theme() {
return array(
'comment_form' => array(
'arguments' => array('form' => NULL),
),
);
}
function YOURTHEME_comment_form($form) {
$output = '';
$output .= '<div class="comment-help">' . t('Please write comments in correct grammatical English, otherwise they will not published.') . '</div>';
$output .= drupal_render($form);
return $output;
}
Replace YOURTHEME with the name of your theme. If you already have a YOURTHEME_theme function you will need to add the 'comment_form' key to the array it is already returning. I doubt you do, but it's worth mentioning just in case.
A note: you should not be editing any of the themes in /themes, but you should have made a new theme or copied and renamed any of those themes into /sites/default/themes or /sites/all/themes.
The above code is based on code from this page.
Once you are inside a hook_form_alter function you can use the Development module (http://drupal.org/project/devel) dpm() function in place of var_dump to help view and isolate which properties to change in the big form arrays. I find this is a must-have when trying to figure out changes to an existing form. It puts all the elements of the form array into clickable rows.
In Drupal 7 go to
admin/structure/types/manage/mycontenttype/comment/fields/comment_body
There you can add your text. It will be shown below the field as usual. If you want the warning displayed above the field, you'd have to go the form_alter way.
this module does a good job at creating a tagcloud block - all good here. now id also like to have a page that lists all tags with next to each tag the number of posts that were tagged with this term. all terms are listed ok on http://example.com/?q=tagadelic/list/3 but i dont think tagadelic can add the number of posts per tag?
also, it seems tagadelic can just output one single block "tags in tags". whatever changes i make in the tagadelic configuration is applied to the tagadelic/list/3 url AND to the tagcloud block in the sidebar (the order of tags and number of tag levels)
does what i need require some custom module or are there others around that can achieve this? ive been playing around with Views 2 but cant quite get what I need
Use views and views_cloud for a much more flexible solution.
Edit: If you are having trouble with the views module, there is some very good in-browser instructions that come with it, but they require the advanced_help module.
For historical information:
Tagadelic can add the number of posts per tag, just fine. Assuming your theme is called "red":
/**
* theme function that renders the HTML for the tags
* #ingroup themable
*/
function red_tagadelic_weighted($terms) {
$output = '';
foreach ($terms as $term) {
$output .= l($term->name, taxonomy_term_path($term), array('attributes' => array('class' => "tagadelic level$term->weight", 'rel' => 'tag'))) .' ('. $term->count .') ';
}
return $output;
}