1

I want to display a value of a field which the user has no access to in a dsm when the user edits the page. The dsm is showing up after saving the node, and while that's fine (I'd even consider it a feature) it's showing the before-edit value of the node.

I believe I've run into this problem but there doesn't seem to be an easy fix. Here's a look at the template.php function.

function themename_form_vakature_node_form_alter(&$form){
    if(isset($form['#node']->field_vacature_scheduled_for_unp)){
        if($form['#node']->field_vacature_scheduled_for_unp['und'][0]['value']){
            $expirydate = date("F j Y",$form['#node']->field_unpublish_date['und'][0]['value']);

            if($form['#node']->field_unpublish_date['und'][0]['value'] < time()){
                drupal_set_message(t("This vacature has expired."),'status',FALSE);
            }
            else{
                drupal_set_message(t("This vacature will expire at: ").$expirydate,'status',FALSE);
            }
        }
    }
}

Basically, the message displayed on the edit page is repeated after a save. I need it to either not repeat or show the correct updated information.

1 Answer 1

0

Form alter changes the form when it's being built for display.

If you want to use the value on the node after update, use a hook like

instead.

To tweak your code snippet, something like:

/**
 * Implements hook_node_presave
 */
function mymodule_node_presave($node){
  if ($items = field_get_items('node', $node, 'field_vacature_scheduled_for_unp')) {
    $vacature = reset($items); // Use first field item value
    $expirydate = date("F j Y", $vacature['value']);
    $items = field_get_items('node', $node, 'field_unpublish_date')
    $unpublish = reset($items); // Use first field value
    if ($unpublish['value'] < time()) {
      drupal_set_message(t("This vacature has expired."), 'status', FALSE);
    }
    else{
      drupal_set_message(t("This vacature will expire at: "). $expirydate, 'status', FALSE);
    }
  }
}
5
  • This looks promising but how would I show the value on the edit page without it appearing twice (That's more important than getting it to show after saving)
    – J V
    Jan 31, 2013 at 11:10
  • If you want to show the message when the user goes to edit the node, use hook_node_load instead and check if on edit page, e.g arg(0) == 'node' and arg(2) == 'edit' Jan 31, 2013 at 11:19
  • Thanks! I'll still have to do some work to get things working properly but I should be able to get it from here!
    – J V
    Jan 31, 2013 at 11:23
  • By the way, is there a way to do this in the theme rather than a module?
    – J V
    Jan 31, 2013 at 12:27
  • hook_node_load triggers twice while saving the node. Now I have 2x the old value and 1x the new one.
    – J V
    Jan 31, 2013 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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