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 ?

  • What type of field is field_ip? – argiepiano Jan 3 '16 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 Jan 3 '16 at 1:53
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. :)

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

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.