2010
01.29

“Login or register to vote” snippet for Fivestar module in Drupal

VotingAPI and Fivestar function very well if you want to implement a voting widget and all necessary functionalities that comes with it.

When I wanted to implement “Login to Vote” functionality -voting widget behaving similar to the comments for anonymous users-, I came across the following snippet which works flawlessly. Just rename [nameofyourtheme] to your theme name (without []) and insert it in your template.php file and you are good to go.

<?php
/**
* Display a static fivestar value as stars with a title and description.
*/

function [nameofyourtheme]_fivestar_static_element($value, $title = NULL, $description = NULL) {
  $output .= '<div class="fivestar-static-form-item">';
  $element = array(
    '#type' => 'item',
    '#title' => $title,
  );

  $output .= theme('form_element', $element, $value);
  $output .= '<div class="description">'. t('Login or register to vote.') .'</div>';
  $output .= '</div>';
  return $output;
}
?>

Source: quicksketch

Author: (16)

2010
01.28

It might be very convenient to create a page with PHP-filter and then insert a drupal_goto function to divert the user according to the conditions (e.g. logged-in, role ).

However, you should be careful not to break Cron with drupal_goto. As cron will get stuck trying to index your page with drupal_goto function.

In order to prevent this you can check for the $_SERVER['SCRIPT_NAME']. For example:

if ($_SERVER['SCRIPT_NAME'] != '/cron.php') {
       drupal_goto('someplace/in/your/website');
     }

Author: (16)

2010
01.25

Two useful snippets for block visibility in Drupal

Here are two very useful snippets that you should keep somewhere easy to access and safe:

To display the block ONLY on Node View but not on Node Edit:

<?php
// Only show on true node VIEW pages. Not node/add or node/n/edit etc
return (arg(0)=='node' && is_numeric(arg(1)) && (!arg(2)));
?>

The same principle works with ‘user’ as well if you want the block to show only in user profile but not in registration, request password screens.

Source: Drupal PHP snippets

To display the block ONLY on logged-in user's account page but not on other user's account page:

<?php
global $user;
// Return TRUE if we have a user viewing her/his own user profile.
return $user->uid && arg(0) == 'user' && is_numeric(arg(1)) && arg(1) == $user->uid;
?>

by Nedjo

Author: (16)

2010
01.19

Creating profile picture block in Drupal

Even though, it is quite easy to figure it out with a little PHP and Drupal knowledge, it might still be frustrating for beginners to find an easy answer for this question.

Try creating a block with the following PHP code (do not forget to select the PHP code as input format):

<?php
$owner = user_load(arg(1));
print theme('user_picture', $owner);
?>

Most probably, you will want to limit the visibility of the block to user/* and/or users/* (if you are using pathauto module).

Source: dalad’s comment

Author: (16)

2010
01.13

Rules module provides a very powerful infrastructure to define conditionally executed actions based on occurring events. Let’s say a major upgrade to the core Triggers module. It makes a lot of sense to use Rules with Token module to create robust triggers, actions and even workflows without writing a single line of code.

For a recent project, I needed to access the vote value provided by the Voting API and use it with Rules. Strangely enough, when I checked the tokens available to Rules (which is very comprehensive), I realized that the vote value was not available.

Once I figured out that there is no very easy solution for that, I started to look for the easiest way to overcome that inconvenience and further ones. Custom Token module came to my help.

Use the following code to create a custom token (Type=Node) via the Custom Token module. This will make the vote of the logged-in user be available anywhere the regular tokens are available.

global $user;
$criteria = array('content_type' => 'node', 'content_id' => $node->nid, 'uid' => $user->uid);
$user_vote = votingapi_select_single_vote_value($criteria)/10;
return $user_vote;

Author: (16)

2010
01.09

All web developers/designers need to use some tools (I am talking about non-application tools) to make their life easier. Most of these tools are quite famous and they generally depend on Firefox’s add-on system. (e.g. Firebug, Web Developer).

However, there is one other tool that I use quite often and very happy to have. So I decided that it deserves a mention in this blog. It is a javascript bookmarklet called Design by Allan Jardine.

Design features the following components:

  • Grid – overlays a highly configurable layout grid over a web-page. Grid can be set to match any set of dimensions, allowing easy development of CSS layouts in the web-browser.
  • Rule – displays rulers on a page, with all the expected features of the rulers found in a desktop design application, including guides which snap to block display elements and origin location control.
  • Unit – allows measurements to be made between any two points on a web-page, giving basic information about each of the points clicked on, and drawing a line on the page for the measurement.
  • Crosshair – draws a crosshair cursor on the page to assist in layout alignment. Cursor information is also presented in a tooltip.

Author: (16)