[development] Edit restriction

Brian Vuyk brian at brianvuyk.com
Wed Dec 2 21:58:30 UTC 2009


This one wouldn't be that bad.

Just use something like the below in addition to an admin form that lets 
the user set the 'yourmodule_edit_expire' variable.

Brian

function yourmodule_perm() {
  return array('ignore edit time restriction');
}

function yourmodule_menu_alter(&$items) {
  $items['node/%node/edit']['access callback'] = 
'yourmodule_access_callback';
  $items['node/%node/edit']['access arguments'] = array('update', 1);
}

function yourmodule_access_callback($op, $nid) {
  if (is_int($nid)) {
    $created = db_result(db_query('SELECT created FROM {node} WHERE nid 
= %d', $nid));
   
    // Created will be FALSE if the node doesn't exist, or a positive 
integer if it does.
    if ($created) {

      // Deny edit access if we are past the edit expiry time.
      $expiry_time = $created + variable_get('yourmodule_edit_expire', 0);
      if (time() < $expiry_time && !user_access('ignore edit time 
restriction') {
        return FALSE;
      }
    }
  }
 
  // Now send through the normal node_access check.
  return node_access($op, $nid);
}

Jeff Greenberg wrote:
> The last interesting requirement is sort of like creating a new post 
> in a forum, in that the node, once created, can be edited, based on 
> permissions, like normal, but only for an amount of time selectable 
> (globally) in the admin panel. So the user who creates the node can 
> edit it for 12 hours, after which the ability to edit it goes away.
>



More information about the development mailing list