pm.field.inc in Drupal PM (Project Management) 7.2
Field creation functions.
File
includes/pm.field.inc
View source
<?php
define('PM_FIELD_BASE_CREATE_FAILURE', 0);
define('PM_FIELD_BASE_CREATE_SUCCESS', 1);
define('PM_FIELD_BASE_UPDATE_SUCCESS', 2);
define('PM_FIELD_INSTANCE_CREATE_FAILURE', 0);
define('PM_FIELD_INSTANCE_CREATE_SUCCESS', 1);
define('PM_FIELD_INSTANCE_UPDATE_SUCCESS', 2);
function pm_field_bases_create_if_required(array $field_bases) {
$result = array();
if (!empty($field_bases) and is_array($field_bases)) {
foreach ($field_bases as $key => $field_base) {
$field_name = $field_base['field_name'];
$result[$key] = pm_field_base_create_if_required($field_name, $field_base);
}
}
return $result;
}
function pm_field_base_create_if_required($field_name, $field_base) {
try {
$field = field_info_field($field_name);
if (empty($field)) {
$field = field_create_field($field_base);
return PM_FIELD_BASE_CREATE_SUCCESS;
}
else {
field_update_field($field_base);
return PM_FIELD_BASE_UPDATE_SUCCESS;
}
} catch (Exception $e) {
watchdog('pm', 'See ' . __FUNCTION__ . '() ' . $e
->getTraceAsString(), NULL, WATCHDOG_ERROR);
}
return PM_FIELD_BASE_CREATE_FAILURE;
}
function pm_field_instances_create_if_required(array $field_instances) {
$result = array();
if (!empty($field_instances) and is_array($field_instances)) {
foreach ($field_instances as $key => $field_instance) {
$field_name = $field_instance['field_name'];
$result[$key] = pm_field_instance_create_if_required($field_name, $field_instance);
}
}
return $result;
}
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'];
try {
if ($field) {
$instance_info = field_info_instance($entity_type, $field_name, $bundle_name);
if (empty($instance_info)) {
field_create_instance($field_instance);
return PM_FIELD_INSTANCE_CREATE_SUCCESS;
}
else {
$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;
}