You are here

function custom_search_form_alter in Custom Search 6

Same name and namespace in other branches
  1. 8 custom_search.module \custom_search_form_alter()
  2. 7 custom_search.module \custom_search_form_alter()

Implementation of hook_form_alter().

File

./custom_search.module, line 94
Bring customizations to the default search box

Code

function custom_search_form_alter(&$form, $form_state, $form_id) {

  // Filter the form_id value to identify all the custom blocks
  $form_id_processed = $form_id;
  $delta = '';
  for ($a = 1; $a <= variable_get('custom_search_blocks_number', 1); $a++) {
    if ($form_id == 'custom_search_blocks_form_' . $a) {
      $form_id_processed = 'custom_search_blocks_form';
      $delta = 'blocks_' . $a . '_';
    }
  }
  switch ($form_id_processed) {
    case 'search_form':
      if (isset($form['advanced'])) {

        // Criteria
        if (!variable_get('custom_search_advanced_or_display', TRUE)) {
          $form['advanced']['keywords']['or']['#type'] = 'hidden';
        }
        if (!variable_get('custom_search_advanced_phrase_display', TRUE)) {
          $form['advanced']['keywords']['phrase']['#type'] = 'hidden';
        }
        if (!variable_get('custom_search_advanced_negative_display', TRUE)) {
          $form['advanced']['keywords']['negative']['#type'] = 'hidden';
        }

        // Taxonomy
        if (module_exists('taxonomy')) {
          $vocabularies = taxonomy_get_vocabularies();
          foreach ($vocabularies as $voc) {
            if (!variable_get('custom_search_advanced_voc' . $voc->vid . '_display', TRUE)) {
              unset($form['advanced']['category']['#options'][$voc->name]);
            }
          }
          if (!count($form['advanced']['category']['#options'])) {
            unset($form['advanced']['category']['#type']);
          }
        }

        // Content Types
        $names = array_keys(node_get_types('names'));
        foreach ($names as $name) {
          if (!variable_get('custom_search_advanced_type_' . $name . '_display', TRUE)) {
            unset($form['advanced']['type']['#options'][$name]);
          }
        }
        if (!count($form['advanced']['type']['#options'])) {
          unset($form['advanced']['type']['#type']);
        }
        if (!variable_get('custom_search_results_search', TRUE)) {

          // If basic search is hidded, original search terms are imported into advanced search.
          $original_keys = $form['basic']['inline']['keys']['#default_value'];
          $temp_keys = explode(' ', $original_keys);
          foreach ($temp_keys as $value) {
            if (drupal_substr($value, 0, 5) != 'type:' && drupal_substr($value, 0, 9) != 'category:') {
              $keys[] = $value;
            }
          }
          $form['advanced']['keywords']['or']['#default_value'] = implode(' ', $keys);
          if (!isset($GLOBALS['custom_search_nb_results']) || isset($GLOBALS['custom_search_nb_results']) && !$GLOBALS['custom_search_nb_results']) {
            $form['advanced']['#collapsed'] = FALSE;
          }
          $form['basic']['#prefix'] = '<div class="element-invisible">';
          $form['basic']['#suffix'] = '</div>';
        }
        $form['advanced']['#collapsible'] = variable_get('custom_search_results_advanced_search_collapsible', TRUE);
        $form['advanced']['#collapsed'] = variable_get('custom_search_results_advanced_search_collapsed', TRUE);
        if (!variable_get('custom_search_results_advanced_search', TRUE)) {
          $form['advanced']['#type'] = 'hidden';
        }
      }
      break;
    case 'search_theme_form':
    case 'search_block_form':
    case 'custom_search_blocks_form':
      if (user_access('use custom search')) {

        // Title.
        $form[$form_id]['#title'] = variable_get('custom_search_' . $delta . 'label', CUSTOM_SEARCH_LABEL_DEFAULT);
        if (!variable_get('custom_search_' . $delta . 'label_visibility', TRUE)) {
          $form[$form_id]['#post_render'] = array(
            '_custom_search_hide_label',
          );
        }

        // Search box.
        $form[$form_id]['#default_value'] = variable_get('custom_search_' . $delta . 'text', '');
        $form[$form_id]['#weight'] = variable_get('custom_search_' . $delta . 'search_box_weight', 0);
        if (!isset($form[$form_id]['#attributes']['class'])) {
          $form[$form_id]['#attributes']['class'] = '';
        }
        $form[$form_id]['#attributes']['class'] .= ' custom-search-default-value custom-search-box';
        $form[$form_id]['#size'] = variable_get('custom_search_' . $delta . 'size', CUSTOM_SEARCH_SIZE_DEFAULT);
        $form[$form_id]['#maxlength'] = variable_get('custom_search_' . $delta . 'max_length', CUSTOM_SEARCH_MAX_LENGTH_DEFAULT);

        // Default text.
        $form['default_text'] = array(
          '#type' => 'hidden',
          '#default_value' => variable_get('custom_search_' . $delta . 'text', ''),
          '#attributes' => array(
            'class' => 'default-text',
          ),
        );

        // CSS
        drupal_add_css(drupal_get_path('module', 'custom_search') . '/custom_search.css', 'module');

        // Criterion
        $criteria = array(
          'or' => 6,
          'phrase' => 7,
          'negative' => 8,
        );
        foreach ($criteria as $c => $w) {
          if (variable_get('custom_search_' . $delta . 'criteria_' . $c . '_display', FALSE)) {
            $form['custom_search_criteria_' . $c] = array(
              '#type' => 'textfield',
              '#title' => variable_get('custom_search_' . $delta . 'criteria_' . $c . '_label', constant('CUSTOM_SEARCH_CRITERIA_' . strtoupper($c) . '_LABEL_DEFAULT')),
              '#size' => 15,
              '#maxlength' => 255,
              '#weight' => variable_get('custom_search_' . $delta . 'criteria_' . $c . '_weight', $w),
            );
          }
        }

        // Content type & other searches.
        // Content types.
        $toptions = array();
        $types = array_keys(array_filter(variable_get('custom_search_' . $delta . 'node_types', array())));
        if (count($types)) {
          $names = node_get_types('names');
          if (count($types) > 1 || variable_get('custom_search_' . $delta . 'any_force', FALSE)) {
            $toptions['c-all'] = variable_get('custom_search_' . $delta . 'type_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT);
          }
          foreach ($types as $type) {
            $toptions['c-' . $type] = $names[$type];
          }
        }
        $options = array();

        // Other searches.
        $others = array_keys(array_filter(variable_get('custom_search_' . $delta . 'other', array())));

        // If content types and other searches are combined, make an optgroup.
        if (count($others) && count($toptions) && variable_get('custom_search_' . $delta . 'type_selector', 'select') == 'select') {
          $content = module_invoke('node', 'search', 'name');
          $options[$content] = $toptions;
        }
        else {
          $options = $toptions;
        }
        foreach (module_implements('search') as $module) {
          if ($module != 'node' && ($name = module_invoke($module, 'search', 'name'))) {
            if (in_array($module, $others)) {
              $options['o-' . $module] = $name;
            }
          }
        }
        if (count($options)) {
          $selector_type = variable_get('custom_search_' . $delta . 'type_selector', 'select');
          if ($selector_type == 'selectmultiple') {
            $selector_type = 'select';
            $multiple = TRUE;
          }
          else {
            $multiple = FALSE;
          }
          $form['custom_search_types'] = array(
            '#type' => $selector_type,
            '#multiple' => $multiple,
            '#title' => variable_get('custom_search_' . $delta . 'type_selector_label', CUSTOM_SEARCH_TYPE_SELECTOR_LABEL_DEFAULT),
            '#options' => $options,
            '#default_value' => variable_get('custom_search_' . $delta . 'type_selector', 'select') == 'checkboxes' ? array(
              'c-all',
            ) : 'c-all',
            '#attributes' => $selector_type != 'radios' && $selector_type != 'checkboxes' ? array(
              'class' => 'custom-search-selector custom-search-types',
            ) : array(),
            '#prefix' => $selector_type == 'radios' || $selector_type == 'checkboxes' ? '<div class="custom-search-selector custom-search-types">' : '',
            '#suffix' => $selector_type == 'radios' || $selector_type == 'checkboxes' ? '</div>' : '',
            '#weight' => variable_get('custom_search_' . $delta . 'content_types_weight', 1),
            '#validated' => TRUE,
          );

          // If there's only one type, hide the selector
          if (count($others) + count($types) == 1 && !variable_get('custom_search_' . $delta . 'any_force', FALSE)) {
            $form['custom_search_types']['#type'] = 'hidden';
            $form['custom_search_types']['#default_value'] = key(array_slice($options, count($options) - 1));
          }
          if (!variable_get('custom_search_' . $delta . 'type_selector_label_visibility', TRUE)) {
            $form['custom_search_types']['#post_render'] = array(
              '_custom_search_hide_label',
            );
          }
        }

        // Custom paths
        if (variable_get('custom_search_' . $delta . 'paths', '') != '') {
          $options = array();
          $lines = explode("\n", variable_get('custom_search_' . $delta . 'paths', ''));
          foreach ($lines as $line) {
            $temp = explode('|', $line);
            $options[$temp[0]] = count($temp) >= 2 ? t($temp[1]) : '';
          }
          if (count($options) == 1) {
            $form['custom_search_paths'] = array(
              '#type' => 'hidden',
              '#default_value' => key($options),
            );
          }
          else {
            $form['custom_search_paths'] = array(
              '#type' => variable_get('custom_search_' . $delta . 'paths_selector', 'select'),
              '#title' => variable_get('custom_search_' . $delta . 'paths_selector_label', CUSTOM_SEARCH_PATHS_SELECTOR_LABEL_DEFAULT),
              '#options' => $options,
              '#default_value' => key($options),
              '#weight' => variable_get('custom_search_' . $delta . 'custom_paths_weight', 9),
            );
            if (!variable_get('custom_search_' . $delta . 'paths_selector_label_visibility', TRUE)) {
              $form['custom_search_paths']['#post_render'] = array(
                '_custom_search_hide_label',
              );
            }
          }
        }

        // Submit button.
        $form['submit']['#value'] = variable_get('custom_search_' . $delta . 'submit_text', CUSTOM_SEARCH_SUBMIT_TEXT_DEFAULT);
        if (variable_get('custom_search_' . $delta . 'image_path', '') != '') {
          $form['submit']['#type'] = 'image_button';
          $form['submit']['#src'] = variable_get('custom_search_' . $delta . 'image_path', '');
          $form['submit']['#name'] = 'op';
          $form['submit']['#attributes'] = array(
            'alt' => variable_get('custom_search_' . $delta . 'submit_text', CUSTOM_SEARCH_SUBMIT_TEXT_DEFAULT),
            'class' => 'custom-search-button',
          );
        }
        elseif ($form['submit']['#value'] == '') {
          $form['submit']['#attributes'] = array(
            'style' => 'display:none;',
          );
        }
        $form['submit']['#weight'] = variable_get('custom_search_' . $delta . 'submit_button_weight', 3);

        // Popup
        $form['popup'] = array(
          '#type' => 'fieldset',
          '#weight' => 1 + variable_get('custom_search_' . $delta . 'search_box_weight', 0),
          '#attributes' => array(
            'class' => 'custom_search-popup',
          ),
        );
        if (!empty($form['custom_search_types']) && variable_get('custom_search_' . $delta . 'content_types_region', 'block') == 'popup') {
          $form['popup']['custom_search_types'] = $form['custom_search_types'];
          unset($form['custom_search_types']);
        }
        if (!empty($form['custom_search_paths']) && variable_get('custom_search_' . $delta . 'custom_paths_region', 'block') == 'popup') {
          $form['popup']['custom_search_paths'] = $form['custom_search_paths'];
          unset($form['custom_search_paths']);
        }
        foreach ($criteria as $c => $w) {
          if (variable_get('custom_search_' . $delta . 'criteria_' . $c . '_display', FALSE) && variable_get('custom_search_' . $delta . 'criteria_' . $c . '_region', 'block') == 'popup') {
            $form['popup']['custom_search_criteria_' . $c] = $form['custom_search_criteria_' . $c];
            unset($form['custom_search_criteria_' . $c]);
          }
        }

        // Form attributes
        $form['#attributes']['class'] = 'search-form';
        $form['#submit'][] = 'custom_search_submit';
        $form['form_token']['#default_value'] = drupal_get_token($form_id);
      }
      break;
  }
}