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 Actions configuration page and assigned to triggers on the Triggers configuration page.", array('@actions_url' => url('admin/config/system/actions'), '@triggers_url' => url('admin/structure/trigger/node'))); } /** * Action function for action_example_basic_action. * * This action is not expecting any type of entity object, and can be used with * any trigger type or any event. * * @param object $entity * An optional entity object. * @param array $context * Array with parameters for this action: depends on the trigger. * * @see action_example_action_info() */ 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.'); } /** * Action function for action_example_unblock_user_action. * * This action is expecting an entity object user, node or comment. If none of * the above is provided (because it was not called from an user/node/comment * trigger event), then the action will be taken on the current logged in user. * * Unblock an user. This action can be fired from different trigger types: * - User trigger: this user will be unblocked. * - Node/Comment trigger: the author of the node or comment will be unblocked. * - Other: (including system or custom defined types), current user will be * unblocked. (Yes, this seems like an incomprehensible use-case.) * * @param object $entity * An optional user object (could be a user, or an author if context is * node or comment) * @param array $context * Array with parameters for this action: depends on the trigger. The context * is not used in this example. */ 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))); } /** * Form function for action_example_node_sticky_action. * * Since we defined action_example_node_sticky_action as 'configurable' => TRUE, * this action requires a configuration form to create/configure the action. * In this circumstance, Drupal will attempt to call a function named by * combining the action name (action_example_node_sticky_action) and _form, in * this case yielding action_example_node_sticky_action_form. * * In Drupal, actions requiring creation and configuration are called 'advanced * actions', because they must be customized to define their functionality. * * The 'action_example_node_sticky_action' allows creating rules to promote and * set sticky content created by selected users on certain events. A form is * used to configure which user is affected by this action, and this form * includes the standard _validate and _submit hooks. */ /** * Generates settings form for action_example_node_sticky_action(). * * @param array $context * An array of options of this action (in case it is being edited) * * @return array * Settings form as Form API array. * * @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 object $node * A node object provided by the associated trigger. * @param array $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". */