Monday, 31 January 2011

css show/hide example

<head>
<style type="text/css">
#hidiv {
position: fixed;
left: 400px;
top: 70px;
width:125px;
text-align:center;
background-color: lightyellow;
padding:10px;
border:1px solid orange;
display: none;
font-family:'trebuchet ms';
}
#ctext {
position: fixed;
left: 200px;
top: 70px;
width:125px;
text-align:center;
background-color: lightyellow;
padding:10px;
border:1px solid orange;
font-family:'trebuchet ms';
}
</style>
<script language="javascript">
function hiddendiv(id, visibility) {
document.getElementById(id).style.display = visibility;
}
</script>
</head>
<body>
<div id="ctext">
<a name=type onclick="hiddendiv('hidiv', 'block');";>
show</a> |
<a name=type onclick="hiddendiv('hidiv', 'none');";>
hidde</a>
</div>
<div id="hidiv">Hidden Div</div>

</body>

Saturday, 29 January 2011

Creating View of Distinct Active Taxonomy Terms (browse by category)


A list of distinct active taxonomy terms can be useful as it allows visitors to browse content by taxonomy term (categories). Trying to set up such a list using views often results in a list with duplicates. As lots of people seem to hit the same wall when trying to set this up I thought I'd put in the effort of creating this recipe. :)
For this you'll need to have views installed and taxonomy enabled. You should already know how to set up vocabularies and how to categorize content. If not, you may want to read the Taxonomy handbook first.
When I first created this recipe I used a method provided by Himerus on his website. After a couple of months a much, much easier way of doing this became available thanks to joel_guesclin and merlinofchaos:
1) create a new Term View (not a Node View like most people tend to use and like the previous method below)
2) add the Taxonomy Term to the list of Fields
3) Add a relationship "Taxonomy: Node" and make it obligatory
4) Add some filters to narrow the results as needed. (i.e. only nodes from a certain content type)

Friday, 28 January 2011

Adding Custom Links to Drupal Views

http://www.enterana.com/blog/web-development/adding-custom-links-drupal-views
For a project that I am working on, I needed to add function links to various Views that I was creating. I wanted to add links to actions that I had written in the site's custom module. Initially, I had some trouble figuring out how to do this but once I found the right kind of field to add it's a piece of cake.
The magic lies in the "Global: Custom Text" field type, and the only real trick is that you need to add your included fields first. Here's how to do it:
  1. Create or open a View
  2. Add your desired Fields
  3. For my action, I wanted to reference the Node's id (nid). However, I had no reason to display the nid to the user. So, I added the nid Field but selected the "Exclude from Display" option.

Wednesday, 26 January 2011

How to customise the search box in Drupal 6

Use Drupal 6's preprocess function in your theme's template.php file to modify the template variables before they are passed into the template files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Override or insert PHPTemplate variables into the search_theme_form template.
*
* @param $vars
*   A sequential array of variables to pass to the theme template.
* @param $hook
*   The name of the theme function being called (not used in this case.)
*/
  
function mytheme_preprocess_search_theme_form(&$vars, $hook) {
  
  // Modify elements of the search form
  $vars['form']['search_theme_form']['#title'] = t('Search mysite.com');
  
  // Set a default value for the search box
  $vars['form']['search_theme_form']['#value'] = t('Search');
  
  // Add a custom class to the search box
  $vars['form']['search_theme_form']['#attributes'] = array('class' => t('cleardefault'));
  
  // Change the text on the submit button
  $vars['form']['submit']['#value'] = t('Go');
  
  // Rebuild the rendered version (search form only, rest remains unchanged)
  unset($vars['form']['search_theme_form']['#printed']);
  $vars['search']['search_theme_form'] = drupal_render($vars['form']['search_theme_form']);
  
  // Rebuild the rendered version (submit button, rest remains unchanged)
  unset($vars['form']['submit']['#printed']);
  $vars['search']['submit'] = drupal_render($vars['form']['submit']);
  
  // Collect all form elements to make it easier to print the whole form.
  $vars['search_form'] = implode($vars['search']);
}
Note: you must rename the preprocess function so it's right for your theme (replace mytheme with the name of your theme)
In order for this to work, you must also ensure you have a copy of search-theme-form.tpl.php in your theme directory (you can grab it out of modules/search/ from your Drupal core files). You can then edit this as you like for further theming.


