3

The updated date of the content is displayed on each node with this code in template file.

<?php print strftime( "%Y-%m-%d", $node->changed) ?>

Now when I save the content with admin role the date displayed is the date I updated (of course). But I only want to display the date updated by the author of the content. So how can I prevent admin role from changing "updated date" and only keep the date updated by the content author? Or is there a way to display only the updated date by the author of content?

1

Like Clive mentioned there is no good/easy way to prevent the node change timestamp from being updated, but as a possible alternative you could use node creation time instead:

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'presave':
      global $user;
      if ($node->type == 'mynodetype' && !in_array('administrator', $user->roles)) {
        $node->created = time();
      }
      break;
  }
}

This implementation of hook_nodeapi() would set the 'mynodetype' node creation time to the current time when a non-administrator saves it. You'll have to keep in mind that users can manually set the creation time when editing nodes and this will override it for non-administrators. It will also mean that in those cases the actual node creation time will be lost even if the user isn't modifying it.

  • Thanks. I'm not sure if I understand this or not.. So basically the original updated time will be kept as usual and the node creation time will be something like "the updated type by none admin users" then original node creation time will be lost as a consequence. If I got it right I might be able to achieve what I've been looking for. Because this content type is "content profile" so I don't really need to keep the node creation time as user creation time is nearly the same thing. By the way it's also possible to implement this only to one content type? – chinita7 Feb 27 '12 at 19:22
  • Yeah you understand that correctly and you can restrict it to a specific content type by checking the $node->type value (I added this to my answer). – Madis Feb 27 '12 at 19:49
  • Can you help me a bit more please? I don't know why but also admin role is changing the "node creation time" when save the content.. So what about letting only "node author" change "node creation time"? – chinita7 Feb 29 '12 at 14:41
  • It's working for me so you may want to recheck that the names used in the code fit with the ones on your site. Anyway to check for node author instead you should switch out the current if statement with if ($node->type == 'mynodetype' && $node->uid == $user->uid). – Madis Feb 29 '12 at 18:44
  • Now it works perfectly for my need. I realized letting only the node author makes more sense than preventing the admin role.Because like sub admin role also will not change it. Thanks a lot ! – chinita7 Mar 1 '12 at 2:36
2

You can't really, it's hard coded into the node_save() function with this bit of code:

$time = time();
if (empty($node->created)) {
  $node->created = $time;
}

// The changed timestamp is always updated for bookkeeping purposes (revisions, searching, ...)
$node->changed = $time;

Short of implementing your own node form which bypasses node_save() there's not really much you can do (except hacking the core node.module file which is never advised).

  • Thanks for letting me know. So then probably the only way I can do is to replace "last updated date" with "last login date of the user" .. – chinita7 Feb 27 '12 at 13:14
1
<?php if (($logged_in)&&($user->uid != "1")): ?>
<?php print $submitted; ?>
<?php endif; ?>
  • 1
    I'm not talking "who is logged in" but "who has updated the content".. Sorry I had the wrong code by mistake above so I edited it. and what I have on my template now is <?php print strftime( "%Y-%m-%d", $node->changed) ?> – chinita7 Feb 27 '12 at 8:59
0

In order to prevent changes to nodes by administrators from updating the changed date, you can make a module with the following code. The way this works is when an administrator saves a node, the new changed date is replaced with the old changed date.

<?php

/**
 * Implements hook_node_presave().
 * @param $node
 */
function YOURMODULE_node_presave($node) {
  global $user;
  if (in_array('administrator', $user->roles)) {
    $node->changed = $node->original->changed;
  }
}

The above code will prevent the date from updating on all node types. If you only want this to apply to specific node types, try something like the code below. You might want to extract the $types array into a module configuration form.

<?php

/**
 * Implements hook_node_presave().
 * @param $node
 *
 * Add node types to $types array to freeze their changed date.
 */
function YOURMODULE_node_presave($node) {
  $types = array(
    'your_node_type',
    'node_type_2'
  );
  if (in_array($node->type, $types)) {
    $node->changed = $node->original->changed;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.