You are here

function ddf_selector_element_process in Dynamic dependent fields 7

1 string reference to 'ddf_selector_element_process'
ddf_field_widget_options_select_form_alter in ./ddf.module
Implements hook_field_widget_WIDGET_TYPE_form_alter().

File

./ddf.module, line 376

Code

function ddf_selector_element_process($element, &$form_state, $form) {

  // Do not process hidden fields.
  if (isset($field['#access']) && $field['#access'] == FALSE) {
    return $element;
  }
  if (!empty($form_state['process_input'])) {
    $entity_type = $element['#entity_type'];
    $bundle = $element['#bundle'];
    $entity = $element['#entity'];
    if (is_object($entity)) {
      $entity = clone $entity;
    }
    $properties = $element['#properties'];
    $field_name = $element['#field_name'];
    $dependencies = ddf_load_dependencies($entity_type, $bundle);
    if (empty($dependencies)) {
      return $element;
    }
    $field = field_info_field($field_name);
    $instance = field_info_instance($entity_type, $field_name, $bundle);
    $form_state_copy = $form_state;
    $form_copy = $form;
    foreach ($dependencies as $dependency) {
      if ($dependency[1] == $field_name) {
        $controlling_field_name = $dependency[0];
        $controlling_field = field_info_field($controlling_field_name);
        $columns = $controlling_field['type'] === 'entityreference' ? array(
          'target_id',
        ) : array_keys($controlling_field['columns']);
        $available_languages = field_available_languages($entity_type, $controlling_field);
        $languages = _field_language_suggestion($available_languages, NULL, $controlling_field_name);
        foreach ($languages as $langcode) {
          $path = array_merge($element['#field_parents'], array(
            $controlling_field['field_name'],
            $langcode,
          ));
          $key_exists = NULL;
          $items = drupal_array_get_nested_value($form_state_copy['values'], $path, $key_exists);
          if (!$key_exists) {
            $items = drupal_array_get_nested_value($form_state_copy['input'], $path, $key_exists);
          }
          if ($key_exists) {
            if (!is_array($items)) {
              foreach ($columns as $column) {
                $entity->{$controlling_field_name}[$langcode] = array(
                  0 => array(
                    $column => $items,
                  ),
                );
              }
            }
            elseif ($items !== array()) {
              if (array_key_exists($columns[0], $items)) {
                $entity->{$controlling_field_name}[$langcode] = array(
                  0 => $items,
                );
              }
              else {
                $entity->{$controlling_field_name}[$langcode] = $items;
              }
            }
          }
        }
      }
    }
    $options = _options_get_options($field, $instance, $properties, $entity_type, $entity);
    $element_copy = $element;
    $element_copy['#options'] = $options;
    $default_items = field_get_default_value($entity_type, $entity, $field, $instance);
    $context = array(
      'form' => $form_copy,
      '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_copy, $form_state_copy, $context);
    $element['#options'] = $element_copy['#options'];
  }
  return $element;
}