7

I created an entity via Entity Construction Kit, Initially I wanted to use rules to create the new entity but the entity is fielded and the fields don't get exposed in rules (as of now, someone is trying to work on a patch for that), but in the interm I wanted to write a simple function that creates a entity, which I assumed would be somewhat like creating a node programmatically, but I've been at this for like 4 hours and I haven't gained an inch, I have entity_create and entity_save but they don't do much and I can't find any documentation on it.

What I'd like to see is how to create an example entity from a bundle made with eck, or through code, that is fielded.

Thanks so much

Edit: The last thing I need to figure out is how to specify the name of the fields that are attached to the entity so I can enter values for those as well.

$values = array('uid' => 1, 'field_receipt_prod_ref' => 1, 'type' => 'receipt');        
$new_ent = entity_create('receipt', $values)->save();
1

2 Answers 2

15

Most of what you have doesn't look too bad. The key thing with fields that you are missing is that they have a language, that they can be multi-valued, and that the value is generally stored in a 'value' subkey. I'm assuming that the ref field is a nodereference, which means it's actually stored with nid instead of value.

The easiest thing you can do to see how this is structured is to install the Devel module, and use the Devel tab that to look at the structure. Not sure if that shows up with ECK entities, but it does for Nodes.

Try this:

$new_ent = entity_create('receipt', array(
  'uid' => 1,
  'type' => 'receipt', //bundle
  'field_receipt_prof_ref' => array(      
    LANGUAGE_NONE => array(
      0 => array(
        'nid' => 1,
      ),
    ),
  ),
))->save();
0
27

The simpler approach is with the help of entity_metadata_wrapper().

$e = entity_create($entityType, array('type' => $bundleName));
$ew = entity_metadata_wrapper($entityType, $e);
$ew->field_name->set('value');
$ew->save();
0

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.