You are here

function finder_optionwidgets_finder_element in Finder 6

Same name and namespace in other branches
  1. 7 modules/finder_optionwidgets/finder_optionwidgets.module \finder_optionwidgets_finder_element()

Implementation of hook_finder_element().

See also

hook_finder_element()

File

modules/finder_optionwidgets/finder_optionwidgets.module, line 147
The finder optionwidgets module.

Code

function finder_optionwidgets_finder_element($element, &$form_element) {
  $finder = finder_load($element->finder_id);
  $fields =& $element->settings['choices']['field'];
  foreach ($fields as $key => $field) {
    $field_info[$key] = finder_split_field($field);
    $field_names[$key] = $field_info[$key]['field'];
  }
  if ($element->element == 'select') {
    $form_element['#type'] = 'select';
    $properties = array(
      'multiple',
      'size',
    );
    if ($element->settings['form']['multiple']) {
      if (!is_array($form_element['#default_value'])) {
        $form_element['#default_value'] = explode("\n", $form_element['#default_value']);
      }
    }
  }
  elseif ($element->element == 'buttons') {
    if ($element->settings['form']['multiple']) {
      $form_element['#type'] = 'checkboxes';
      if (!is_array($form_element['#default_value'])) {
        $form_element['#default_value'] = explode("\n", $form_element['#default_value']);
      }
    }
    else {
      $form_element['#type'] = 'radios';
    }
  }
  elseif ($element->element == 'onoff') {
    $form_element['#type'] = 'checkbox';
    $properties = array(
      'return_value',
    );
  }
  foreach ((array) $properties as $property) {
    if ($element->settings['form'][$property]) {
      $form_element['#' . $property] = $element->settings['form'][$property];
    }
  }
  $options = finder_find($finder, array(
    $element->finder_element_id => NULL,
  ), 'choices');
  $form_element['#options'] = array();
  foreach ((array) $options as $key => $option) {
    $display = theme('finder_optionwidgets_' . $element->element . '_option', $option, $element);
    $value = theme('finder_optionwidgets_' . $element->element . '_value', $option, $element);
    if ($display && $value) {
      if (!empty($element->settings['choices']['rewrite']['php'])) {
        $variables = array(
          'finder_element' => $element,
          'value' => $display,
          'key' => $value,
          'option' => $option,
        );
        $display = finder_eval($element->settings['choices']['rewrite']['php'], $variables);
      }
      $form_element['#options'][$value] = $display;
    }
  }
  if (!empty($element->settings['choices']['sort'])) {
    natcasesort($form_element['#options']);
  }
  if ($element->settings['form']['empty']) {
    $empty = array(
      '' => $element->settings['form']['empty_text'],
    );
    if ($element->settings['form']['empty'] == 'prepend') {
      $form_element['#options'] = $empty + $form_element['#options'];
    }
    elseif ($element->settings['form']['empty'] == 'append') {
      $form_element['#options'] = $form_element['#options'] + $empty;
    }
  }
}