function context_field_field_insert in Context Field 7
Implements hook_field_insert().
We set the related context name to context_field-ENTITYTYPE-ID if we are using the auto create widget
1 call to context_field_field_insert()
- context_field_field_update in ./
context_field.module - Implements hook_field_update().
File
- ./
context_field.module, line 391 - Context Field
Code
function context_field_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
$entity_info = entity_get_info($entity_type);
$id_field = $entity_info['entity keys']['id'];
$id = $entity->{$id_field};
if (empty($items)) {
$items[0] = array();
}
$default_name = $instance['settings']['default_context'];
switch ($instance['widget']['type']) {
case 'context_field_autocomplete':
foreach ($items as $id => $value) {
$string = $items[$id]['context'];
if (preg_match("/\\[(.*)\\]/", $string, $matches)) {
$items[$id]['context'] = $matches[1];
}
elseif (!empty($string) && $instance['widget']['settings']['form_element']['allow_create']) {
$items[$id]['context'] = preg_replace("/[^a-z0-9_]+/", "_", strtolower($string));
$context = context_load($items[$id]['context']);
if (is_array($context) || empty($context)) {
$cat = $instance['widget']['settings']['form_element']['category'];
context_field_clone_default($default_name, $items[$id]['context'], $string, $cat);
}
}
}
break;
case 'context_field':
// Set the context name to a entity spacific name
$custom_name = "context_field-{$entity_type}-{$id}";
// Lets see if we already have a context
$context = context_load($custom_name);
// If user toggle is enabled then we need to create or delete the
// context bases on the setting of the context
if ($instance['widget']['settings']['form_element']['user_toggle']) {
if ($items[0]['context']) {
// set the context to our custom name
$items[0]['context'] = $custom_name;
if (!$context) {
context_field_clone_default($default_name, $custom_name);
}
}
else {
// Set context to false as we do not have one
$items[0]['context'] = FALSE;
// If there is a context delete it
if ($context) {
context_delete($context);
}
}
}
else {
$items[0]['context'] = $custom_name;
if (!context_load($custom_name)) {
context_field_clone_default($default_name, $custom_name);
}
}
break;
}
}