Thursday, August 19, 2010

Hook form alter

Example of hook_form alter

/*
 * implementation of hook_form_alter
 * remove user term form by
 * removing user_term fieldset
 */
function helper_form_alter(&$form, $form_state, $form_id) {
  //if ($form_id == 'user-profile-form') {
      $form['user_terms']['#type']['fieldset'] = FALSE;
    //}
    dsm($form_id);
  }

Example of theme links option #2


<?php
global $user;
$uid = $user->uid;
$app = 0;
$friend_req = db_result(db_query("SELECT  COUNT(*) AS counts FROM {user_relationships} ur WHERE ur.requestee_id = %d AND ur.approved = %d", $uid, $app));
$message = privatemsg_unread_count();
$links = array();
if($friend_req >= 1){
  $links[] = array(
    'title' => format_plural($friend_req, t('1 Friendship request'), t('@count Friendship requests')),
    'href' => 'relationships/requests',
    'attributes' => array('class' => 'friend-requests'),
  );
}

if($message >= 1){
  $message_count = format_plural($message, t('1 New message'), t('@count New messages'));
  $links[] = array(
    'title' => format_plural($message, t('1 New message'), t('@count New messages')),
    'href' => 'messages',
    'attributes' => array('class' => 'messages'),
  );
}
if($links){
  print theme('links', $links);
}

?>


Example of theme links

Below is the example of used of theme_links

<?php
$friend_req = $row->counts; //says db_query has been defined
$message = privatemsg_unread_count(); // count new privatemsg

if($friend_req >= 1){
    $friend_req_count = format_plural($friend_req, t('1 Friendship request'), t('@count Friendship requests'));
}

if($message >= 1){
    $message_count = format_plural($message, t('1 New message'), t('@count New messages'));   
}




//define $links as array
$links = array();
$links[] = array('title' => $friend_req_count, 'href' => 'relationships/requests', 'attributes' => array('class' => 'friend-requests'));
$links[] = array('title' => $message_count, 'href' => 'messages', 'attributes' => array('class' => 'messages'));
 
//use theme links to display link
print theme('links', $links);

?>

Example of format_plural combined with theme item_list

Below it the example of used of Drupal format_plural combined with theme item_list for showing notifications of new messages and friendship request using privatemsg module and user_relationships module.

<?php
global $user;
$uid = $user->uid;
$app = 0;
$row = db_fetch_object(db_query("SELECT  COUNT(*) AS counts FROM {user_relationships} ur WHERE ur.requestee_id = %d AND ur.approved = %d", $uid, $app));
$friend_req= $row->counts;
$message = privatemsg_unread_count();

$items = array();

if($friend_req >= 1){
    $friend_req_count = format_plural($friend_req, t('1 Friendship request'), t('@count Friendship requests'));
    $items[] = l($friend_req_count, 'relationships/requests', array('attributes' => array('class' => 'friend-requests')));
}

if($message >= 1){
    $message_count = format_plural($message, t('1 New message'), t('@count New messages'));
    $items[] = l($message_count, 'messages', array('attributes' => array('class' => 'messages')));
}

if(COUNT($items) >= 1){
    print theme('item_list', $items);
}