You are here

function field_default_token_field_update_instance in Field default token 7

Implements hook_field_update_instance().

File

./field_default_token.module, line 11
Enables to use tokens as field default values.

Code

function field_default_token_field_update_instance($instance, $prior_instance) {
  $has_tokens = FALSE;
  if (!empty($instance['default_value'])) {
    foreach ($instance['default_value'] as $values) {
      if (!is_array($values)) {
        if (strpos($values, '[') !== FALSE) {
          $has_tokens = TRUE;
        }
      }
      else {
        foreach ($values as $value) {
          if (!is_array($value)) {
            if (strpos($value, '[') !== FALSE) {
              $has_tokens = TRUE;
            }
          }
          else {
            foreach ($value as $column_value) {
              if (is_array($column_value)) {

                // Complex fields such as Table Field contain a structure inside the value, skip them.
                continue;
              }
              if (strpos($column_value, '[') !== FALSE) {
                $has_tokens = TRUE;
              }
            }
          }
        }
      }
    }
  }
  $update = FALSE;
  if (empty($instance['default_value_function'])) {
    if ($has_tokens) {
      $instance['default_value_function'] = 'field_default_token_default_value_function';
      $update = TRUE;
    }
  }
  elseif ($instance['default_value_function'] == 'field_default_token_default_value_function') {
    if (!$has_tokens) {
      unset($instance['default_value_function']);
      $update = TRUE;
    }
  }
  if ($update) {

    // Save the instance and clear caches again without hook invoking.
    // @see field_update_instance()
    _field_write_instance($instance, TRUE);
    field_cache_clear();
  }
}