Hi - Doing something like:

  $newnode = entity_create('node', array(
    'type' => 'participant',
    'nid' => 0,
    'uid' => $uid,
  ));
  $participant_wrapper = entity_metadata_wrapper('node', $newnode);

I expected that $participant_wrapper->field_some_field->value() would have all the fields defined by the bundle with their default values set, but not so. Is there a recommended way to set field defaults for creation of a new entity? Does it involve explicitly iterating over all the fields attached to that bundle, looking up their field definitions, and applying individually?

Thanks
-Bronius

Comments

texas-bronius created an issue. See original summary.

farald’s picture

Also interested in this, it seems cumbersome to replicate each and every one of the default values when creating a new entity.

farald’s picture

First get the field instances, then apply the default values using that information.
Here's how you get the default values, assuming you have just created a basic node object $node:

<?php
 $fields = field_info_instances('node','my-bundle');
  foreach($fields AS $fid => $field){
    $instance = field_info_instance('node',$fid,'my-bundle');
    $value = field_get_default_value('node', $node, $field, $instance);
    $node->{$fid}[LANGUAGE_NONE] = $value;
  }
?> 
farald’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.