0

I have an entityform with a field call "IP" (field_ip is its machine name). Now I need to set a value before that the entityform is saved. I have thinked to use hook_entity_presave() in this way:

function mymodule_entity_presave($entity, $type) {
      if ($type == 'entityform') {
        if ($entity->is_new === true || 1==1) {
          $entity->field_ip = array('LANGUAGE_NONE' => array(0 => array('value' => $_SERVER["REMOTE_ADDR"],'format' => '','safe_value' => $_SERVER["REMOTE_ADDR"])));
          $entity->original->field_ip = $entity->field_ip;   //Try this, too.
        };
      }
    }

(1==1 is only for test/debug).

Via "devel" module I can see that the new value of field_ip is correctly set, but when I see the new version of the entityform entry (after the save action) I see the default value of that field and not my new value. And infact if I re-edit the same entyform (id) I see the old value and not the new one. Where is my error ?

2
  • What type of field is field_ip?
    – argiepiano
    Commented Jan 3, 2016 at 1:34
  • I tried something similar to this, and it's working fine with a text field in entityform. If field_ip is a Text field, you only need to assign its 'value' (text fields do not have 'format' or 'safe_value' properties)
    – argiepiano
    Commented Jan 3, 2016 at 1:53

1 Answer 1

1

OK I now see what the problem is. LANGUAGE_NONE should not have quotes around it. It's a constant. Been there, done that. :)

1
  • Thanks. I have view and review my function more and more times but I have never see quotes around LANGUAGE_NONE.
    – ZioBudda
    Commented Jan 3, 2016 at 5:21

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.