Skip to content

Instantly share code, notes, and snippets.

@lukerhd
Created July 25, 2018 21:57
Show Gist options
  • Save lukerhd/818a91763962b6b0893efc90bef52f49 to your computer and use it in GitHub Desktop.
Save lukerhd/818a91763962b6b0893efc90bef52f49 to your computer and use it in GitHub Desktop.
function scolaris_mhe_search_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form['#id'] == 'views-exposed-form-search-page-1') {
$current_route = \Drupal::routeMatch()->getRouteName();
$options = FALSE;
// Check if we're on a node page.
if ($current_route == 'entity.node.canonical') {
$node = \Drupal::routeMatch()->getParameter('node');
// We only want additional options for the book, chapter
// and section node types.
$allowed_types = ['item_book', 'item_chapter', 'item_section'];
// Setup our options for allowed types.
if (in_array($node->getType(), $allowed_types)) {
dsm('node');
ksm($node->getTitle(), 'node');
$facet_query = ['content_source:' . $node->getTitle()];
$options = [
'within' => t('Within this book'),
'all' => t('All of AccessEngineering'),
];
}
}
// Check if we're on the search page.
elseif ($current_route == 'view.search.page_1') {
// Retain any facet query parameters are provided.
$facet_query = \Drupal::request()->query->filter('f');
// Setup our options for the search page.
$options = [
'within' => t('Within your selection'),
'all' => t('All of AccessEngineering'),
];
}
// If we have options, add the select list with
// said options.
if ($options) {
$form['search_context'] = [
'#type' => 'select',
'#title' => t('Search Context'),
'#options' => $options,
'#weight' => -10,
];
if (!empty($facet_query)) {
#dsm('facet query found');
foreach ($facet_query as $i => $facet_query_param) {
#ksm($facet_query_param, 'facet_query_param');
$form['f[' . $i . ']'] = [
'#type' => 'hidden',
'#value' => $facet_query_param,
'#weight' => 0,
];
}
}
}
$form['#validate'][] = 'scolaris_mhe_search_form_validate';
$form['#submit'][] = 'scolaris_mhe_search_form_submit';
#ksm($form, 'form from my custom code');
}
}
function scolaris_mhe_search_form_validate($form, FormStateInterface &$form_state) {
$search_context = $form_state->getValue('search_context');
#dsm('validate! form and formstate');
#ksm($form, $form_state);
if ($search_context == 'all') {
#dsm('search context == all, dont set any query facet params!');
}
}
function scolaris_mhe_search_form_submit($form, FormStateInterface &$form_state) {
$search_context = $form_state->getValue('search_context');
dsm('submit! form and formstate');
ksm($form, $form_state);
if ($search_context == 'all') {
dsm('sssssearch context == all, dont set any query facet params!');
ksm($form_state->getValues());
ksm($form_state->getUserInput());
$user_input = $form_state->getUserInput();
$value = ['search_context'];
$form_state->setValue(['search_context'], 'content_source:Foobar');
$form_state->setRebuild();
ksm($form_state->getValues());
$form['f[0]'] = [
'#type' => 'hidden',
'#value' => '',
'#weight' => 0,
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment