class ShurlyCreateForm in ShURLy 8
ShurlyCreateForm.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\shurly\Form\ShurlyCreateForm
Expanded class hierarchy of ShurlyCreateForm
1 string reference to 'ShurlyCreateForm'
File
- src/
Form/ ShurlyCreateForm.php, line 11
Namespace
Drupal\shurly\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
ShurlyCreateForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ShurlyCreateForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ShurlyCreateForm:: |
public | function | ||
ShurlyCreateForm:: |
public | function | ||
ShurlyCreateForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ShurlyCreateForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |