Anyone know if there's a way to allow users without "administer nodes" checked to actually be able to change the "Authored on" date of their stories? Or a way to limit "administer nodes" to just a user's own (self created) nodes?

I have a sitie where I need to allow users to post stories from/to either the past or future— very rarely on the same date as when they're logged in and create the story. I can see how this is done when "access control->administer nodes" is checked, but then that also seems to allow a user to modify any other user's stories, which I definitely don't want.

I've searched around and found the following possibly related posts, which all seem to say the answer might be no, but I'm really hoping there's a solution out there since Drupal does everything else so well:

Any help much appreciated!

Thanks,
KV

Comments

pwolanin’s picture

You probably should just do this via a small module using hook_form_alter.

See the forms API for more info: http://api.drupal.org/api/4.7/file/developer/topics/forms_api.html

As well as this mini module, for example: http://drupal.org/node/70906

The code below is adapted pretty much straight from the node.module (Drupal 4.7). This is the sort of code you'd put in your hook_form_alter implementation to add the date field for non-admin users to the node form:

  if (!user_access('administer nodes')) { //don't conflict with the core code
    $form['date'] = array(
      '#type' => 'textfield', 
      '#title' => t('Authored on'), 
      '#maxlength' => 25, 
      '#description' => t('Format: %time. Leave blank to use the time of form submission.', 
                        array('%time' => $node->date)));
    $node = $form['#node']; //a trick here- the node is stored in the form in function node_form_array
    if (isset($node->nid)) {
      $form['date']['#default_value'] = $node->date;
    }
}

note- I haven't actually tested this code; Caveat Emptor.

---
Work: BioRAFT

petervm’s picture

Hi,
I have a similar problem: I want to allow visitors (with a certain role) to add pages and indicate under which menu item this page is to be shown without giving them access to the entire menu system. Would you have a solution to that too??

Peter

pwolanin’s picture

Well, perhaps - first you should try out the code above and see if it actually works! You'll have to look at the menu module itself- it may be that without giving the "administer menus" permission, users can never add a menu item.

---
Work: BioRAFT

krisvannest’s picture

Thanx very much for the fast & detailed feedback; I'm not at all familiar with the forms API (or even where this hook code would go), so I've got some reading to do.

But in case it's frankly over my head, also wondering if there's not a plugin or module that does something similar w/ less engineering (not being a hard-core coder myself)? I just ran across the Actions module and wonder if I added and exposed a custom vocab of mm/dd/yyyy to a story, perhaps I could have the Actions and/or Workflow automatically (behind the scenes to the user) set a story's publish date to that custom-vocab date... so the user would just enter a value in my new "To be published on date:" field for that story and the module would take it from there. I'm not very familiar w/ either Actions or Workflow yet so will check into that as well.

Ya know i'm sorta surprised that Drupal doesn't have an out-of-box feature like this because it seems like such a common feature to mainstream blog apps. But BIG kudos to the Drupal community for making such a powerful tool; i'm constantly surprised by just what IS possible (and often so fast)— rather amazing. In fact, this is actually the 1st real non-trivial problem for me w/ this site, and I've added quite a few behind-the-scenes features ('course as Murphy would have it, it's a must-have feature requirement for my site).

Thanks again,
KV

--note: just found incompatability error between my VotingAPI and Actions module; will research

krisvannest’s picture

OK, in order to pretend I know what I'm doing before adding hook_form stuff, I thought I'd read up first— started at http://drupal.org/node/17914 and went through a few tutorial steps there. That's some heavy reading! Actually, it's not too bad and pretty well explained (to contributors: thank you). But I can see it's going to take me (non-programmer) some significant time to make sure I know enough to feel comfortable implementing the above hook_form_alter implementation w/out fear of breaking my site and/or giving non-admins too much inadvertant access.

Some newbie clarification questions on the above code:

  • Filename— what name would I give this small module?
  • Location— from the http://drupal.org/node/17914 docs, I gather I'd put it straight into my site's /module directory.
  • Security— does anyone know if there are, in fact, any security probs (e.g., allowing non-admins backdoor access to restricted/admin content or functions) with this implementation?

BTW, I have a feeling this level of PHP/coding is a bit over my head, so if anyone has an existing small module or other code available, please feel free to post. :)

Thanks
KV

pwolanin’s picture

see: http://drupal.org/node/70906

This includes the code for an entire small module that modifies the node form via hook_form_alter.

---
Work: BioRAFT

tqvn2004’s picture

This is the code allowing people with no 'node administer' right to access 'Authored on' (and possible 'Authored by') field:

/**
 * Implementation of hook_form_alter().
 */
function story_form_alter($form_id, &$form) {
 if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {	
  if (!user_access('administer nodes')) { //don't conflict with the core code
      $node = $form['#node'];
      if (!isset($node->created)) {
     	$node->created = time();
      }
      if (!isset($node->date)) {
        $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
      }
      // Node author information
      $form['author'] = array(
        '#type' => 'fieldset', 
        '#title' => t('Authoring information'), 
        '#collapsible' => TRUE, 
        '#collapsed' => TRUE, 
        '#weight' => 20);
        
      //$form['author']['name'] = array(
      //  '#type' => 'textfield', 
      //  '#title' => t('Authored by'), 
      //  '#maxlength' => 60, 
      //  '#autocomplete_path' => 'user/autocomplete', 
      //  '#default_value' => $node->name ? $node->name : '', 
      //  '#weight' => -1, 
      //  '#description' => t('Leave blank for %anonymous.', array('%anonymous' => theme('placeholder', variable_get('anonymous', 'Anonymous')))));
    
      $form['author']['date'] = array(
        '#type' => 'textfield', 
        '#title' => t('Authored on'), 
        '#maxlength' => 25, 
        '#description' => t('Format: %time. Leave blank to use the time of form submission.', array('%time' => $node->date)));

      if (isset($node->nid)) {
        $form['author']['date']['#default_value'] = $node->date;
      }    
  }
 }
}