Searched words: 
how to theme search form in drupal 6 edit-submit form-text
So it's 1am and you want to theme the search form output of your brand new drupal 6 theme, here's the quick and easy one step guide that will get you though it. Yep, that's right, 1 step, because that's how we do it the agaric way...

Resolution

STEP 1 of 1
Paste the code below into into your trusty template.php file and customize to your heart's desire... (leave out the php open and closing tags, we use em to make the output look nice, with pretty colors and stuff...)
<?php
/**
* Override or insert PHPTemplate variables into the search_theme_form template.
*
* @param $vars
*   A sequential array of variables to pass to the theme template.
* @param $hook
*   The name of the theme function being called (not used in this case.)
*/

function mytheme_preprocess_search_theme_form(&$vars, $hook) {
  // Modify elements of the search form
  $vars['form']['search_theme_form']['#title'] = t('Search mysite.com');
  // Set a default value for the search box
  $vars['form']['search_theme_form']['#value'] = t('Search');
  // Add a custom class to the search box
  $vars['form']['search_theme_form']['#attributes'] = array('class' => t('cleardefault'));
  // Change the text on the submit button
  $vars['form']['submit']['#value'] = t('Go');
  // Rebuild the rendered version (search form only, rest remains unchanged)
  unset($vars['form']['search_theme_form']['#printed']);
  $vars['search']['search_theme_form'] = drupal_render($vars['form']['search_theme_form']);
  // Rebuild the rendered version (submit button, rest remains unchanged)
  unset($vars['form']['submit']['#printed']);
  $vars['search']['submit'] = drupal_render($vars['form']['submit']);
  // Collect all form elements to make it easier to print the whole form.
  $vars['search_form'] = implode($vars['search']);
}
?>

I came across a scenario where it was needed to customize Drupal 6 search box for my current drupal based theme and this customization was needed to :
  1. Change Default text on submit button
  2. Change Default text in search box (Search this site)
  3. When user clicks on search box, default search box text should disappear
All of above tasks should be easily attainable if you are not working in Drupal but it doesn’t mean that these are very difficult while working in Drupal. Somehow, it is a little tricky.
Most recommendable and suitable way that i found was to create a new module and use form_alter hook to track the search form events and bring all above changes in these events.
I’ll recommend you to read this thread: http://drupal.org/node/214592 where Heine helped alot to get clear ideas about form_alter hooks and their usage. Hopefully it will help you too.
Now lets move towards the solution.
First of all create a new module under sites/all/modules with any name. Say it is named ‘abc’ (supposing that you will be aware of the process of module creation and all files needed to create for a new module in Drupal). Open your abc.module file and create a new function there named ‘abc_form_alter‘ having parameters (&$form, $form_state, $form_id).  Basically, ‘abc_form_alter‘ will implement hook_form_alter (explained well http://drupal.org/node/214592) and will use this function to grab search form events and implement my search box changes on triggering of those events.  So this is the summary of our solution and following code you’ll write in abc.module file.
function abc_form_alter(&$form, $form_state, $form_id) {
  if($form_id=='search_theme_form')
  {
 $form['submit'] = array('#type' => 'submit', '#value' => t('[ GO ]'));
 $form['search_theme_form']['#default_value'] = 'Search My Site';
 $form['search_theme_form']['#attributes'] = array('onfocus' => "if (this.value == 'Search My Site') {this.value = '';}" );
  }
}
and That’s All :) .
Lets make a short review on above code,
This condition if($form_id==’search_theme_form’) will check if ‘search_theme_form’ is about to render then do following stuff:
$form['submit'] = array('#type' => 'submit', '#value' => t('[ GO ]'));
Above Line will change the default submit button text and make it to be [ GO ]
 $form['search_theme_form']['#default_value'] = 'Search My Site';
