Tuesday, May 31, 2011

Customize the Default Error reporting

Customize the Default Error reporting

go to "Admin" > Error Reporting > Set "Default 403 (access denied) page:" to:

access_denied

<?php
/**
 * Implementation of hook_menu().
 */
function myModule_menu() {
  $items = array();

  $items['access_denied'] = array(
    'title' => '',
    'page callback' => 'myModule_default_403_access_denied_page',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}


function myModule_default_403_access_denied_page() {
  // Customize error reporting message for some URL.
  // Note: you should use $_REQUEST['destination'] instead of $_GET['destination'].
  if ($_REQUEST['destination'] === 'user/register') {
    $title = '';
    $str = t('You have been signed as our member. Thanks for your interest.');
  }
  else {
    $title = t('Access denied');
    $str = t('You are not authorized to access this page.');
  }
  
  drupal_set_title($title);

  return $str;
}
?>

Monday, May 30, 2011

check all / uncheck all checkbox drupal

<?php
function myModule_batch_add_opp_form($form_state) {
  $form = array();

  $nid_contactCheckUncheckAll_js = <<< EOD
    Drupal.behaviors.nid_contactCheckUncheckAll = function(context) {
      $('#edit-nid-contact-').click(function(event) {
        $('#myModule-batch-add-opp-form').find('input[@type=checkbox]').each(function() {
          this.checked = $('#edit-nid-contact-').attr('checked');
        });
      });
    };
EOD;

  drupal_add_js($nid_contactCheckUncheckAll_js, 'inline');

  $form['nid_contact'] = array(
    '#type' => 'checkboxes',
    '#title' => 'Select Contacts',
    '#options' => array('' => 'Check / Uncheck All') + myModule_nid_contactArr(),
    '#required' => TRUE,
  );

  return $form;
}
?>

How to update a module's weight

How to update a module's weight

In Drupal, the order in which a module's hooks get called is dependent on the weight of your module in the system table. You can set a low weight (including negative numbers) to get your module to execute before others. Or, you can set a high weight to execute after other modules.

Note: In Drupal 7, modules can vary the weights of their hooks to be more flexible.

Code to update weight

You will want to modify and then place this code into your module's modulename.install file in a modulename_install function. See more details on the hook_install in the hook_install API documentation.

UPDATE system SET weight = [yournumber] WHERE type = 'module' AND name = 'yourmodulename';

Reference:
http://drupal.org/node/110238

Saturday, May 28, 2011

這才是人生 (曹興誠)

這才是人生 (曹興誠)

曹興誠在台北演講時,有學生問他關於生涯規劃。他答說一生從不生涯規劃 ,只做好眼前一、兩年內的事
十歲的我,面對天真的隔壁小女孩,她說:「大哥哥,長大後我要嫁給你,我會對你好,以後你就會知道。」

二十歲的我,面臨聯考的挫折,只聽見父親說:「大頭,不努力考上好學校,將來怎麼找到好工作,現在貪玩,以後你就會知道。」

三十歲的我,第一次相親,媒人告訴我:「小嬋是個好女孩,聰明、孝順,會煮一手好吃的飯菜,娶了有幫
夫運的她啊,會好運一輩子,以後你就會知道。」

四十歲的我,收到滿是紅色的體檢報告,醫師告訴我:「大頭先生,你應該少應酬,少抽煙喝酒,少吃肉類
和生猛海鮮,多運動,作息要正常。你要再不好好保重自己的身體,以後你就會知道。」

五十歲的我,喝了自己兒子的喜酒,鄰座的阿尚告訴我:「親家!呷你恭喜呵!娶一個水媳婦,啊嗯哥(可
是)欠腳的媳婦派管(能幹的媳婦難管),以後你丟摘啊啦(以後你就會知道)。」

六十歲的我,參加了同學的葬禮。看到浮雲蒼狗,它彷彿告訴我:「......以後你就會知道.......。」

現在的我,只擁有一本相本夾著幾張泛黃的照片;幾張忘記要寄出、也不記得要寄給誰的情書;幾件參加
過無數喜喪的西裝;一紙成績單包著數十年努力後的退休金;還有一直陪在自己身邊的老伴。我告訴我自
己:「其實,你很富有;所以,把握當下,珍惜一切,努力為人群去付出,至於以後,你根本不須知道。」

忙的時候,想要休息;渡假的時候,想到未來。窮的時候,渴望富有;生活安逸了,怕幸福不能長久。
該決定的時候,擔心結果不如預期;看明白了,後悔當初沒有下定決心。

不屬於自己的,常常心存慾望;握在手裡了,又懷念未擁有前的輕鬆。

「生命若不是現在,那是何時?」「能隨心所欲的去過生活,這才是人生!」

路已盡頭,該轉彎了

今天在報紙上看到一篇女生寫的文章,讓我感觸很深,她因為初戀的失敗,一直離不開失戀的痛苦而有
輕生的念頭,在她想自殺的那一剎那,她看到了公車上有的一小段詩,詩的內容就有這麼一句:『不是路已
走到盡頭,而是該轉彎了!』而讓她拋棄了輕生的念頭。最後她寫說:大二的我,被一個不速之客擾亂了
平靜的生活卻也不經意的被另一個不速之客救贖了。

生命中總有挫折,那不是盡頭,只是在提醒你,該轉彎了。

『不是路已走到盡頭,而是該轉彎了!』 這句話其實很有意思!

當你遇到一件事,已無法解決,甚至是已經影響到你的生活、心情時,何不停下腳步,暫時的想一想是否
有轉闤的空間,或許換種方法,換條路走事情便會簡單點。但通常在那一刻,我們並來不及想到這些,是
一昧的在原地踏步、繞圈,讓自己一直的陷在痛苦的深淵中,生命中總有挫折,那不是盡頭,只是在提醒
你:該轉彎了!

放手不代表承認失敗,放手只是為自己再找條更美好的路走! 『做好手邊的事,這就是人生。』

活在當下才是最好的選擇!把握每一個美好的當下!每一件事情都是美好的!

zip compress file with password protected encryption protection for Mac

zip -e mytest.zip mytest.txt

Thursday, May 26, 2011

Few ways to validate drupal node and node form

Few ways to validate drupal node and node form

Method 1: drupal form
<?php
function moduleName_sample_form($form_state) {
  $form['comp_name'] = array(
    '#type' => 'textfield',
    '#title' => 'Company Name',
  );
}

function moduleName_sample_form_validate($form, &$form_state) {
  if (strlen($form_state['values']['comp_name']) < 3)) {
    form_set_error('comp_name', 'company name must be more than two characters.');
  }
}
?>

