function custom_search_form_alter in Custom Search 7
Same name and namespace in other branches
- 8 custom_search.module \custom_search_form_alter()
- 6 custom_search.module \custom_search_form_alter()
Implements hook_form_alter().
File
- ./
custom_search.module, line 92 - 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['module']) && $form['module']['#value'] == 'node') {
if (isset($form['advanced']) && variable_get('custom_search_results_advanced_search', TRUE)) {
// Reprocess the key value for later uses.
$search_args = array(
'keys' => array(),
'criteria' => array(
'phrase' => '',
'negative' => array(),
),
'types' => array(),
'languages' => array(),
);
$phrase_pos = strpos($form['basic']['keys']['#default_value'], '"');
if ($phrase_pos !== FALSE) {
$phrase_pos_end = strpos($form['basic']['keys']['#default_value'], '"', $phrase_pos + 1);
$search_args['criteria']['phrase'] = substr($form['basic']['keys']['#default_value'], $phrase_pos + 1, $phrase_pos_end - $phrase_pos - 1);
$form['basic']['keys']['#default_value'] = str_replace('"' . $search_args['criteria']['phrase'] . '"', '', $form['basic']['keys']['#default_value']);
}
$search_key_array = explode(' ', $form['basic']['keys']['#default_value']);
foreach ($search_key_array as $key) {
$arg = explode(':', $key);
if (count($arg) == 2) {
switch ($arg[0]) {
case 'type':
$search_args['types'] = explode(',', $arg[1]);
break;
case 'language':
$search_args['languages'] = explode(',', $arg[1]);
break;
default:
$search_args['keys'][] = $arg[1];
}
}
else {
if ($arg[0] != '') {
if ($arg[0][0] == '-') {
$search_args['criteria']['negative'][] = substr($arg[0], 1);
}
else {
$search_args['keys'][] = $arg[0];
}
}
}
}
// 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';
}
$form['advanced']['keywords']['phrase']['#default_value'] = $search_args['criteria']['phrase'];
$form['advanced']['keywords']['negative']['#default_value'] = implode(' ', $search_args['criteria']['negative']);
// Content types.
$names = array_keys(node_type_get_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']);
}
else {
$form['advanced']['type']['#default_value'] = $search_args['types'];
}
// Languages.
foreach (language_list('language') as $key => $entity) {
if ($entity->enabled) {
$language_options[$key] = $entity->name;
}
}
// Add Language neutral to the list.
$form['advanced']['language']['#options']['und'] = t('Language neutral');
$language_options['und'] = t('Language neutral');
foreach ($language_options as $key => $name) {
if (!variable_get('custom_search_advanced_language_' . $key . '_display', TRUE)) {
unset($form['advanced']['language']['#options'][$key]);
}
}
if (!count($form['advanced']['language']['#options'])) {
unset($form['advanced']['language']['#type']);
}
else {
$form['advanced']['language']['#default_value'] = $search_args['languages'];
}
// Remove unnecessary args from keys.
$form['basic']['keys']['#default_value'] = implode(' ', $search_args['keys']);
}
if (!variable_get('custom_search_results_search', TRUE)) {
if (isset($form['basic']['keys'])) {
// If basic search is hidden, import terms into advanced search.
$original_keys = $form['basic']['keys']['#default_value'];
$temp_keys = explode(' ', $original_keys);
foreach ($temp_keys as $value) {
if (drupal_substr($value, 0, 5) != 'type:' && drupal_substr($value, 0, 5) != 'term:') {
$keys[] = $value;
}
}
$form['advanced']['keywords']['or']['#default_value'] = implode(' ', $search_args['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);
$form[$form_id]['#title_display'] = !variable_get('custom_search_' . $delta . 'label_visibility', FALSE) ? 'invisible' : 'before';
// Search box.
if (module_exists('elements') && variable_get('custom_search_' . $delta . 'element', 'textfield') != 'textfield') {
$form[$form_id]['#type'] = variable_get('custom_search_' . $delta . 'element', 'textfield');
}
$form[$form_id]['#weight'] = variable_get('custom_search_' . $delta . 'search_box_weight', 0);
$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);
if (!isset($form[$form_id]['#attributes'])) {
$form[$form_id]['#attributes'] = array(
'class' => array(),
);
}
elseif (!isset($form[$form_id]['#attributes']['class'])) {
$form[$form_id]['#attributes']['class'] = array();
}
$form[$form_id]['#attributes']['class'][] = 'custom-search-box';
$form[$form_id]['#attributes']['placeholder'] = variable_get('custom_search_' . $delta . 'text', '');
$form[$form_id]['#attributes']['title'] = variable_get('custom_search_' . $delta . 'hint_text', CUSTOM_SEARCH_HINT_TEXT_DEFAULT);
// CSS.
drupal_add_css(drupal_get_path('module', 'custom_search') . '/custom_search.css');
// Criteria.
$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_type_get_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] = t($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_info');
$options[$content['title']] = $toptions;
}
else {
$options = $toptions;
}
foreach (module_implements('search_info') as $module) {
if ($module != 'node' && ($name = module_invoke($module, 'search_info'))) {
if (in_array($module, $others)) {
$options['o-' . $module] = t($name['title']);
}
}
}
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' => array(
'class' => array(
'custom-search-selector',
'custom-search-types',
),
),
'#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']['#title_display'] = 'invisible';
}
}
// 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']['#title_display'] = 'invisible';
}
}
}
// Submit button.
$form['actions']['submit']['#value'] = variable_get('custom_search_' . $delta . 'submit_text', CUSTOM_SEARCH_SUBMIT_TEXT_DEFAULT);
if (variable_get('custom_search_' . $delta . 'image_path', '') != '') {
$form['actions']['submit']['#type'] = 'image_button';
$form['actions']['submit']['#src'] = variable_get('custom_search_' . $delta . 'image_path', '');
$form['actions']['submit']['#name'] = 'op';
$form['actions']['submit']['#attributes'] = array(
'alt' => array(
variable_get('custom_search_' . $delta . 'submit_text', CUSTOM_SEARCH_SUBMIT_TEXT_DEFAULT),
),
'class' => array(
'custom-search-button',
),
);
unset($form['actions']['submit']['#value']);
}
elseif ($form['actions']['submit']['#value'] == '') {
$form['actions']['submit']['#attributes'] = array(
'style' => 'display:none;',
);
}
$form['actions']['#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' => array(
'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]);
}
}
// Invoke other modules hooks.
foreach (module_implements('custom_search_box') as $module) {
$function = $module . '_custom_search_box';
if (function_exists($function)) {
call_user_func_array($function, array(
&$form,
$form_id,
$delta,
));
}
}
// If nothing has been added to the popup, don't output any markup.
if (!count(element_children($form['popup']))) {
unset($form['popup']);
}
// Form attributes.
$form['#attributes']['class'] = array(
'search-form',
);
$form['#attributes']['role'] = 'search';
if (!in_array('custom_search_submit', $form['#submit'])) {
$form['#submit'][] = 'custom_search_submit';
}
}
break;
}
}