1

I had another question at Save data to custom field columns using entity_metadata_wrapper and a similar issue at https://drupal.org/node/2110161, which nobody has been able to figure out yet.

Since entity_metadata_wrapper isn't working for writing custom field values, how can I do this manually? I've tried the following code, but when I view the node afterwards, the field value is blank.

$fieldname = 'field_device_id';
    $query = db_insert('field_data_' . $fieldname)
        ->fields(array(
          'entity_type' => 'node',
          'bundle' => 'equipment',
          'entity_id' => $new_ent->getIdentifier(),
          'language' => 'und',
          'delta' => 0,
          $fieldname . '_esn_hex' => isset($conversion_values['ESN_HEX']) ? $conversion_values['ESN_HEX'] : NULL,
          $fieldname . '_esn_dec' => isset($conversion_values['ESN_DEC']) ? $conversion_values['ESN_DEC'] : NULL,
          $fieldname . '_meid_hex' => isset($conversion_values['MEID_HEX']) ? $conversion_values['MEID_HEX'] : NULL,
          $fieldname . '_meid_dec' => isset($conversion_values['MEID_DEC']) ? $conversion_values['MEID_DEC'] : NULL,
          $fieldname . '_imei_hex' => isset($conversion_values['IMEI_HEX']) ? $conversion_values['IMEI_HEX'] : NULL,
          $fieldname . '_imei_dec' => isset($conversion_values['IMEI_DEC']) ? $conversion_values['IMEI_DEC'] : NULL,
          $fieldname . '_iccid_hex' => isset($conversion_values['ICCID_HEX']) ? $conversion_values['ICCID_HEX'] : NULL,
          $fieldname . '_iccid_dec' => isset($conversion_values['ICCID_DEC']) ? $conversion_values['ICCID_DEC'] : NULL,
          $fieldname . '_raw_id' => $id,
        ))
        ->execute();
  }

This code runs after a submit handler passes in a device id that a user has entered, and converts it to several different formats, which are available in $conversion_values.

There has got to be a way to programmatically write values to custom field columns defined in hook_schema.

1

As far as the procedural method is concerned, you would use exactly the same method for a custom field as you would a contrib/core field.

Core field:

$node->body[LANGUAGE_NONE][0] = array(
  'value' = '<p>Some body text</p>',
  'format' => 'full_html',
);

Your custom field:

$node->field_whatever[LANGUAGE_NONE][0] = array(
  'esn_hex' => $some_val,
  'esn_dec' => $some_other_val,
  // etc...
);

To persist changes to both, use node_save() (or entity_save(), or whataver helper method the entity you're using provides).

node_save($node);

That's all you need to do. Manipulating the field tables manually is not recommended, as you bypass a lot of hooks that need to run, cache management, etc.

To use custom fields with an EntityMetadataWrapper, you need to go a bit further and describe the field and its columns to the entity module.

This involves adding property_type and property_callbacks to your field definition in hook_field_info(), and providing appropriate callbacks for those. There's a bit more information in this post

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.