Created
December 1, 2013 13:15
-
-
Save ahmad24/7733501 to your computer and use it in GitHub Desktop.
Drupal : action triggers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* action_example.info | |
* name = Action example | |
* description = Demonstrates providing actions that can be associated to triggers. | |
* package = Example modules | |
* core = 7.x | |
* dependencies[] = trigger | |
* files[] = action_example.test | |
* ; Information added by packaging script on 2013-11-04 | |
* version = "7.x-1.x-dev" | |
* core = "7.x" | |
* project = "examples" | |
* datestamp = "1383601761" | |
*/ | |
/** | |
* go to : http://localhost/drupal-7.24/admin/structure/trigger/node | |
* you ahve to find : | |
* 1-Action Example: A basic example action that does nothing | |
* 2-Action Example: Unblock a user | |
* and you can assign trigger to event | |
* | |
* To find : 3-Action Example: Promote to frontpage and sticky on top any content created by | |
* you have to configur it first so go to : http://localhost/drupal-7.24/admin/config/system/actions | |
* | |
*/ | |
function action_example_action_info() { | |
return array( | |
'action_example_basic_action' => array( | |
'label' => t('Action Example: A basic example action that does nothing'), | |
'type' => 'system', | |
'configurable' => FALSE, | |
'triggers' => array('any'), | |
), | |
'action_example_unblock_user_action' => array( | |
'label' => t('Action Example: Unblock a user'), | |
'type' => 'user', | |
'configurable' => FALSE, | |
'triggers' => array('any'), | |
), | |
'action_example_node_sticky_action' => array( | |
'type' => 'node', | |
'label' => t('Action Example: Promote to frontpage and sticky on top any content created by :'), | |
'configurable' => TRUE, | |
'behavior' => array('changes_property'), | |
'triggers' => array('node_presave', 'node_insert', 'node_update'), | |
), | |
); | |
} | |
/** | |
* Implements hook_menu(). | |
* | |
* Provides a menu entry which explains what the module does. | |
*/ | |
function action_example_menu() { | |
$items['examples/action_example'] = array( | |
'title' => 'Action Example', | |
'description' => 'Provides a basic information page.', | |
'page callback' => '_action_example_page', | |
'access callback' => TRUE, | |
); | |
return $items; | |
} | |
/** | |
* A simple page to explain to the developer what to do. | |
*/ | |
function _action_example_page() { | |
return t("The Action Example provides three example actions which can be configured on the <a href='@actions_url'>Actions configuration page</a> and assigned to triggers on the <a href='@triggers_url'>Triggers configuration page</a>.", array('@actions_url' => url('admin/config/system/actions'), '@triggers_url' => url('admin/structure/trigger/node'))); | |
} | |
/** | |
* Action function for action_example_basic_action. | |
*/ | |
function action_example_basic_action(&$entity, $context = array()) { | |
// | |
// In this case we are ignoring the entity and the context. This case of | |
// action is useful when your action does not depend on the context, and | |
// the function must do something regardless the scope of the trigger. | |
// Simply announces that the action was executed using a message. | |
drupal_set_message(t('action_example_basic_action fired')); | |
watchdog('action_example', 'action_example_basic_action fired.'); | |
} | |
/** | |
* | |
*/ | |
function action_example_unblock_user_action(&$entity, $context = array()) { | |
// First we check that entity is a user object. If this is the case, then this | |
// is a user-type trigger. | |
if (isset($entity->uid)) { | |
$uid = $entity->uid; | |
} | |
elseif (isset($context['uid'])) { | |
$uid = $context['uid']; | |
} | |
// If neither of those are valid, then block the current user. | |
else { | |
$uid = $GLOBALS['user']->uid; | |
} | |
$account = user_load($uid); | |
$account = user_save($account, array('status' => 1)); | |
watchdog('action_example', 'Unblocked user %name.', array('%name' => $account->name)); | |
drupal_set_message(t('Unblocked user %name', array('%name' => $account->name))); | |
} | |
/** | |
* @see action_example_action_info() | |
*/ | |
function action_example_node_sticky_action_form($context) { | |
/* | |
* We return a configuration form to set the requirements that will | |
* match this action before being executed. This is a regular Drupal form and | |
* may include any type of information you want, but all the fields of the | |
* form will be saved into the $context variable. | |
* | |
* In this case we are promoting all content types submitted by this user, but | |
* it is possible to extend these conditions providing more options in the | |
* settings form. | |
*/ | |
$form['author'] = array( | |
'#title' => t('Author name'), | |
'#type' => 'textfield', | |
'#description' => t('Any content created, presaved or updated by this user will be promoted to front page and set as sticky.'), | |
'#default_value' => isset($context['author']) ? $context['author'] : '', | |
); | |
// Verify user permissions and provide an easier way to fill this field. | |
if (user_access('access user profiles')) { | |
$form['author']['#autocomplete_path'] = 'user/autocomplete'; | |
} | |
// No more options, return the form. | |
return $form; | |
} | |
/** | |
* Validates settings form for action_example_node_sticky_action(). | |
* Verifies that user exists before continuing. | |
*/ | |
function action_example_node_sticky_action_validate($form, $form_state) { | |
if (! $account = user_load_by_name($form_state['values']['author']) ) { | |
form_set_error('author', t('Please, provide a valid username')); | |
} | |
} | |
/** | |
* Submit handler for action_example_node_sticky_action. | |
* | |
* Returns an associative array of values which will be available in the | |
* $context when an action is executed. | |
*/ | |
function action_example_node_sticky_action_submit($form, $form_state) { | |
return array('author' => $form_state['values']['author']); | |
} | |
/** | |
* Action function for action_example_node_sticky_action. | |
* | |
* Promote and set sticky flag. This is the special action that has been | |
* customized using the configuration form, validated with the validation | |
* function, and submitted with the submit function. | |
* | |
* @param $node | |
* A node object provided by the associated trigger. | |
* @param $context | |
* Array with the following elements: | |
* - 'author': username of the author's content this function will promote and | |
* set as sticky. | |
*/ | |
function action_example_node_sticky_action($node, $context) { | |
if (function_exists('dsm')) { | |
dsm($node, 'action_example_node_sticky_action is firing. Here is the $node'); | |
dsm($context, 'action_example_node_sticky_action is firing. Here is the $context'); | |
} | |
// Get the user configured for this special action. | |
$account = user_load_by_name($context['author']); | |
// Is the node created by this user? then promote and set as sticky. | |
if ($account->uid == $node->uid) { | |
$node->promote = NODE_PROMOTED; | |
$node->sticky = NODE_STICKY; | |
watchdog('action', 'Set @type %title to sticky and promoted by special action for user %username.', array('@type' => node_type_get_name($node), '%title' => $node->title, '%username' => $account->name)); | |
drupal_set_message(t('Set @type %title to sticky and promoted by special action for user %username.', array('@type' => node_type_get_name($node), '%title' => $node->title, '%username' => $account->name))); | |
} | |
} | |
/** | |
* @} End of "defgroup action_example". | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment