You are here

function custom_search_i18n_form_alter in Custom Search 6

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

Implementation of hook_form_alter().

File

modules/custom_search_i18n/custom_search_i18n.module, line 12
Brings Internationalization to Custom Search

Code

function custom_search_i18n_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_theme_form':
    case 'search_block_form':
    case 'custom_search_blocks_form':
      if (user_access('use custom search')) {

        // Criteria
        $criteria = array(
          'or',
          'phrase',
          'negative',
        );
        foreach ($criteria as $c) {
          if (variable_get('custom_search_' . $delta . 'criteria_' . $c . '_display', FALSE)) {
            if (variable_get('custom_search_' . $delta . 'criteria_' . $c . '_region', 'block') == 'popup') {
              $form['popup']['custom_search_criteria_' . $c]['#title'] = i18nstrings('custom_search:criterion:1:' . $delta . $c . '_label', variable_get('custom_search_' . $delta . 'criteria_' . $c . '_label', constant('CUSTOM_SEARCH_CRITERIA_' . strtoupper($c) . '_LABEL_DEFAULT')));
            }
            else {
              $form['custom_search_criteria_' . $c]['#title'] = i18nstrings('custom_search:criterion:1:' . $delta . $c . '_label', variable_get('custom_search_' . $delta . 'criteria_' . $c . '_label', constant('CUSTOM_SEARCH_CRITERIA_' . strtoupper($c) . '_LABEL_DEFAULT')));
            }
          }
        }

        // Title
        $form[$form_id]['#title'] = i18nstrings('custom_search:common:1:' . $delta . 'label', variable_get('custom_search_' . $delta . 'label', CUSTOM_SEARCH_LABEL_DEFAULT));

        // Default text
        $form[$form_id]['#default_value'] = i18nstrings('custom_search:common:1:' . $delta . 'text', variable_get('custom_search_' . $delta . 'text', ''));
        $form['default_text']['#default_value'] = i18nstrings('custom_search:common:1:' . $delta . 'text', variable_get('custom_search_' . $delta . 'text', ''));

        // Submit button
        $form['submit']['#value'] = i18nstrings('custom_search:common:1:' . $delta . 'submit_text', variable_get('custom_search_' . $delta . 'submit_text', CUSTOM_SEARCH_SUBMIT_TEXT_DEFAULT));
        if ($form['submit']['#value'] == '') {
          $form['submit']['#attributes'] = array(
            'style' => 'display:none;',
          );
        }

        // Type selector
        if (!empty($form['custom_search_types']) && is_array($form['custom_search_types']) || !empty($form['popup']['custom_search_types']) && is_array($form['popup']['custom_search_types'])) {
          if (variable_get('custom_search_' . $delta . 'content_types_region', 'block') == 'popup') {
            $form['popup']['custom_search_types']['#title'] = i18nstrings('custom_search:content_types:1:' . $delta . 'selector_label', variable_get('custom_search_' . $delta . 'type_selector_label', CUSTOM_SEARCH_TYPE_SELECTOR_LABEL_DEFAULT));
            if (array_key_exists('c-all', $form['popup']['custom_search_types']['#options'])) {
              $form['popup']['custom_search_types']['#options']['c-all'] = i18nstrings('custom_search:content_types:1:' . $delta . 'selector_all', variable_get('custom_search_' . $delta . 'type_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT));
            }
          }
          else {
            $form['custom_search_types']['#title'] = i18nstrings('custom_search:content_types:1:' . $delta . 'selector_label', variable_get('custom_search_' . $delta . 'type_selector_label', CUSTOM_SEARCH_TYPE_SELECTOR_LABEL_DEFAULT));
            if (array_key_exists('c-all', $form['custom_search_types']['#options'])) {
              $form['custom_search_types']['#options']['c-all'] = i18nstrings('custom_search:content_types:1:' . $delta . 'selector_all', variable_get('custom_search_' . $delta . 'type_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT));
            }
          }
        }

        // Taxonomy
        if (module_exists('custom_search_taxonomy')) {
          $vocabularies = taxonomy_get_vocabularies();

          // Translate label and -all-
          foreach ($vocabularies as $voc) {
            if (!empty($form['custom_search_vocabulary_' . $voc->vid]) && is_array($form['custom_search_vocabulary_' . $voc->vid]) || !empty($form['popup']['custom_search_vocabulary_' . $voc->vid]) && is_array($form['popup']['custom_search_vocabulary_' . $voc->vid])) {
              if (variable_get('custom_search_' . $delta . 'taxonomy' . $voc->vid . '_region', 'block') == 'popup') {
                $form['popup']['custom_search_vocabulary_' . $voc->vid]['#title'] = i18nstrings('custom_search:vocabulary:' . $voc->vid . ':' . $delta . 'selector_label', variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_label', $voc->name));
                if (is_array($form['popup']['custom_search_vocabulary_' . $voc->vid]['#options']) && array_key_exists('c-all', $form['popup']['custom_search_vocabulary_' . $voc->vid]['#options'])) {
                  $form['popup']['custom_search_vocabulary_' . $voc->vid]['#options']['c-all'] = i18nstrings('custom_search:vocabulary:' . $voc->vid . ':' . $delta . 'selector_all', variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT));
                }
              }
              else {
                $form['custom_search_vocabulary_' . $voc->vid]['#title'] = i18nstrings('custom_search:vocabulary:' . $voc->vid . ':' . $delta . 'selector_label', variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_label', $voc->name));
                if (is_array($form['custom_search_vocabulary_' . $voc->vid]['#options']) && array_key_exists('c-all', $form['custom_search_vocabulary_' . $voc->vid]['#options'])) {
                  $form['custom_search_vocabulary_' . $voc->vid]['#options']['c-all'] = i18nstrings('custom_search:vocabulary:' . $voc->vid . ':' . $delta . 'selector_all', variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT));
                }
              }
            }
          }

          // Translate terms
          if (module_exists('i18ntaxonomy')) {
            foreach ($vocabularies as $voc) {
              if (!empty($form['custom_search_vocabulary_' . $voc->vid]) && is_array($form['custom_search_vocabulary_' . $voc->vid]['#options']) || !empty($form['popup']['custom_search_vocabulary_' . $voc->vid]) && is_array($form['popup']['custom_search_vocabulary_' . $voc->vid]['#options'])) {
                if (variable_get('custom_search_' . $delta . 'taxonomy' . $voc->vid . '_region', 'block') == 'popup') {
                  foreach ($form['popup']['custom_search_vocabulary_' . $voc->vid]['#options'] as $tid => $name) {
                    if ($tid != 'c-all') {
                      $real_tid = substr($tid, 2);
                      $translated_term = i18nstrings('taxonomy:term:' . $real_tid . ':name', $name);
                      $form['popup']['custom_search_vocabulary_' . $voc->vid]['#options'][$tid] = $translated_term[0] != '-' ? str_repeat('-', _custom_search_i18n_term_depth($real_tid)) . ' ' . $translated_term : $translated_term;
                    }
                  }
                }
                else {
                  foreach ($form['custom_search_vocabulary_' . $voc->vid]['#options'] as $tid => $name) {
                    if ($tid != 'c-all') {
                      $real_tid = substr($tid, 2);
                      $translated_term = i18nstrings('taxonomy:term:' . $real_tid . ':name', $name);
                      $form['custom_search_vocabulary_' . $voc->vid]['#options'][$tid] = $translated_term[0] != '-' ? str_repeat('-', _custom_search_i18n_term_depth($real_tid)) . ' ' . $translated_term : $translated_term;
                    }
                  }
                }
              }
            }
          }
        }

        // Custom Paths
        if (!empty($form['custom_search_paths']) && is_array($form['custom_search_paths']) || !empty($form['popup']['custom_search_paths']) && is_array($form['popup']['custom_search_paths'])) {
          if (variable_get('custom_search_' . $delta . 'custom_paths_region', 'block') == 'popup') {
            $form['popup']['custom_search_paths']['#title'] = i18nstrings('custom_search:paths:1:' . $delta . 'selector_label', variable_get('custom_search_' . $delta . 'paths_selector_label', CUSTOM_SEARCH_PATHS_SELECTOR_LABEL_DEFAULT));
          }
          else {
            $form['custom_search_paths']['#title'] = i18nstrings('custom_search:paths:1:' . $delta . 'selector_label', variable_get('custom_search_' . $delta . 'paths_selector_label', CUSTOM_SEARCH_PATHS_SELECTOR_LABEL_DEFAULT));
          }
        }
      }
      break;
    case 'custom_search_admin':
      $form['#submit'][] = 'custom_search_i18n_admin_submit';
      break;
    case 'custom_search_content_admin':
      $form['#submit'][] = 'custom_search_i18n_content_admin_submit';
      break;
    case 'custom_search_taxonomy_admin':
      $form['#submit'][] = 'custom_search_i18n_taxonomy_admin_submit';
      break;
    case 'custom_search_results_admin':
      $form['#submit'][] = 'custom_search_i18n_results_admin_submit';
      break;
    case 'block_admin_configure':
      $form['#submit'][] = 'custom_search_i18n_block_admin_configure_submit';
      break;
  }
}