This code Line will Change Default text in search box and make it to be ‘Search My Site’
 $form['search_theme_form']['#attributes'] = array('onfocus' => "if (this.value == 'Search My Site') {this.value = '';}" );
This final code line gives the way to change any attribute for this text box (you may use it to change the default css class used for search box, too). Here i changed the ‘onfocus’ attribute and wrote the javascript code so when user click on search box for searching some keyword, search box’s default text (Search My Site) should get vanished.
One last thing, if you’r at very basic level in Drupal or even not a programmer then i’ll recommend you to go for this module Custom Search Box. This module is good but keep in mind that you won’t have everything in your hand while using this module but above process can help you to play freely with search form attributes.
I hope this post will open new horizons for you to customize any Drupal based Form

Tuesday, 25 January 2011

Creating Links (aka Anchor tags) - The "l" Function

l() is a function that is available anywhere within a Drupal installation. It accepts three arguments:
  1. $text: The text that will be visible to users for your link
  2. $path: The URL that the link will go to when clicked.
  3. $options: An associative array which allows you to set additional parameters to further customize the link with CSS classes etc… It is here that you would need to establish that the link will be an image if you so desire (I’ll go into this later). The $options array is optional.

 l($text, $path, $options = array())

 

Specifying Anchor Tag Attributes

Two common anchor tag attributes are class and target. Here's how we add them to the previous example:
<?php

   $attributes
= array( 'class' => 'foo', 'target' => '_blank' );
  
$link = l('Permalink', 'node/1', $attributes);
?>

Linking to Images

The function's default action assumes that $text is pure text, not HTML. If < and > are present they're automatically converted into &lt; and &gt;. If you're linking an image you need the HTML so set the $html parameter TRUE. As you'll see in the l function's full description the $html parameter is the last of seven parameters, so you need to specify all of the preceding parameters. This call would therefore look something like:
<?php

   $image
= '<img src="…" alt="Alternative Text" />';
  
$link = l($image, $url, array(), NULL, NULL, FALSE, TRUE);
?>

How to create an image based link using the l() function for Drupal

The $text section of the l() function accepts any text so this text could be html and therefore could be an image. However by default it filters out html unless you tell it otherwise. To tell the l() function to accept html for its text you need to build the $options array and pass it as an argument.

   $options = array(
              'attributes' => array(),
              'html' => TRUE,
            );
