7.x field.module field_get_default_value($entity_type, $entity, $field, $instance, $langcode = NULL)

Helper function to get the default value for a field on an entity.

Parameters

$entity_type: The type of $entity; e.g., 'node' or 'user'.

$entity: The entity for the operation.

$field: The field structure.

$instance: The instance structure.

$langcode: The field language to fill-in with the default value.

Related topics

2 calls to field_get_default_value()
field_default_form in modules/field/field.form.inc
Creates a form element for a field and can populate it with a default value.
field_default_insert in modules/field/field.default.inc
Default field 'insert' operation.

File

modules/field/field.module, line 483
Attach custom data fields to Drupal entities.

Code

function field_get_default_value($entity_type, $entity, $field, $instance, $langcode = NULL) {
  $items = array();
  if (!empty($instance['default_value_function'])) {
    $function = $instance['default_value_function'];
    if (function_exists($function)) {
      $items = $function($entity_type, $entity, $field, $instance, $langcode);
    }
  }
  elseif (!empty($instance['default_value'])) {
    $items = $instance['default_value'];
  }
  return $items;
}

Comments

ivanjaros’s picture

Seems the 'default_value_function' is missing in field settings form or is hidden from users anyway.

clintu’s picture

Rob230’s picture

Note that this does not work for image fields where the default is set on the field rather than the instance.

LukeEL’s picture

I need to do this on the front page, i.e. when there is no node.

cmonnow’s picture

Sorry I'm on my mobile but you can try something like what's shown here: http://drupal.stackexchange.com/questions/100507/get-the-default-image-f...

$info = field_info_instance('node', 'field_image', 'machine_name_content_type');
if ( !empty( $info ) && isset($info['settings']['default_image']) && $info['settings']['default_image'] > 0 ) {
$default_fid = $info['settings']['default_image'];
$default_img_object = file_load( $default_fid );
}

Then you can use the object's uri with the image_style_url() function for the link or theme('image_formatter', $options) for the rendered image.

kerby70’s picture

From discussion https://www.drupal.org/node/1109270 and example http://drupal.stackexchange.com/questions/60259/how-to-reset-a-form-element-to-its-ordinary-default-value

In this example YOURNODETYPE is the bundle name and 'node' is the entity type machine name, $node is the entity object.

  $field = field_info_field('YOURFIELDNAME');
  $instance = field_info_instance('node', 'YOURFIELDNAME', 'YOURNODETYPE');
  $defvalue = field_get_default_value('node', $node, $field, $instance, $node->language);
  print_r($defvalue); // Here structure, like $defvalue[0]['value']

Summary:

  • $entity_type is a string the entity type of $entity; e.g., 'node' or 'user'
  • $entity is an entity object of the entity. Like that returned by node_load().
  • $field is an array structured value. Like that returned by field_info_field()
  • $instance is an array structured value. Like that returned by field_info_instance()
  • $langcode is a Drupal language code.

In my case $entity_type was found at $entity->entityType not to be confused with $entity->type which is the bundle. And I needed to set the field to the default via $entity->YOURFIELDNAME[LANGUAGE_NONE] = $defvalue;, so keep in mind the default value starts below the language level in the structured array, when applicable.