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);
}
}
foreach ($result as $key => $status) {
$variables = array(
'@field_name' => $field_bases[$key]['field_name'],
);
switch ($status) {
case PM_FIELD_BASE_CREATE_FAILURE:
$message = 'Failed to create field base - @field_name';
watchdog('pm', $message, $variables, WATCHDOG_ERROR);
throw new Exception("Drupal PM: Error Creating field base - " . $field_bases[$key]['field_name'], 1);
case PM_FIELD_BASE_CREATE_SUCCESS:
$message = 'Successfully created field base - @field_name';
watchdog('pm', $message, $variables, WATCHDOG_INFO);
break;
case PM_FIELD_BASE_UPDATE_SUCCESS:
$message = 'Successfully Updated field base - @field_name';
watchdog('pm', $message, $variables, WATCHDOG_INFO);
break;
}
}
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);
}
}
foreach ($result as $key => $status) {
$variables = array(
'@field_name' => $field_instances[$key]['field_name'],
);
switch ($status) {
case PM_FIELD_INSTANCE_CREATE_FAILURE:
$message = 'Failed to create field instance - @field_name';
watchdog('pm', $message, $variables, WATCHDOG_ERROR);
throw new Exception("Drupal PM: Error Creating field instance - " . $field_instances[$key]['field_name'] . ' (' . $key . ')', 1);
case PM_FIELD_INSTANCE_CREATE_SUCCESS:
$message = 'Successfully created field instance - @field_name';
watchdog('pm', $message, $variables, WATCHDOG_INFO);
break;
case PM_FIELD_INSTANCE_UPDATE_SUCCESS:
$message = 'Successfully Updated field instance - @field_name';
watchdog('pm', $message, $variables, WATCHDOG_INFO);
break;
}
}
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;
}