Method 2: hook_nodeapi()
<?php
/**
 * Implementation of hook_nodeapi().
 */
function moduleName_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($op === 'validate') {
    if (empty($node->field_start_date[0]['value'])) {
      form_set_error('field_start_date', 'Start Date must not be empty.');
    }
  }
}
?>

Method 3: hook_form_alter()
<?php
/**
 * Implementation of hook_form_alter().
 */
function moduleName_form_alter(&$form, $form_state, $form_id) {
  if ($form_id === 'contact_node_form') {
    moduleName_reset_contact_node_form_fields($form);
  }
}

function moduleName_reset_contact_node_form_fields(&$form) {
  ### validate start date and end date.
  $form['#validate'][] = 'moduleName_validate_field_start_end_date';
}

function moduleName_validate_field_start_end_date($form, &$form_state) {
  if (empty($form_state['values']['field_start_date'][0]['value'])) {
    form_set_error('field_start_date', 'Start Date must not be empty.');
  }
}
?>

make CCK field required

make CCK field required

Since CCK field shared the same global "required" setting, we can hook_form_alter() to make the field required in some situations.

<?php
/**
 * Implementation of hook_form_alter().
 */

function test_form_alter(&$form, $form_state, $form_id) {
  ### make nid_contact field required.
  ### Note: you need following both lines in order to make it work properly.
  $form['field_nid_contact']['#required'] = TRUE;
  $form['field_nid_contact'][0]['#required'] = TRUE;
}
?>