Changing tag cloud link to location - drupal

I am learning Drupal as I go and I am wondering if I can change a link coming from out of a tag cloud.
The link coming from the tag cloud goes to ...category/articles/locations/kittys
I would like it to go to the node tag at ...content/kittys
Any thoughts?

The assummed module you used for this, tagadelic, uses default taxonomy-paths for that.
So the answer is "yes" it can be changed. e.g. Forums (which are terms/tags in a taxonomy too) in a tagcloud will link to the forum home, not to the forum overview. This works because tagadelic uses taxonomy_term_path().
However, your question is a bit unclear about what (and why) you want to achieve this. What is "content/kitties"? Your question makes me believe you want to link to a node? Why? Tag-clouds represent tags, where the tag links to the list of posts within that tag.
That said, the easy way to change outgoing links is in the theme_function: to override the theme function.
/**
* theme function that renders the HTML for the tags
* #ingroup themable
*/
function my_custom_chees_puff_theme_tagadelic_weighted($terms) {
$output = '';
foreach ($terms as $term) {
$output .= l($term->name, "/link/to/anywere", array(
'attributes' => array(
'class' => "tagadelic level$term->weight",
'rel' => 'tag',
'title' => $term->description,
)
)
) ." \n";
}
return $output;
}
The other option is to override the general "where should a tag-link-link-to" Drupalwide. As forementioned forum.module does, trough hook_term_path():
function my_cheesy_puffs_kitten_module_term_path($term) {
return 'links/to/kittens/' . $term->tid;
}
Success! Bèr Kessels - Author and maintainer of Tagadelic :)

Related

Drupal 7 Views custom view template fields

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.

add_menu_page() add_submenu_page() | passing a $variable with called function

I have added a menu page on my WordPress backend with some submenu items.
A snippet of the code i use is:
// Add to admin_menu function
add_menu_page(__('New Menu'), __('New Menu Title'), 'edit_themes', 'new_menu_item', 'functiontocallonclick', '', 3.5);
// Add to secondlevel menu
add_submenu_page('new_menu_item', __('New |Sub Menu item'), __('New Menu Title item'), 'edit_themes', 'new_menu_sub_item', 'subfunctiontocallonclick',');
As you can see above it is calling the function functiontocallonclick when you go to the New menu item in the backend.
What i am wondering now:
I would like to pass a variable with the function.
functiontocallonclick($value);
Ofcourse it can't be done that way, so what is the good way?
I use this:
switch($_GET['page']){
case 'suppliers': $type='c';
break;
case 'contractors': $type='s';
break;
default: $type='';
break;
}
but I try to find some better solution.
This answer might seem late but I'll share how I work around this because it looks like quite a few people are looking for an answer to this question.
My answer is going to be passing variables to a nested function within a class-based context so if you're using functional programming then disregard the $this.
The Main hook action:
function mainSetup(){
add_action('admin_menu', array($this, 'mainMenuSetup'));
add_action('admin_menu', array($this, 'subMenuSetup'));
}
Menu Setup:
function mainMenuSetup(){
add_menu_page('DashBoard',
'DashBoard',
'manage_options',
'[yoursitesname]-admin-menu',
function(){ $this->pageSelect("admin"); },
'',
200
);
}
Within the example of the Main menu navigation option, we are passed a string in the nested function that will be passed as an argument to the method(function) 'pageSelect', but the string is hardcoded and can't be changed easily. We'll address this problem when setting up the submenu(s).
TODO: You should replace [yoursitename].
SubMenu Setup:
For the submenu let's say we want to make it easier to go back and add new submenus and change who has access to which submenus. For this, I'm going to make an associative array called 'subMenuObjects' where the array's keys are the submenu objects and their values are the page's permissions.
function subMenuSetup(){
$subMenuObjects = array(
'Settings' => 'manage_options',
'Services' => 'manage_options'
);
foreach ($subMenuObjects as $subMenu => $value) {
add_submenu_page('[yoursitesname]-admin-menu',
$subMenu . 'Page',
$subMenu,
$value,
'[yoursitesname]' . strtolower($subMenu) . '-menu',
function() use ($subMenu){ $this->pageSelect($subMenu); }
);
}
}
Here we use the 'use' to pass the $subMenu to the nested function.
Page select:
function pageSelect($page){
switch ($page) {
case 'admin':
echo '<div>Welcome to the Admin page</div>';
break;
case 'Settings':
echo '<div>Welcome to the Settings page</div>';
break;
case 'Services':
echo '<div>Welcome to the Service page</div>';
break;
default:
echo '<div>something happened, contact dev</div>';
break;
}
}
Yes, the way you're doing it is the way WordPress does it itself. To re-use admin screens, you have to pass some query var in the URL and then show/hide elements based on that.
You can also create invisible admin screens: How to enable additional page in WordPress custom plugin?. And this may be useful too: Redirect from add_menu_page

Drupal 7 - Appending class names to a menu block <ul>

I've been stuck on how to manipulate in the template file what a menu block outputs in it's html. The regular < ul class="menu" > with li links is fine and I don't need to completely gut this html that drupal creates for all menus but I want to inject classes 'inline' and 'links' like the system main menu (usually) already has under it's ul element. I could print the menu directly in the theme skipping blocks altogether but it would be more helpful in the long run to learn injecting class names into the output of menu blocks that are generated.
So far from googling around I've only been able to find a module that can enter ID's and Class names on the individual li's but not the ul wrapping them and I've been unable to get any similar template file snippets I've come across to work.
There is a way to use a hook function to do this isn't there?
Why don't you add the classes you want via javascript?!
Example:
jQuery("#MY_MENU_WRAPPER ul.menu").addClass("inline");
If that's the case, try the following code in your theme's template.php file
function return_menu_markup($menu_name, $attributes)
{
$items = array();
$menu_tree = menu_tree_all_data($menu_name);
$menu_tree_output = menu_tree_output($menu_tree);
foreach($menu_tree_output as $item_id => $item_data)
{
if(is_numeric($item_id) && is_array($item_data))
{
$items[] = l('<span>' . $item_data['#title'] . '</span>', $item_data['#href'], array(
'attributes' => $item_data['#attributes'],
'html' => TRUE,
)
);
}
}
return theme('item_list', array('items' => $items, 'type' => 'ul'));
}
Then anywhere in the template, simply do the following:
$attributes = array();
$attributes['id'] = "MY_MENU_ID";
attributes['class'] = array('MY_CLASS_1', 'MY_CLASS_2', 'MY_CLASS_3');
return_menu_markup("main-menu", $attributes);
Hope you find what needed :)
-Muhammad.
You can use template.php in your theme folder, use hook:
function THEMENAME_menu_tree__menu_main_navigation($variables){
return "<ul class=\"inline\">\n" . $variables['tree'] ."</ul>\n";
}
also note this menu_main_navigation is menu URL path, other values are always same. Do some cache delete few times, maybe it want work immediately.

How to add warning text in Drupal comment form

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.

Drupal tagging via tagadelic

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;
}

Resources