Sunday, September 26, 2010

Example of hook_comment & drupal_mail

Below is the example of drupal mail in the hook comment
function MODULENAME_comment(&$a1, $op){
    switch($op){
      case 'insert' :
        $nid = $a1['nid'];
        $node = node_load($nid);
        $account = user_load($node->uid);
 

        //Collect $params contents values to be used on hook_mail
        $params['commentator'] = $a1->name;
        $params['title'] = $node->title;
        $params['type'] = $node->type;
        $params['nid'] = $node->nid;
        $params['language'] = $account->language;
        $params['nodeauthor'] = $node->name;
        drupal_mail('MODULENAME', 'commentnotice', $account->mail, user_preferred_language($account), $params);    
    }
}


// define the drupal hook_mail
function MODULENAME_mail($key, &$message, $params){
  switch($key){
    case 'commentnotice' :
      $language = $message['language'];
      $message['subject'] = t('Notification from @site', array('@site' => variable_get('site_name', NULL)), $language);
      $message['body'] = t('Dear @username \n\n@commentator commented on your @type', array('@username' => $params['nodeauthor'], '@commentator' => $params['commentator'], '@type' => $params['type']), $language->language);
      break;
  }
}

Wednesday, September 1, 2010

Removing some content from profile page

If there is some content on profile page you dont want to display, for example, you want to remove user summary, you can do it by using hook_profile_form

/**
 * Implementation of hook_profile_alter().
 */
function helper_profile_alter(&$account) {

  //var_dump($account->content);
  unset($account->content['summary']['#title']); //remove summary
  unset($account->content['summary']['member_for']); 
}

Example of hook form alter

This is an example for altering form

function helper_form_alter(&$form, &$form_state, $form_id) {   
    //var_dump($form_id);
    switch ($form_id){
        case 'user_profile_form' :
            unset ($form['user_terms']); // remove user_term form
            unset ($form['locale']); //remove locale form
            break;
    }

}