0

I'm using the CER module in Drupal 7. Is there a way to create a preset programmatically?

0

[EDIT]

Almost a year later, I found a better way (the entity_import way actually did not work).

CERs are entities. The best way to create one is using $cer = entity_create('cer', array('bundle' => 'cer')); adding appropriate values to the fields of the created entity, and saving it by using entity_save('cer', $cer);

=======================

Finally figured it out. You can use the entity_import function.

  1. Create a variable containing a JSON-like string of the entity (which you can get by exporting a CER entity), something like:

    $j = '{ "identifier" : "node:time_slot:field_ref_student*commerce_line_item:registration:field_ref_slots", "cer_bidirectional" : { "und" : [ { "value" : "1" } ] }, "cer_enabled" : { "und" : [ { "value" : "1" } ] }, "cer_weight" : { "und" : [ { "value" : "0" } ] }, "cer_left" : { "und" : [ { "path" : "node:time_slot:field_ref_student" } ] }, "cer_right" : { "und" : [ { "path" : "commerce_line_item:registration:field_ref_slots" } ] }, "rdf_mapping" : [] }';

  2. Then import and save:

    $e = entity_import('cer', $j); entity_save('cer', $e);

0

I know it's an old question, but for anybody looking for a way to do this argiepano's answer is not very clear. Found that their first solution works just fine, and actually that's how it's done by the code that does the importing in the CER admin UI:

// Create the entity using the JSON string.
$entity = entity_import('cer', '{JSON STRING HERE}');
// Now do the actual saving.
entity_save('cer', $entity);

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.