You are here

public function ScannerForm::buildForm in Search and Replace Scanner 8

Form constructor.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/ScannerForm.php, line 62

Class

ScannerForm
Form for performing searching.

Namespace

Drupal\scanner\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = \Drupal::config('scanner.admin_settings');
  $mode = $form_state
    ->getValue('scanner_mode') ? $form_state
    ->getValue('scanner_mode') : $config
    ->get('scanner_mode');
  $wholeword = $form_state
    ->getValue('scanner_wholeword') ? $form_state
    ->getValue('scanner_wholeword') : $config
    ->get('scanner_wholeword');
  $regex = $form_state
    ->getValue('scanner_regex') ? $form_state
    ->getValue('scanner_regex') : $config
    ->get('scanner_regex');
  $published = $form_state
    ->getValue('scanner_published') ? $form_state
    ->getValue('scanner_published') : $config
    ->get('scanner_published');
  $language = $form_state
    ->getValue('scanner_language') ? $form_state
    ->getValue('scanner_language') : $config
    ->get('scanner_language');
  $form['settings_link'] = [
    '#prefix' => '<p>',
    '#suffix' => '</p>',
  ];
  $form['search'] = [
    '#type' => 'textfield',
    '#default_value' => '',
    '#title' => $this
      ->t('Step 1: Search for'),
    '#maxlength' => 256,
  ];
  $form['submit_search'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Search'),
  ];
  $form['replace'] = [
    '#type' => 'textfield',
    '#default_value' => '',
    '#title' => $this
      ->t('Step 2: Replace with'),
    '#maxlength' => 256,
    '#access' => \Drupal::currentUser()
      ->hasPermission('perform search and replace') ? TRUE : FALSE,
  ];
  $form['submit_replace'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Replace'),
    '#access' => \Drupal::currentUser()
      ->hasPermission('perform search and replace') ? TRUE : FALSE,
  ];
  $form['options'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Search Options'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];
  $form['options']['surrounding'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Surrounding Text'),
    '#collapsible' => FALSE,
    '#description' => $this
      ->t('You can limit matches by providing the text that should appear immediately before or after the search text. Remember to account for spaces.  Note: Case sensitivity and regular expression options will all apply here, too. Whole word is not recommended.'),
  ];
  $form['options']['surrounding']['preceded'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Preceded by'),
    '#default_value' => '',
    '#maxlength' => 256,
  ];
  $form['options']['surrounding']['followed'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Followed by'),
    '#default_value' => '',
    '#maxlength' => 256,
  ];
  $form['options']['message'] = [
    '#type' => 'markup',
    '#markup' => $this
      ->t('The below settings override the values configured in the <a href="@url" target="_blank">admin settings page</a>.', [
      '@url' => '/admin/config/content/scanner',
    ]),
  ];
  $form['options']['mode'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Case sensitive search'),
    '#default_value' => $mode,
    '#description' => $this
      ->t('Check this if the search should only return results that exactly match the capitalization of your search terms.'),
  ];
  $form['options']['wholeword'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Match whole word'),
    '#default_value' => $wholeword,
    '#description' => $this
      ->t("Check this if you don't want the search to match any partial words. For instance, if you search for 'run', a whole word search will <em>not</em> match 'running'."),
  ];
  $form['options']['regex'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use regular expressions in search'),
    '#default_value' => $regex,
    '#description' => $this
      ->t('Check this if you want to use regular expressions in your search terms.'),
  ];
  $form['options']['published'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Published nodes only'),
    '#default_value' => $published,
    '#description' => $this
      ->t('Check this if you only want your search and replace to affect fields in nodes that are published.'),
  ];
  $form['options']['language'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Content language'),
    '#default_value' => $language,
    '#options' => $this
      ->getLanguages(),
    '#description' => $this
      ->t('The language of the content you would like to search through.'),
  ];
  $scannerStore = $this->tempStore
    ->get('scanner');

  // Empty the results on initial load, otherwise results from previous query
  // will be displayed.
  if (empty($form_state
    ->getValues())) {
    $scannerStore
      ->set('results', '');
  }
  else {
    $renderable = $scannerStore
      ->get('results');
    $markup = \Drupal::service('renderer')
      ->render($renderable);
    $form['results'] = [
      '#type' => 'markup',
      '#markup' => $markup,
    ];
  }
  return $form;
}