You are here

class AdministrationForm in Google Search Appliance 8

Class AdministrationForm.

@package Drupal\google_appliance\Form

Hierarchy

Expanded class hierarchy of AdministrationForm

1 string reference to 'AdministrationForm'
google_appliance.routing.yml in ./google_appliance.routing.yml
google_appliance.routing.yml

File

src/Form/AdministrationForm.php, line 21

Namespace

Drupal\google_appliance\Form
View source
class AdministrationForm extends ConfigFormBase {

  /**
   * Route builder.
   *
   * @var \Drupal\Core\Routing\RouteBuilderInterface
   */
  protected $routeBuilder;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('router.builder'));
  }

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactoryInterface $configFactory, RouteBuilderInterface $routeBuilder) {
    parent::__construct($configFactory);
    $this->routeBuilder = $routeBuilder;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'google_appliance_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return 'google_appliance.settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $settings = $this
      ->configFactory()
      ->get($this
      ->getEditableConfigNames())
      ->get();
    $settings['display_settings']['onebox_modules'] = implode("\n", $settings['display_settings']['onebox_modules']);
    $form['connection_info'] = [
      '#title' => $this
        ->t('Connection Information'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    ];
    $form['connection_info']['hostname'] = [
      '#type' => 'url',
      '#title' => $this
        ->t('Google Search Appliance Host Name'),
      '#description' => $this
        ->t('Valid URL or IP address of the GSA device, including <em>http://</em> or <em>https://</em>. Do <b>not</b> include <em>/search</em> at the end, or a trailing slash, but you should include a port number if needed. Example: <em>http://my.gsabox.com:8443</em>'),
      '#default_value' => $settings['connection_info']['hostname'],
      '#required' => TRUE,
    ];
    $form['connection_info']['collection'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Collection'),
      '#description' => $this
        ->t('The name of a valid collection on the GSA device (case sensitive).'),
      '#default_value' => $settings['connection_info']['collection'],
      '#required' => TRUE,
    ];
    $form['connection_info']['frontend'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Frontend client'),
      '#description' => $this
        ->t('The name of a valid frontend client on the GSA device (case sensitive).'),
      '#default_value' => $settings['connection_info']['frontend'],
      '#required' => TRUE,
    ];
    $form['connection_info']['timeout'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Search Timeout'),
      '#default_value' => $settings['connection_info']['timeout'],
      '#min' => 3,
      '#max' => 30,
      '#description' => $this
        ->t('Length of time to wait for response from the GSA device before giving up (timeout in seconds).'),
      '#required' => TRUE,
    ];
    $form['query_param'] = [
      '#title' => $this
        ->t('Search Query Parameter Setup'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    ];
    $form['query_param']['autofilter'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Search Results Auto-Filtering Options'),
      '#default_value' => $settings['query_param']['autofilter'],
      '#options' => [
        '0' => $this
          ->t('No filtering'),
        's' => $this
          ->t('Duplicate Directory Filter'),
        'p' => $this
          ->t('Duplicate Snippet Filter'),
        '1' => $this
          ->t('Both Duplicate Directory and Duplicate Snippet Filters'),
      ],
      '#description' => $this
        ->t('Learn more about GSA auto-filtering @gsa_link. In general, employing both filters enhances results, but sites with smaller indexes may suffer from over-filtered results.', [
        '@gsa_link' => Link::fromTextAndUrl($this
          ->t('here'), Url::fromUri('http://code.google.com/apis/searchappliance/documentation/68/xml_reference.html#request_filter_auto'))
          ->toString(),
      ]),
    ];

    // // @todo: FIX
    //    if (\Drupal::moduleHandler()->moduleExists('locale')) {
    //      $form['query_param']['language_filter_toggle'] = [
    //        '#type' => 'checkbox',
    //        '#title' => $this->t('Enable Language Filtering'),
    //        '#default_value' => $settings['query_param']['language_filter_toggle'],
    //      ];
    //      $form['query_param']['language_filter_options'] = [
    //        '#type' => 'checkboxes',
    //        '#title' => $this->t('Restrict searches to specified languages'),
    //        '#default_value' => $settings['query_param']['language_filter_options'],
    //        // @todo: FIX
    //        '#options' => [
    //            '***CURRENT_LANGUAGE***' => $this->t("Current user's language"),
    //            '***DEFAULT_LANGUAGE***' => $this->t('Default site language'),
    //          ] + locale_language_list(),
    //        '#states' => [
    //          'visible' => [
    //            ':input[name=language_filter_toggle]' => ['checked' => TRUE],
    //          ],
    //        ],
    //        '#description' => $this->t('If there are no results in the specified language, the search appliance is expected to return results in all languages.'),
    //      ];
    //    }.
    $form['query_param']['query_inspection'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable Query Inspection'),
      '#description' => $this
        ->t('Inspect the search query parameters sent to the GSA device in the Drupal message area every time a search is performed. Only really useful for sites not using the Devel module, as dsm() provides more information. The inspector is only shown to administrators, but should be disabled in a production environment.'),
      '#default_value' => $settings['query_param']['query_inspection'],
    ];
    $form['display_settings'] = [
      '#title' => $this
        ->t('Search Interface Settings'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    ];
    $form['display_settings']['drupal_path'] = [
      '#title' => $this
        ->t('Search path'),
      '#type' => 'textfield',
      '#field_prefix' => '<span dir="ltr">' . Url::fromUserInput('/', [
        'absolute' => TRUE,
      ])
        ->toString(),
      '#field_suffix' => '</span>&lrm;',
      '#default_value' => $settings['display_settings']['drupal_path'],
      '#description' => $this
        ->t('The URL of the search page provided by this module. Include neither leading nor trailing slash.'),
      '#required' => TRUE,
    ];
    $form['display_settings']['search_title'] = [
      '#title' => $this
        ->t('Search Name'),
      '#type' => 'textfield',
      '#default_value' => $settings['display_settings']['search_title'],
      '#description' => $this
        ->t('Serves as the page title on results pages and the default menu item title'),
      '#required' => TRUE,
    ];
    $form['display_settings']['results_per_page'] = [
      '#title' => $this
        ->t('Results per page'),
      '#type' => 'number',
      '#default_value' => $settings['display_settings']['results_per_page'],
      '#min' => 1,
      '#max' => 1000,
      '#description' => $this
        ->t('Number of results to show on the results page. More results will be available via a Drupal pager.'),
      '#required' => TRUE,
    ];
    $form['display_settings']['spelling_suggestions'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Display Spelling Suggestions'),
      '#default_value' => $settings['display_settings']['spelling_suggestions'],
    ];
    $form['display_settings']['advanced_search_reporting'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable Advanced Search Reporting'),
      '#default_value' => $settings['display_settings']['advanced_search_reporting'],
      '#description' => $this
        ->t('Learn more about @this_feature. You need to enable Advanced Search Reporting on the front end client. The device should provide a file named "/clicklog_compiled.js" when using the search interface on the GSA.', [
        '@this_feature' => Link::fromTextAndUrl($this
          ->t('this feature'), Url::fromUri('http://www.google.com/support/enterprise/static/gsa/docs/admin/70/gsa_doc_set/xml_reference/advanced_search_reporting.html'))
          ->toString(),
      ]),
    ];
    $form['display_settings']['sitelinks_search_box'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable Google.com Sitelinks Search Box'),
      '#default_value' => $settings['display_settings']['sitelinks_search_box'],
      '#description' => $this
        ->t('Learn more about @this_feature. Note: your site may not necessarily be a candidate for the google.com Sitelinks search box.', [
        '@this_feature' => Link::fromTextAndUrl($this
          ->t('this feature'), Url::fromUri('https://developers.google.com/structured-data/slsb-overview'))
          ->toString(),
      ]),
    ];
    $form['display_settings']['onebox_modules'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Onebox modules'),
      '#description' => $this
        ->t('A list of Onebox modules, one per line. Each module listed will have a corresponding block. These blocks must be placed via the block configuration page, or another layout mechanism like Context or Panels.'),
      '#default_value' => $settings['display_settings']['onebox_modules'],
    ];
    $form['display_settings']['error_gsa_no_results'] = [
      '#title' => $this
        ->t('No results error message'),
      '#type' => 'text_format',
      '#default_value' => $settings['display_settings']['error_gsa_no_results']['value'],
      '#format' => $settings['display_settings']['error_gsa_no_results']['format'],
      '#description' => $this
        ->t('The message displayed to the user when no results are found for the given search query.'),
      '#required' => TRUE,
    ];
    $form['display_settings']['error_curl_error'] = [
      '#title' => $this
        ->t('Connection error message'),
      '#type' => 'text_format',
      '#default_value' => $settings['display_settings']['error_curl_error']['value'],
      '#format' => $settings['display_settings']['error_curl_error']['format'],
      '#description' => $this
        ->t('The message displayed to the user when there is an error connecting to the search appliance.'),
      '#required' => TRUE,
    ];
    $form['display_settings']['error_lib_xml_parse_error'] = [
      '#title' => $this
        ->t('XML parse error message'),
      '#type' => 'text_format',
      '#default_value' => $settings['display_settings']['error_lib_xml_parse_error']['value'],
      '#format' => $settings['display_settings']['error_lib_xml_parse_error']['format'],
      '#description' => $this
        ->t('The message displayed to the user when the XML returned by the appliance is malformed.'),
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $oneboxValue = explode("\n", $form_state
      ->getValue('onebox_modules'));
    $oneboxValue = array_map('trim', $oneboxValue);
    $oneboxValue = array_filter($oneboxValue, 'strlen');
    $this->configFactory
      ->getEditable($this
      ->getEditableConfigNames())
      ->set('connection_info.hostname', $form_state
      ->getValue('hostname'))
      ->set('connection_info.collection', $form_state
      ->getValue('collection'))
      ->set('connection_info.frontend', $form_state
      ->getValue('frontend'))
      ->set('connection_info.timeout', $form_state
      ->getValue('timeout'))
      ->set('query_param.autofilter', $form_state
      ->getValue('autofilter'))
      ->set('query_param.query_inspection', $form_state
      ->getValue('query_inspection'))
      ->set('display_settings.drupal_path', trim($form_state
      ->getValue('drupal_path'), '/'))
      ->set('display_settings.search_title', $form_state
      ->getValue('search_title'))
      ->set('display_settings.results_per_page', $form_state
      ->getValue('results_per_page'))
      ->set('display_settings.spelling_suggestions', $form_state
      ->getValue('spelling_suggestions'))
      ->set('display_settings.advanced_search_reporting', $form_state
      ->getValue('advanced_search_reporting'))
      ->set('display_settings.sitelinks_search_box', $form_state
      ->getValue('sitelinks_search_box'))
      ->set('display_settings.onebox_modules', $oneboxValue)
      ->set('display_settings.error_gsa_no_results', $form_state
      ->getValue('error_gsa_no_results'))
      ->set('display_settings.error_curl_error', $form_state
      ->getValue('error_curl_error'))
      ->set('display_settings.error_lib_xml_parse_error', $form_state
      ->getValue('error_lib_xml_parse_error'))
      ->save();
    $this->routeBuilder
      ->rebuild();
    parent::submitForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);

    // @todo
    //   For security, we check that the user has access to use these filters.
    //    $field_text_format_keys = [
    //      'error_gsa_no_results',
    //      'error_curl_error',
    //      'error_lib_xml_parse_error',
    //    ];
    //    $formats = filter_formats();
    //    foreach ($field_text_format_keys as $field) {
    //      if (!filter_access($formats[$form_state['values'][$field]['format']])) {
    //        form_set_error($field . '][format', $this->t('An illegal choice has been detected. Please contact the site administrator.'));
    //      }
    //      else {
    //        // Alter the formatted text area settings to our expectations.
    //        $form_state['values'][$field . '_format'] = $form_state['values'][$field]['format'];
    //        $form_state['values'][$field] = trim($form_state['values'][$field]['value']);
    //      }
    //    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdministrationForm::$routeBuilder protected property Route builder.
AdministrationForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
AdministrationForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
AdministrationForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
AdministrationForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AdministrationForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
AdministrationForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
AdministrationForm::__construct public function Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase::__construct
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.