function ddf_entityreference_ddf_update_widget in Dynamic dependent fields 7
Implements hook_ddf_update_widget().
File
- modules/
ddf_entityreference/ ddf_entityreference.module, line 16
Code
function ddf_entityreference_ddf_update_widget($dependent_field, $parameters, $selector, $entity, $settings, $controlling_field_name, $entity_type, $bundle) {
if ($dependent_field['type'] != 'entityreference') {
return NULL;
}
EntityReference_SelectionHandler_DDF::storeControllingFieldValues($parameters, $entity_type, $entity);
$field = field_info_field($dependent_field['field_name']);
$instance = field_info_instance($entity_type, $dependent_field['field_name'], $bundle);
if ($instance['widget']['type'] != 'options_select') {
return NULL;
}
$form = array();
$form_state = array();
$element = array(
'#entity_type' => $entity_type,
'#entity' => $entity,
'#required' => $instance['required'],
);
$element = options_field_widget_form($form, $form_state, $dependent_field, $instance, LANGUAGE_NONE, array(), 0, $element);
$default_items = field_get_default_value($entity_type, $entity, $field, $instance, LANGUAGE_NONE);
$context = array(
'form' => $form,
'field' => $field,
'instance' => $instance,
'langcode' => LANGUAGE_NONE,
'items' => $default_items,
'delta' => 0,
);
drupal_alter(array(
'field_widget_form',
'field_widget_' . $instance['widget']['type'] . '_form',
), $element, $form_state, $context);
$result = '';
if (isset($element['#options'])) {
if (!empty($element['#properties'])) {
$position = NULL;
if (isset($element['#properties']['empty_option']) && $element['#properties']['empty_option'] == 'option_select') {
if (count($element['#options']) == 2) {
$position = 1;
}
}
elseif (!isset($element['#properties']['empty_option'])) {
if (count($element['#options']) == 1) {
$position = 0;
}
}
if (!is_null($position)) {
$keys = array_keys($element['#options']);
if (isset($keys[$position])) {
$element['#value'] = $keys[$position];
}
}
}
$result = form_select_options($element);
}
$commands = array();
$command = ajax_command_html($selector, $result);
$command['command'] = 'ddf_insertnowrap';
$commands[] = $command;
// If chosen is applied, it can't be updated by attachBehavior().
$commands[] = ajax_command_invoke($selector, 'trigger', array(
'liszt:updated',
));
$commands[] = ajax_command_invoke($selector, 'trigger', array(
'chosen:updated',
));
// Options are changed, so run 'change' handlers.
$commands[] = ajax_command_invoke($selector, 'trigger', array(
'change',
));
return $commands;
}