Skip to content
Snippets Groups Projects
UwSearchForm.php 4.4 KiB
Newer Older
<?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>'),
        'uw-search--checkboxlabel',
      ],
      '#attributes' => [
        'class' => [
          'uw-input',
          'uw-input--checkboxform',
        ],
      '#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' => $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' => [
        '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));
  }

}