You are here

public function SiteVerifyAdminForm::buildForm in Site verification 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 ConfigFormBase::buildForm

File

src/Form/SiteVerifyAdminForm.php, line 32

Class

SiteVerifyAdminForm
Configure cron settings for this site.

Namespace

Drupal\site_verify\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $record = [], $engine = NULL, $site_verify = NULL) {
  if (!empty($site_verify)) {
    $record = \Drupal::service('site_verify_service')
      ->siteVerifyLoad($site_verify);
  }
  $storage = $form_state
    ->getStorage();
  if (!isset($storage['step'])) {
    $record += [
      'svid' => NULL,
      'file' => '',
      'file_contents' => $this
        ->t('This is a verification page.'),
      'meta' => '',
      'engine' => $engine,
    ];
    !empty($record['engine']) ? $form_state
      ->setStorage([
      'step' => 2,
      'record' => $record,
    ]) : $form_state
      ->setStorage([
      'step' => 1,
      'record' => $record,
    ]);
  }
  else {
    $record = $storage['record'];
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $storage = $form_state
    ->getStorage();
  switch ($storage['step']) {
    case 1:
      $engines = \Drupal::service('site_verify_service')
        ->siteVerifyGetEngines();
      $options = [];
      foreach ($engines as $key => $engine) {
        $options[$key] = $engine['name'];
      }
      asort($options);
      $form['engine'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Search engine'),
        '#options' => $options,
      ];
      break;
    case 2:
      $form['svid'] = [
        '#type' => 'value',
        '#value' => $record['svid'],
      ];
      $form['engine'] = [
        '#type' => 'value',
        '#value' => $record['engine']['key'],
      ];
      $form['engine_name'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('Search engine'),
        '#markup' => $record['engine']['name'],
      ];
      $form['#engine'] = $record['engine'];
      $form['meta'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Verification META tag'),
        '#default_value' => $record['meta'],
        '#description' => $this
          ->t('This is the full meta tag provided for verification. Note that this meta tag will only be visible in the source code of your <a href="@frontpage">front page</a>.', [
          '@frontpage' => Url::fromRoute('<front>')
            ->toString(),
        ]),
        '#element_validate' => $record['engine']['meta_validate'],
        '#access' => $record['engine']['meta'],
        '#maxlength' => NULL,
        '#attributes' => [
          'placeholder' => $record['engine']['meta_example'],
        ],
      ];
      $form['file_upload'] = [
        '#type' => 'file',
        '#title' => $this
          ->t('Upload an existing verification file'),
        '#description' => $this
          ->t('If you have been provided with an actual file, you can simply upload the file.'),
        '#access' => $record['engine']['file'],
      ];
      $form['file'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Verification file'),
        '#default_value' => $record['file'],
        '#description' => $this
          ->t('The name of the HTML verification file you were asked to upload.'),
        '#element_validate' => $record['engine']['file_validate'],
        '#access' => $record['engine']['file'],
        '#attributes' => [
          'placeholder' => $record['engine']['file_example'],
        ],
      ];
      $form['file_contents'] = [
        '#type' => 'textarea',
        '#title' => $this
          ->t('Verification file contents'),
        '#default_value' => $record['file_contents'],
        '#element_validate' => $record['engine']['file_contents_validate'],
        '#wysiwyg' => FALSE,
        '#access' => $record['file_contents'],
      ];
      if ($record['engine']['file']) {
        $form['#attributes'] = [
          'enctype' => 'multipart/form-data',
        ];
      }
      break;
  }
  $form['actions']['cancel'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Cancel'),
    '#url' => isset($_GET['destination']) ? $_GET['destination'] : Url::fromRoute('site_verify.verifications_list'),
    '#weight' => 15,
  ];
  return parent::buildForm($form, $form_state);
}