l('<img src="/images/path-to-image.png" />', 'node/1', $options);
The 'html' =>TRUE, is the key here. Yes, I know, for building an image based link this is a little more than an ideal amount of code but this is the current state of affairs. I personally would rather have 4 arguments with $html = FALSE being the default for the third argument and $options for the 4th. This was changed in Drupal 5 where all of the options were individual arguments, and honestly the Drupal 6 version is better.
<?php
l
('Very thin abstraction layer', 'node/11554', array('fragment' => 'comment-37717', 'class' => 'active'); // really you don't need the class set to active, because l() adds class="active" by default.?>
<?php
l
('Very thin abstraction layer', 'node/11554', array('fragment' => 'comment-37717', 'target' => _blank); ?>
<?php
l
('Very thin abstraction layer', 'node/11554', array('fragment' => 'comment-37717', 'attributes' => array('class' => 'active', 'target' => '_blank'));> // really you don't need the class set to active, because l() adds class="active" by default.?>

Example with custom class, attributes

This will produce a link with the class="widelink" and rel="lightbox" attributes on the a tag.

<?php
l
(
 
t('My link'),
 
'node/56',
  array(
   
'attributes' => array(
     
'class' => 'widelink',
     
'rel' => 'lightbox',
    )
  )
);
?>

Links to an anchor, or hash-only links

To create a link to a named anchor (e.g. #namedanchor ) you will need to use a small work-around.
l('linktext', '', array('fragment' => 'namedanchor', 'external' => TRUE));
to create a hash-only link (to #) you'll need to adapt it to:
l('linktext', '', array('fragment' => ' ', 'external' => TRUE));
(note that fragment does contains a space.)

how to prevent l() from screwing up an ubercart purchase link

In porting a D5 site to D6, I found within their Ubercart logic they are generating a purchase item image-link like this:
   $buy_img = theme_image('sites/all/themes/customTheme/images/buy.png', 'BUY', 'Buy The' . check_plain($node->title), array('class' => 'coins'));
    $buy_link = 'cart/add/e-p' . $node->nid . '_q1_m0?destination=cart/checkout';
    print l( $buy_img, $buy_link, array('html' => true) );
The problem is the l() function is supposed to generate this:
http://localhost/cart/add/e-p176_q1_m0?destination=cart/checkout
but is generating this incorrect link with character encoding rather than '?' and '=' characters:
http://localhost/cart/add/e-p176_q1_m0%3Fdestination%3Dcart/checkout
Apparently, the D5 l() function does not do that same character encoding, and was not an issue... So here's the fix for D6:

    $buy_link = 'cart/add/e-p' . $node->nid . '_q1_m0';
    print l( $buy_img, $buy_link, array('html' => true, 'query' => 'destination=cart/checkout') );
The solution being to place the query string portion of the link into a 'query' attribute.

Add permissions to view links

I thought this was helpful...
if (user_access('create blog entries')) {
    $items[] = l(t('Create new blog entry.'), "node/add/blog");
  }
Used in blog module function blog_page_last

Internal Module Links

To clarify for those who want their module to support the 'Clean URLs' option.
<?php
  $var
= l("Click Text", "ModuleName");?>

returns a link complete with <a href= tags. For a link that goes with your hook_menu() and 'page arguments' you just add the arguments to the path.
<?php
  $var
= l("Click Text", "ModuleName/value/value");?>
If you need a link without the <a href= tags use 'url' instead.
<?php
  $var
= url("ModuleName/value/value")?>

Examples

Various implementation of l() function
simple:

<?php
  l
(t('Link text'), 'about-us');?>
with class:

<?php
  l
(t('Link text'), 'about-us', array('attributes' => array('class' => 'about-link')));?>
link to user:

<?php
  l
(t($user->name), 'user/'.$user->uid, array('attributes' => array('class' => 'link', 'id' => 'xxx', 'title' => 'Title')));?>

Always use user/user id!!! simple link with image:

<?php
$img
= '<img src="'.$base_path . $directory . '/images/rssIcon.png" />';
l($img, 'user/3', array('html' => array('html' => 'true')));
// also valid
l($img, 'user/3', array('html' => 'true'));?>
complex link with image:

<?php
l
($img, 'user/3', array('attributes' => array('class' => 'link', 'id' => 'xxx', 'title' => 'Title'), 'html' => 'true'));?>

Some of those examples

*might* lead to security issues.. (I mean... sure I did some of those mistakes... back in the days... :) )
the l() function sanitize the first argument (the text) when the option "html" isn't TRUE...
it means that you need to really take care about what the first argument will be if using the "html" option
For example:

<?php// making an img tag like might not be the safest solution
//$img = '<img src="'.$base_path . $directory . '/images/rssIcon.png" />';
// this would be much better, but still, you need to know that the $directory variable is also safe
$img = theme('image', $directory .'/images/rssIcon.png');// this is, I think, not possible...
//l($img, 'user/3', array('html' => array('html' => 'true')));
// is ok
l($img, 'user/3', array('html' => 'true'));?>
<?php// well, stop me if I am wrong, but I guess might be useful to wrap $user->name within t()..
//l(t($user->name), 'user/'.$user->uid, array('attributes' => array('class' => 'link', 'id' => 'xxx', 'title' => 'Title')));
// Something who might be handy if you need to "define" an HTML id is the form_clean_id()
// it will prevent to have twice the same id in the HTML (which is bad :) )
l($user->name, 'user/'.$user->uid, array('attributes' => array('class' => 'link', 'id' => form_clean_id('xxx'), 'title' => 'Title')));?>
Just a note..
if you need to have a variable in a translation.. like it might have been the case in the example

<?php
l
(t('Some string related to @user_name', array('@user_name' => $user->name)), 'user/'. $user->uid);// I might explain why it might be better to use @user_name over a simple @user... but it's not the topic ;)?>

or...
<?php
theme
('username', $user);// whatever.. ?>
anyway, it was nice from you to help!

in $options you forget to

in $options you forget to explain language options, to protect links against language prefix names like 'site.name.com/en/sites/defailt/files/blag.pdf'
I protect my site against this using below solution

<?php
$link
= l($title, $path, array('language' => 'pl'));?>

'boxes' table in database

<?php
l
($img, 'user/3', array('attributes' => array('class' => 'link', 'id' => 'xxx', 'title' => 'Title'), 'html' => 'true'));?>

link -localized_options-query

I'm using ThemeKey to change the theme on a series of pages on my website that have a completely different theme from the rest of the site -- based on a parameter appended to the url. Some of these pages are nodes that must be able to be updated from the subtheme side, as well as the main theme. So I’m trying to hook into the menu_item_link in template.php, but it doesn’t like my code. I copied the applicable snippet from the zen theme template php, and modified it as below for my zen_dis sub theme -- It still seems to be looking at the main Zen theme for this option, though – how do I phrase the Hook_ language to force it to use this?:
function zen_dis_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
// If an item is a LOCAL TASK, render it as a tab
if ($link['type'] & MENU_IS_LOCAL_TASK) {
$link['title'] = '' . check_plain($link['title']) . '';
$link['localized_options']['query'] = '?dis-site-search-organization';
$link['localized_options']['html'] = TRUE;
}
return l($link['title'], $link['href'], $link['localized_options']);
}

For Drupal6

If you look at the description of the function in Drupal 6 parameters you will find under $options:
Additional $options elements used by the url() function.
But I was not able to put additional parameters into URL in this case:

print l("Click here", $internal_path, array(
   'listing_url' => 'data'
));
So I made the following changes as the user described above and it worked:

print l("Click here", $internal_path, array(
   'query' => array(
      'listing_url' => 'data'
    ),
));

Class Active

Hi,
I've tried experimenting with this function in order to append the class="active". It works great with the internal path (node/#), but if I supply the url alias it doesn't seem to work.
I've tried this way :
<?php
print l(t('Link Text'),'my-url-alias',array('alias' => 'TRUE')); ?>
Maybe I'm doing this wrong. Any thoughts?
Regards

Link to #

This is for you modesia...
to create a hash-only link (to #) you'll need to adapt it to:
l('linktext', '', array('fragment' => ' ', 'external' => TRUE));
(note that fragment does contains a space.)
Thanks to alexanderpas!

Title and Alt attributes are sanitized

Actually all the "attributes" values are sanitized in drupal_attributes, so if you do anything to sanitize it yourself, you can end up double encoding. For example, l($title, $path, array('attributes' => array('title' => t('All about @names', array('@names' => 'Adam & Eve'))))) will result in the ampersand being double encoded. In this example, it is okay to use '!names' because drupal_attributes will do a check_plain for you.

Link to #

to create a hash-only link (to #) you'll need to adapt it to:
l('linktext', '', array('fragment' => ' ', 'external' => TRUE));
(note that fragment does contains a space.)
Thanks to alexanderpas!

Can't figure out how to

Can't figure out how to combine attributes and fragment, since I need both - the first one to apply a class, second one to generate proper URI to the comment. What is wrong in my code below?
<?php
$block_content
.= l(t('Conversate'), "node/$links->nid", array('attributes'=>array('class'=>'comment')), array('fragment' => "comment-$links->cid")).'</div>';?>
Login or register to post comments

Tuesday, 11 January 2011

Source base installation of php, mysql and apache in ubuntu/ linux

Compile and Install a LAMP(Linux/Apache/MySQL/PHP) Server from Source In the last post, I described the method to install a LAMP ser...