You are here

public function ShurlyCreateForm::submitAjaxCall in ShURLy 8

Parameters

array $form:

\Drupal\Core\Form\FormStateInterface $form_state:

Return value

mixed

File

src/Form/ShurlyCreateForm.php, line 177

Class

ShurlyCreateForm
ShurlyCreateForm.

Namespace

Drupal\shurly\Form

Code

public function submitAjaxCall(array &$form, FormStateInterface $form_state) {
  $errors = $form_state
    ->getErrors();
  $long_url = $form_state
    ->getValue('long_url');
  $short_url = $form_state
    ->getValue('short_url');
  if (count($errors)) {
    $form['wrapper']['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -999,
    ];
    if (isset($errors['wrapper][options][short_url'])) {
      $form['wrapper']['options']['#open'] = TRUE;
    }
    return $form['wrapper'];
  }
  $storage = [
    'shurly' => [
      'long_url' => $long_url,
      'short_url' => $short_url,
      'final_url' => urldecode(_shurly_get_shurly_base() . '/' . $short_url),
    ],
  ];
  $custom = $form_state
    ->setValue('custom', [
    $form_state
      ->getValue('custom'),
  ]);
  if (empty($form_state
    ->getValue('url_exists'))) {
    shurly_save_url($long_url, $short_url, NULL, $custom);
  }
  $form['wrapper']['final_url'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Shortened URL'),
    '#title_display' => 'invisible',
    '#value' => $storage['shurly']['final_url'],
    '#disabled' => TRUE,
    '#attributes' => [
      'readonly' => 'readonly',
      'onclick' => 'this.select()',
    ],
  ];
  $form['wrapper']['long_url']['#access'] = FALSE;
  $form['wrapper']['options']['#access'] = FALSE;
  $form['wrapper']['actions']['submit']['#access'] = FALSE;
  $form['wrapper']['actions']['reset']['#access'] = TRUE;
  return $form['wrapper'];
}