You are here

function pm_field_instance_create_if_required in Drupal PM (Project Management) 7.2

Same name and namespace in other branches
  1. 8 includes/pm.field.inc \pm_field_instance_create_if_required()
  2. 7.3 includes/pm.field.inc \pm_field_instance_create_if_required()

Create specific field instance if not present.

1 call to pm_field_instance_create_if_required()
pm_field_instances_create_if_required in includes/pm.field.inc
Iterate through an array of field instances, creating if not present.

File

includes/pm.field.inc, line 84
Field creation functions.

Code

function pm_field_instance_create_if_required($field_name, $field_instance) {
  $field_name = $field_instance['field_name'];
  $entity_type = $field_instance['entity_type'];
  $bundle_name = $field_instance['bundle'];
  $field = field_info_field($field_name);
  $entity_type = $field_instance['entity_type'];

  // Create only if field is defined.
  try {
    if ($field) {

      // Try to load field_instance if present.
      $instance_info = field_info_instance($entity_type, $field_name, $bundle_name);

      // Create ony if instance is not present.
      if (empty($instance_info)) {
        field_create_instance($field_instance);
        return PM_FIELD_INSTANCE_CREATE_SUCCESS;
      }
      else {

        // Update field_instance if already present.
        $merged_settings = $field_instance + $instance_info;
        field_update_instance($merged_settings);
        return PM_FIELD_INSTANCE_UPDATE_SUCCESS;
      }
    }
  } catch (Exception $e) {
    watchdog('pm', 'See ' . __FUNCTION__ . '() ' . $e
      ->getTraceAsString(), NULL, WATCHDOG_ERROR);
  }
  return PM_FIELD_INSTANCE_CREATE_FAILURE;
}