You are here

class ShurlyCreateForm in ShURLy 8

ShurlyCreateForm.

Hierarchy

Expanded class hierarchy of ShurlyCreateForm

1 string reference to 'ShurlyCreateForm'
shurly.routing.yml in ./shurly.routing.yml
shurly.routing.yml

File

src/Form/ShurlyCreateForm.php, line 11

Namespace

Drupal\shurly\Form
View source
class ShurlyCreateForm extends FormBase {

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['wrapper'] = [
      '#prefix' => '<div id="shurly-create">',
      '#suffix' => '</div>',
    ];
    $form['wrapper']['long_url'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#default_value' => $form_state
        ->getValue('long_url'),
      '#maxlength' => 2048,
      '#placeholder' => $this
        ->t('Shorten your link'),
      '#attributes' => [
        'autocomplete' => 'off',
      ],
    ];
    $form['wrapper']['options'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Custom URL'),
      '#access' => \Drupal::currentUser()
        ->hasPermission('Enter custom URLs'),
    ];
    $form['wrapper']['options']['short_url'] = [
      '#type' => 'textfield',
      '#default_value' => $form_state
        ->getValue('short_url'),
      '#size' => 6,
      '#field_prefix' => _shurly_get_shurly_base() . '/',
      '#attributes' => [
        'autocomplete' => 'off',
      ],
    ];
    $form['wrapper']['actions'] = [
      '#type' => 'actions',
    ];
    $form['wrapper']['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('Shorten'),
      '#attributes' => [
        'tabindex' => 3,
      ],
      '#ajax' => [
        'callback' => '::submitAjaxCall',
        'wrapper' => 'shurly-create',
        'event' => 'click',
      ],
    ];
    $form['wrapper']['actions']['reset'] = [
      '#type' => 'submit',
      '#value' => t('Shorten another'),
      '#attributes' => [
        'tabindex' => 3,
      ],
      '#access' => FALSE,
      '#ajax' => [
        'callback' => '::resetAjaxCall',
        'wrapper' => 'shurly-create',
        'event' => 'click',
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $rate_limit = shurly_rate_limit_allowed();
    if (!$rate_limit['allowed']) {
      $form_state
        ->setError('', $this
        ->t('Rate limit exceeded. You are limited to @rate requests per @time minute period.', [
        '@rate' => $rate_limit['rate'],
        '@time' => $rate_limit['time'],
      ]));
      return;
    }
    $form_state
      ->setValue('long_url', trim($form_state
      ->getValue('long_url')));
    $form_state
      ->setValue('short_url', trim($form_state
      ->getValue('short_url')));
    $vals = $form_state
      ->getValues();
    $long_url_name = 'wrapper][long_url';
    $short_url_name = 'wrapper][options][short_url';
    if ($vals['long_url']) {

      // Check that they've entered a URL.
      if (!preg_match('/^https?:\\/\\//', $vals['long_url'])) {
        $form_state
          ->setErrorByName($long_url_name, $this
          ->t('Please enter a web URL.'));
      }
      elseif (!shurly_validate_long($vals['long_url'])) {
        $form_state
          ->setErrorByName($long_url_name, t('Invalid URL.'));
      }
    }
    if ($vals['short_url']) {

      // A custom short URL has been entered.
      $form_state
        ->setValue('custom', [
        TRUE,
      ]);
      if (!shurly_validate_custom($vals['short_url'])) {
        $form_state
          ->setErrorByName($short_url_name, $this
          ->t('Short URL contains invalid characters.'));
      }
      elseif ($exists = shurly_url_exists($vals['short_url'], $vals['long_url'])) {
        $form_state
          ->setErrorByName($short_url_name, $this
          ->t('This short URL has already been used.'));
      }
      elseif (_surl($vals['short_url'], [
        'absolute' => TRUE,
      ]) == $vals['long_url'] || _surl($vals['short_url'], [
        'absolute' => TRUE,
        'base_url' => _shurly_get_shurly_base(),
      ]) == $vals['long_url']) {

        // Check that link isn't to itself (creating infinite loop)
        // problem - http vs https.
        $form_state
          ->setErrorByName($short_url_name, $this
          ->t('You cannot create links to themselves'));
      }
      elseif (!shurly_path_available($vals['short_url'])) {
        $form_state
          ->setErrorByName($short_url_name, $this
          ->t('This custom URL is reserved. Please choose another.'));
      }
    }
    else {

      // Custom short URL field is empty.
      $form_state
        ->setValue('custom', TRUE);
      if ($exist = shurly_get_latest_short($vals['long_url'], \Drupal::currentUser()
        ->id())) {
        $short = $exist;

        // We flag this as URL Exists so that it displays but doesn't get saved to the db.
        $form_state
          ->setValue('url_exists', TRUE);
      }
      else {
        $short = shurly_next_url();
      }
      $form_state
        ->setValue('short_url', $short);
      $form_state
        ->setStorage([
        'shurly' => [
          'short_url' => $short,
        ],
      ]);
    }

    // Check that the destination URL is "safe".
    if (\Drupal::config('shurly.settings')
      ->get('shurly_gsb')) {
      $gsb = shurly_gsb($vals['long_url']);
      if ($gsb) {
        $form_state
          ->setErrorByName($long_url_name, t('This URL is either phishing, malware, or both.'));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // AJAX submit only.
  }

  /**
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *
   * @return mixed
   */
  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'];
  }

  /**
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *
   * @return mixed
   */
  public function resetAjaxCall(array &$form, FormStateInterface $form_state) {
    $form['wrapper']['final_url']['#access'] = FALSE;
    $form['wrapper']['actions']['reset']['#access'] = FALSE;
    $form['wrapper']['long_url']['#access'] = TRUE;
    $form['wrapper']['options']['#access'] = TRUE;
    $form['wrapper']['actions']['submit']['#access'] = TRUE;
    return $form['wrapper'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
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.
ShurlyCreateForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
ShurlyCreateForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ShurlyCreateForm::resetAjaxCall public function
ShurlyCreateForm::submitAjaxCall public function
ShurlyCreateForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ShurlyCreateForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
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.