You are here

public function Search::buildConfigurationForm in Google Site Search 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

File

src/Plugin/Search/Search.php, line 188

Class

Search
Handles search using Google Search Engine.

Namespace

Drupal\gss\Plugin\Search

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form['api_key'] = [
    '#title' => $this
      ->t('Google search API key'),
    '#type' => 'key_select',
    '#default_value' => $this->configuration['api_key'],
    '#empty_value' => '',
    '#empty_option' => $this
      ->t('- Debug mode -'),
    '#description' => '<br />' . $this
      ->t('Debug mode will generate sample content for the search results.'),
  ];
  $all_languages = $this
    ->getSupportedLanguages();
  $language_default = $this->languageManager
    ->getDefaultLanguage()
    ->getId();
  foreach ($all_languages as $langcode => $language) {
    $element_key = 'search_engine_id_' . $language
      ->getId();
    $form[$element_key] = [
      '#type' => 'textfield',
      '#default_value' => $this->configuration['search_engine_id_' . $language
        ->getId()],
    ];
    if ($langcode === $language_default) {
      $form[$element_key]['#title'] = $this
        ->t('Google search engine ID (@language, Default site language)', [
        '@language' => $language
          ->getName(),
      ]);
      $form[$element_key]['#description'] = $this
        ->t('This is used when there is no search engine configured for the current language.');
    }
    elseif ($langcode === LanguageInterface::LANGCODE_NOT_SPECIFIED) {
      $form[$element_key]['#title'] = $this
        ->t('Google search engine ID (All languages)');
      $form[$element_key]['#description'] = $this
        ->t('This is used when there is no search engine configured for the current language or the default site language.');
    }
    else {
      $form[$element_key]['#title'] = $this
        ->t('Google search engine ID (@language)', [
        '@language' => $language
          ->getName(),
      ]);
    }
  }
  $form['base_url'] = array(
    '#title' => $this
      ->t('Search engine base url'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('The base URL to send the query to. Use this to override the default request to Google, useful for proxying the request.'),
    '#default_value' => $this->configuration['base_url'],
  );
  $form['miscellaneous'] = array(
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Miscellaneous'),
  );
  $form['miscellaneous']['page_size'] = array(
    '#title' => $this
      ->t('Page size'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('Number of results to display per page.'),
    '#default_value' => $this->configuration['page_size'],
    '#size' => 5,
    '#max_length' => 5,
  );
  $form['miscellaneous']['pager_size'] = array(
    '#title' => $this
      ->t('Pager size'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('Number of pages to show in the pager. Input ONLY odd numbers like 5, 7 or 9 and NOT 6, 8 or 10, for example.'),
    '#default_value' => $this->configuration['pager_size'],
    '#size' => 5,
    '#max_length' => 5,
  );
  $form['miscellaneous']['images'] = array(
    '#title' => $this
      ->t('Image Search'),
    '#type' => 'checkbox',
    '#description' => $this
      ->t('Enable image search.'),
    '#default_value' => $this->configuration['images'],
  );
  $form['miscellaneous']['labels'] = array(
    '#title' => $this
      ->t('Show labels'),
    '#type' => 'checkbox',
    '#description' => $this
      ->t('Let the user filter the search result by labels. <a href=":search-labels">Read more about search labels</a>.', [
      ':search-labels' => 'https://developers.google.com/custom-search/docs/ref_prebuiltlabels',
    ]),
    '#default_value' => $this->configuration['labels'],
  );
  return $form;
}