You are here

function oa_core_oa_section_ref_process in Open Atrium Core 7.2

Process function of the oa_section_ref field.

1 string reference to 'oa_core_oa_section_ref_process'
oa_core_element_info in includes/oa_core.fields.inc
Implements hook_element_info().

File

includes/oa_core.fields.inc, line 155
Code for custom Form API fields in Open Atrium

Code

function oa_core_oa_section_ref_process($element, &$form_state, &$complete_form) {
  global $user;
  $element['#type'] = 'select';
  $element += array(
    '#title' => t('Section'),
    '#description' => t('Select a Section'),
  );
  $element['#options'][-1] = t(' - Current Section - ');
  if (empty($element['#required'])) {
    $element['#options'][0] = t(' - Any - ');
  }
  if (!isset($element['#default_value'])) {
    $element['#default_value'] = !empty($element['#multiple']) ? array(
      -1,
    ) : -1;
  }
  $element += element_info('select');
  $group_ref_key = NULL;
  if (isset($element['#og_group_ref'])) {
    if (!empty($element['#og_group_ref'])) {
      $group_ref = !is_array($element['#og_group_ref']) ? array(
        $element['#og_group_ref'],
      ) : $element['#og_group_ref'];
      $group_ref_key = implode('--', $group_ref);
      $value = drupal_array_get_nested_value($form_state['input'], $group_ref);
      if (is_null($value)) {
        $value = drupal_array_get_nested_value($form_state['values'], $group_ref);
      }
    }
    else {
      $value = -1;
    }

    // Gid == 0 is no group.
    $gid = isset($value) && ($value === 0 || $value != -1) ? $value : oa_core_get_space_context();

    // Add options based on current selected group.
    if ($gid && ($sections = oa_core_space_sections($gid, NODE_PUBLISHED, NULL, array(), TRUE))) {
      foreach ($sections as $nid => $title) {
        $element['#options'][$nid] = check_plain($title);
      }
    }

    // Since IDs change with ajax (to be unique), need a static id for wrappers.
    $element['#ajax_id'] = isset($form_state['oa_section_ref_replace'][$group_ref_key]['id']) ? $form_state['oa_section_ref_replace'][$group_ref_key]['id'] : $element['#id'];
    $element['#prefix'] = '<div id="' . $element['#ajax_id'] . '-replace">';
    $element['#suffix'] = '</div>';
    if (!empty($group_ref_key)) {

      // Add wrapper to replace via ajax.
      // Find the group element and add ajax processing to it.
      $group_element =& _oa_core_oa_section_ref_process_find_element($complete_form, $group_ref);
      $group_element['#ajax'] = array(
        'callback' => 'oa_core_oa_section_ref_replace',
        'wrapper' => $element['#ajax_id'] . '-replace',
        'method' => 'replace',
        'effect' => 'fade',
        'event' => 'change',
      );
    }
    if (arg(0) == 'panels' && strpos($_GET['q'], 'ajax')) {
      $group_element['#ajax']['path'] = 'system/panopoly-magic';
    }

    // Add a message area to see messages.
    $group_element['messages'] = array(
      '#tag' => 'div',
      '#value' => '',
      '#attributes' => array(
        'id' => $element['#ajax_id'] . '-messages',
      ),
      '#type' => 'html_tag',
      '#weight' => -100,
    );

    // Store information about this replacement for the ajax callback.
    if (!isset($form_state['oa_section_ref_replace'][$group_ref_key])) {
      $form_state['oa_section_ref_replace'][$group_ref_key]['parents'] = $element['#parents'];
      $form_state['oa_section_ref_replace'][$group_ref_key]['id'] = $element['#ajax_id'];
    }

    // Because we are altering an already processed form element, need to redo.
    unset($group_element['#ajax_processed']);
    $group_element = ajax_process_form($group_element, $form_state);
    if (!empty($form_state['triggering_element']) && $form_state['triggering_element']['#parents'] == $group_ref) {
      $form_state['triggering_element'] = $group_element;
    }
  }

  // Make sure if ajax with change group, invalid sections are forgotten.
  $values = is_array($element['#value']) ? $element['#value'] : array(
    $element['#value'],
  );
  foreach ($values as $key => $value) {
    if (empty($element['#options'][$value])) {
      unset($values[$key]);
    }
  }
  if (empty($values)) {
    $element['#value'] = !empty($element['#multiple']) ? array(
      -1,
    ) : -1;
  }
  else {
    $element['#value'] = !empty($element['#multiple']) ? $values : reset($values);
  }

  // Allow standard select processing (allows use of #multiple)
  $element = form_process_select($element);
  return $element;
}