/**
 * Implementation of hook_submit().
 */
function story_submit(&$node) {
  if (!user_access('administer nodes')) { //don't conflict with the core code
      // Populate the "authored by" field.
      //if ($account = user_load(array('name' => $node->name))) {
      //  $node->uid = $account->uid;
      //}
      //else {
      //  $node->uid = 0;
      //}
      
      // Populate the "authored on" field
      $node->created = $node->date ? strtotime($node->date) : NULL;
  }
}

Note that the hook_submit() is used to update the $note->uid and $node->created variable, as the node_submit() does not automatically do this! Hope this help!

krisvannest’s picture

Huan, thanks much... this almost works!

Actually it seems to work fine for Story nodes, but I'm using CCK nodes (custom node) and when I change your code above to reference the content, the 1st function (in my case, I change it to "content_form_alter") does display the Authoring information field for entering the date, BUT the 2nd function (in my case, when I change it to "content_submit") throws an error:

    Fatal error: Cannot redeclare content_submit() (previously declared in /home/modules/...) ... on line 293

And, as you suggested, when i remove the 2nd function my CCK content.module does display the Authoring information field for non-admins, but does not actually save/update the node with the edited info.

I looked up the line in content.module in my site and it does already have a content_submit() function defined, so I'm thinking about just taking your 2nd function above and dropping it into my content.module. But not sure what that might mess up so wanted to ask if there might be another way to do it-- like is there a way to modify the existing content_submit() function from my own module and not edit the content.module (or should I just do the latter)? I searched the handbook for something like submit_alter but didn't see it available.

Thanks again,
KV

krisvannest’s picture

well meanwhile i got brave and tried it... added your following code to content.module as the 1st function inside my content.module's content_submit(&$node) function:

 if (!user_access('administer nodes')) { //don't conflict with the core code
// Populate the "authored by" field.
//if ($account = user_load(array('name' => $node->name))) {
// $node->uid = $account->uid;
//}
//else {
// $node->uid = 0;
//}

// Populate the "authored on" field
$node->created = $node->date ? strtotime($node->date) : NULL;
}

Seems to work!

I'm still a bit concerned that i may have created unwanted side-effects, so please feel free to reply if you think there's a better way (see my previous post here).

BTW, I'm still quite surprised it seems not many other Drupal devs have a need to allow their non-admin users to enter/edit creation dates for their own nodes as i thought back-dating of nodes was a pretty popular feature of many blog, etc sites? Maybe not, but here's hoping to someone implementing it for Drupal v5 or perhaps a universal module for those of us that do (i'd take it on myself if I knew more about PHP/Drupal coding).

HappyPike’s picture

I also hope they would add the ability to allow admins to define which groups can specify the "author by" and "author on" info w/o having to grant them administer nodes privileges.

krisvannest’s picture

NICE!!! :-)

Well, I actually haven't tried your mod yet 'cause I have things working at the moment with my hack... er, patch... but I was looking for a solution for so long that I just wanted to say your contrib is MUCH appreciated-- if not by myself, then I'm sure by other newbies as IMO this seemed like a small but very important function for Drupal to be missing.

David Lesieur’s picture

ashlian’s picture

I'm interested in this module because I think it would let me have a pseudo-administrator (a user with the role of Facilitator) CREATE NODES of content type "group" (used by the Organic Groups module) AND SWITCH THE AUTHOR to the group's manager (another user with the role of Contributor Manager).

But, I'm running Drupal 5.1. I know there are some pages on upgrading modules, but I'm not sure how I can help...

Convoluted solution to change OG Manager:
http://drupal.org/node/81318

Contrib modules version 5.x status:
http://drupal.org/node/82257

Converting 4.7.x modules to 5.x:
http://drupal.org/node/64279

"Nothing easy is ever simple." -- Plucky Duck

gusaus’s picture

I'm a bit surprised that there's not an easy solution for 5.x. Am I missing something?

---------------------------------------
Gus Austin
PepperAlley Productions

---------------------------------------
Gus Austin

seakayjay’s picture

I'm looking forward for this as well. How about version 5.1? how to port?

finex’s picture

The module doesn't work with Drupal 5.x. I've filled an issue: http://drupal.org/node/187336.

finex’s picture

Ok, after some test I've found that the module works if the taxonomy access control is not enabled: http://drupal.org/node/187336

therous’s picture

I am not sure why something as basic as specifying the published date, or having a separate notion of "published" and "created" date, is not part of the core functionality of Drupal, but with this module I can get this behaviour without granting administrative permissions to users.

Thanks so much and keep up the great work!

yngens’s picture

is there something similar, but for work flow states?