Skip to content
Snippets Groups Projects
Commit 9303de8b authored by Kevin Paxman's avatar Kevin Paxman
Browse files

Merge branch 'feature/ISTWCMS-4729-ebremner-search-form' into '1.0.x'

ISTWCMS 4729 ebremner search form

See merge request !239
parents 637d0655 e4848fe2
No related branches found
No related tags found
1 merge request!239ISTWCMS 4729 ebremner search form
<?php
namespace Drupal\uw_cfg_common\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Routing\TrustedRedirectResponse;
/**
* Form class for the search form.
*/
class UwSearchForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'uw_search_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Set the attributes and role for the form.
$form['#attributes'] = [
'class' => ['uw-search--form'],
'role' => 'search',
];
// Element used to open the tray for phone view.
$form['opentray'] = [
'#type' => 'checkbox',
'#title' => $this->t('<span class="uw-search--checkboxlabel--labeltext">Search Checkbox</span>'),
'#label_classes' => [
'uw-search--checkboxlabel',
],
'#attributes' => [
'class' => [
'uw-input',
'uw-input--checkboxform',
],
'aria-hidden' => 'true',
'tabindex' => '-1',
],
'#theme_wrappers' => [],
];
// This is the label that is used for the "phone" view on
// the site search. We need to use form element label here
// by itself, since we use theme_wrappers as an empty array
// above in the input, which would normally give us the form
// label.
$form['label'] = [
'#theme' => 'form_element_label',
'#title' => $this->t('<span class="uw-search--checkboxlabel__labeltext">Open Search Location </span>'),
'#title_display' => 'after',
'#required' => FALSE,
'#id' => 'edit-opentray',
'#attributes' => [
'class' => ['uw-search--checkboxlabel'],
],
];
// The search text.
$form['search-input'] = [
'#type' => 'textfield',
'#attributes' => [
'class' => ['uw-input', 'uw-input--search'],
],
'#id' => 'uw-search',
'#placeholder' => $this->t('Search'),
'#title' => $this->t('<span class="uw-search--labeltext">Search for </span>'),
'#title_display' => 'invisible',
'#label' => [
'#theme' => 'form_element_label',
'#required' => FALSE,
'#id' => 'uw-search',
'#attributes' => [
'class' => ['uw-search--hidelabel'],
],
],
];
// Get the URL for this site to be used in the options.
$url = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString();
// The type of search, either all sites or this site.
$form['search-type'] = [
'#type' => 'select',
'#id' => 'uw-select-site',
'#attributes' => [
'class' => ['form-item__select', 'uw-select--search'],
],
'#options' => [
'' => $this->t('On all sites'),
'inurl:' . $url => $this->t('On this site'),
],
'#title_display' => 'invisible',
'#title' => $this->t('Search Location'),
'#label' => [
'#theme' => 'form_element_label',
'#required' => FALSE,
'#id' => 'uw-select-site',
'#attributes' => [
'class' => ['uw-search--hidelabel'],
],
],
];
// The submit button.
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#attributes' => [
'value' => $this->t('Search'),
'class' => [
'button',
'button--submit',
'button--submit__form',
],
],
'#prefix' => '<div class="uw-search-button__wrapper">',
'#suffix' => '</div>',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Variable for the parameters.
$parameters = '';
// Get the values from the form state.
$values = $form_state->getValues();
// If this is a site search, add it to parameters.
if ($values['search-type']) {
$parameters .= $values['search-type'] . ' ';
}
// If there is search text add it to the parameters.
if ($values['search-input']) {
$parameters .= $values['search-input'];
}
// The URL to the uwaterloo search.
$url = 'https://uwaterloo.ca/search';
// If there are parameters, encode and add to URL.
if ($parameters !== '') {
$url .= '/?search-input=' . urlencode($parameters);
}
// Redirect to the uwaterloo search.
$form_state->setResponse(new TrustedRedirectResponse($url, 302));
}
}
...@@ -22,6 +22,18 @@ use Drupal\uw_cfg_common\Service\UWService; ...@@ -22,6 +22,18 @@ use Drupal\uw_cfg_common\Service\UWService;
use Drupal\webform\WebformInterface; use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionStorageInterface; use Drupal\webform\WebformSubmissionStorageInterface;
/**
* Implements hook_preprocess_form_element().
*
* Allows for use of label_class in form elements and will add
* any class in label_classes to the label.
*/
function uw_cfg_common_preprocess_form_element(&$variables) {
if (isset($variables['element']['#label_classes'])) {
$variables['label']['#attributes']['class'] = $variables['element']['#label_classes'];
}
}
/** /**
* Implements hook_sendgrid_integration_categories_alter(). * Implements hook_sendgrid_integration_categories_alter().
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment