우리가 직접 만드는 Actions 모듈의 액션 예제 - Redirect to a page
제목 : 우리가 직접 만드는 Actions 모듈의 액션 예제 - Redirect to a page
Actions 모듈을 설치/활성화하고 나서,
admin/build/actions 화면에 가 보면,
기본적인 액션의 종류가 6개 뿐이다(2008년 2월 버전5 기준).
액션의 종류가 좀 더 다양하면 좋겠다는 생각이 든다.
그래서 우리가 직접 만들어 보는 건 어떨까 싶어 조사해 보았더니,
아래의 주소에 트리거가 발생하면 "특정 페이지로 이동"하는 액션을
추가하는 예제가 있었다. (드루팔, 대단해!)
위 주소에 나타나 있는 예문들을 modules/actions/actions.inc 파일에
추가하고 난 다음에, admin/build/actions 화면에 가 보면 새로운
"특정 페이지로 이동"하는 액션이 나타난다!
주의) 작업하기 전에, actions.inc 파일을 백업해 두기 바란다.
참고로 드루팔은 기본적인 모듈의 수정을 권장하지 않는다.
혹시나 해서 위 예제가 반영돼 있는 소스를 여기에 기재해 둔다.
actions.inc 파일의 제일 하단에 아래와 같은 소스를 추가했다.
$output = '';
switch ($section) {
case 'admin/modules#description':
$output .= t('<strong>Action:</strong> Configurable page redirection for use in workflows.');
break;
}
return $output;
}
function action_redirect($op, $edit = array(), &$node) {
switch ($op) {
case 'metadata' :
return array (
'description' => t('Redirect user to a page'),
'type' => t('Redirect'),
'batchable' => false,
'configurable' => true);
case 'do' :
if(!isset($edit['destination'])){
// log
watchdog('error', t('Action Redirect: no redirect destination availbale!'));
break;
}
// get the path
$destination = $edit['destination'];
// log
watchdog('action', t('Redirecting to %destination', array('%destination' => $destination)));
drupal_goto($destination);
// If the above causes problems try
// $_REQUEST['destination'] = $destination;
break;
// return an HTML config form for the action
case 'form' :
// default values for form
$form = array ();
$form['destination'] = array (
'#type' => 'textfield',
'#title' => 'Redirect destination',
'#default_value' => $edit['destination'],
'#maxlength' => 250,
'#collapsible' => FALSE,
'#required' => TRUE,
'#description' => t('Specify a relative URL to redirect the user to, like `user/register`'),
);
return $form;
// validate the HTML form
case 'validate' :
$errors = array ();
foreach ($errors as $name => $message) {
form_set_error($name, $message);
}
return count($errors) == 0;
// process the HTML form to store configuration
case 'submit' :
$params = array (
'destination' => $edit['destination'],
);
return $params;
}
}
이것을 응용하면 또다른 액션도 만들 수 있겠다... :)


Comments
2 comments posted드루팔.오알지에는 액션에 관한 수많은 예제 팁들이 있다.
http://drupal.org/search/apachesolr_search/action%20snippet
특정 롤(Role)에 해당하는 회원들에게 메일 보내는 드루팔 액션 Snippet 팁도 있다.
Send an email to everyone in a role group
http://drupal.org/node/48738