Last updated March 19, 2012. Created by bneijt on January 21, 2008.
Edited by bfr. Log in to edit this page.

This code defines a module that exposes a block and a trigger. The trigger will be fired when the special trigger block is shown. The trigger block itself only contains HTML comments.

The reason this could come in handy, is if you want a really finely timed trigger (more resolution than cron) for light actions like counters etc. One example of this is a special kind of light trigger that will allow you to automatically publish content like Schedular, but which is checked every time a user views the block. It is a bit of a hack, but should allow simple uses and blog sites to enjoy it.

First the module definition triggerblock.info:

; $Id$
name = Trigger block
description = A block that can trigger when viewed.
dependencies[] = trigger
core = 6.x

This makes sure we have trigger to work with. Now the code for the module triggerblock.module:

<?php
/*
    Triggerblock allows you to trigger when the triggerblock is shown.
    Copyright (C) 2008  A. Bram Neijt <bram@neijt.nl>
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
Triggerblock allows you to trigger when the triggerblock is shown.
It also adds a new action which will publish old posts that are both sticky and promoted.
The sticky bit will be set to 0;
This allows you to get a node published after the authoring date only by
setting it promoted, unpublished and sticky.
*/
/**
* Implements hook_perm().
*/
function triggerblock_perm() {
  return array(
'access triggerblock');
}
/**
* Implements hook_block().
*/
function triggerblock_block($op = 'list', $delta = 0) {
  if (
$op == 'list') {
   
$block[0]['info'] = t('Allows you to trigger when the block is shown');
    return
$block;
  }
  elseif (
$op == 'view') {
    if (
user_access('access triggerblock')) {
     
module_invoke_all('triggerblock', $op);
     
//As module invoke all won't nudge trigger, we do it.
     
$aids = _trigger_get_hook_aids('triggerblock', $op);
     
$context = array(
       
'hook' => 'triggerblock',
       
'op' => $op,
      );
     
$object = NULL;
     
actions_do(array_keys($aids), $object, $context);
      return array(
'subject' => '', 'content' => '<!-- trigger block -->');
    }
  }
}
/**
* Implementation of hook_hook_info().
*/
function triggerblock_hook_info() {
  return array(
   
'triggerblock' => array(
     
'triggerblock' => array(
       
'view' => array(
         
'runs when' => t('The triggerblock is viewed.'),
        ),
      ),
    ),
  );
}
/**
* Implementation of hook_action_info().
*/
function triggerblock_action_info() {
  return array(
   
'triggerblock_publish_old_promoted_sticky_action' => array(
     
'description' => t('Publish and unstick any node that is old, sticky and promoted'),
     
'type' => 'node',
     
'configurable' => FALSE,
     
'hooks' => array(
       
'any' => TRUE,
      ),
    ),
  );
}
/**
* Implementation of a Drupal action.
* Blocks the current user.
*/
function triggerblock_publish_old_promoted_sticky_action(&$object, $context = array()) {
 
db_query('UPDATE {node} SET sticky=0, status=1 WHERE sticky = 1 AND status = 0 AND promote = 1 AND created <= UNIX_TIMESTAMP()');
}
The last function is the action, which published unpublished but sticky old and promoted content and removed it's sticky bit (cleans it).
Because the Trigger module doesn'
t know about every hook that is called, we need to do our own actions_do call. The module_invoke_all is only there because it should be done if you expose the hook, but as of now it does nothing really.
Now for the install file, make sure you clean up an initialize everything ok. Although it's not always needed (not sure, but a cron might clean the actions table now and then, but better be safe about this). The <strong>triggerblock.install</strong>
<?php
/**
* Implementation of hook_install().
*/
function triggerblock_install() {
  actions_synchronize(FALSE, TRUE);
}
/**
* Implementation of hook_uninstall().
*/
function triggerblock_uninstall() {
  actions_synchronize(FALSE, TRUE);
}
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.