You are here

class ShortenAdminForm in Shorten URLs 8

Same name and namespace in other branches
  1. 8.2 src/Form/ShortenAdminForm.php \Drupal\shorten\Form\ShortenAdminForm

Settings form.

Hierarchy

Expanded class hierarchy of ShortenAdminForm

1 string reference to 'ShortenAdminForm'
shorten.routing.yml in ./shorten.routing.yml
shorten.routing.yml

File

src/Form/ShortenAdminForm.php, line 12

Namespace

Drupal\shorten\Form
View source
class ShortenAdminForm extends ConfigFormBase {

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

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'shorten.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
    $config = $this
      ->config('shorten.settings');
    $form['shorten_www'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use "www." instead of "http://"'),
      '#description' => t('"www." is shorter, but "http://" is automatically link-ified by more services.'),
      '#default_value' => \Drupal::config('shorten.settings')
        ->get('shorten_www'),
    );
    $methods = array();
    if (function_exists('file_get_contents')) {
      $methods['php'] = t('PHP');
    }
    if (function_exists('curl_exec')) {
      $methods['curl'] = t('cURL');
    }
    if (\Drupal::config('shorten.settings')
      ->get('shorten_method') != 'none') {
      \Drupal::configFactory()
        ->getEditable('shorten.settings')
        ->set('shorten_method', _shorten_method_default())
        ->save();
    }
    if (empty($methods)) {
      $form['shorten_method'] = array(
        '#type' => 'radios',
        '#title' => t('Method'),
        '#description' => '<p>' . t('The method to use to retrieve the abbreviated URL.') . '</p>' . '<p><strong>' . t('Your PHP installation does not support the URL abbreviation feature of the Shorten module.') . '</strong> ' . t('You must compile PHP with either the cURL library or the file_get_contents() function to use this option.') . '</p>',
        '#default_value' => 'none',
        '#options' => array(
          'none' => t('None'),
        ),
        '#disabled' => TRUE,
      );
      $form['shorten_service'] = array(
        '#type' => 'radios',
        '#title' => t('Service'),
        '#description' => t('The default service to use to create the abbreviated URL.'),
        '#default_value' => 'none',
        '#options' => array(
          'none' => t('None'),
        ),
      );
      $form['shorten_service_backup'] = array(
        '#type' => 'radios',
        '#title' => t('Backup Service'),
        '#description' => t('The service to use to create the abbreviated URL if the primary service is down.'),
        '#default_value' => 'none',
        '#options' => array(
          'none' => t('None'),
        ),
      );
    }
    else {
      $form['shorten_method'] = array(
        '#type' => 'radios',
        '#title' => t('Method'),
        '#description' => t('The method to use to retrieve the abbreviated URL. cURL is much faster, if available.'),
        '#default_value' => \Drupal::config('shorten.settings')
          ->get('shorten_method'),
        '#options' => $methods,
      );
      $all_services = \Drupal::moduleHandler()
        ->invokeAll('shorten_service');
      $services = array();
      foreach ($all_services as $key => $value) {
        $services[$key] = $key;
      }
      $services['none'] = t('None');
      $form['shorten_service'] = array(
        '#type' => 'select',
        '#title' => t('Service'),
        '#description' => t('The default service to use to create the abbreviated URL.') . ' ' . t('If a service is not shown in this list, you probably need to configure it in the Shorten API Keys tab.'),
        '#default_value' => \Drupal::config('shorten.settings')
          ->get('shorten_service'),
        '#options' => $services,
      );
      $form['shorten_service_backup'] = array(
        '#type' => 'select',
        '#title' => t('Backup Service'),
        '#description' => t('The service to use to create the abbreviated URL if the primary or requested service is down.'),
        '#default_value' => \Drupal::config('shorten.settings')
          ->get('shorten_service_backup'),
        '#options' => $services,
      );
      $form['shorten_show_service'] = array(
        '#type' => 'checkbox',
        '#title' => t('Show the list of URL shortening services in the user interface'),
        '#default_value' => \Drupal::config('shorten.settings')
          ->get('shorten_show_service'),
        '#description' => t('Allow users to choose which service to use in the Shorten URLs block and page.'),
      );
    }
    $form['shorten_use_alias'] = array(
      '#type' => 'checkbox',
      '#title' => t('Shorten aliased URLs where possible'),
      '#description' => t('Where possible, generate shortened URLs based on the aliased version of a URL.') . ' <strong>' . t('Some integrated modules ignore this.') . '</strong>',
      '#default_value' => \Drupal::config('shorten.settings')
        ->get('shorten_use_alias'),
    );
    $form['shorten_timeout'] = array(
      '#type' => 'textfield',
      '#title' => t('Time out URL shortening requests after'),
      '#field_suffix' => ' ' . t('seconds'),
      '#description' => t('Cancel retrieving a shortened URL if the URL shortening service takes longer than this amount of time to respond.') . ' ' . t('Lower values (or shorter timeouts) mean your site will respond more quickly if your URL shortening service is down.') . ' ' . t('However, higher values (or longer timeouts) give the URL shortening service more of a chance to return a value.') . ' ' . t('If a request to the primary service times out, the secondary service is used. If the secondary service times out, the original (long) URL is used.') . ' ' . t('You must enter a nonnegative integer. Enter 0 (zero) to wait for a response indefinitely.'),
      '#size' => 3,
      '#required' => TRUE,
      '#default_value' => \Drupal::config('shorten.settings')
        ->get('shorten_timeout'),
    );
    $form['shorten_cache_duration'] = array(
      '#type' => 'textfield',
      '#title' => t('Cache shortened URLs for'),
      '#field_suffix' => ' ' . t('seconds'),
      '#description' => t('Shortened URLs are stored after retrieval to improve performance.') . ' ' . t('Enter the number of seconds for which you would like the shortened URLs to be stored.') . ' ' . t('Leave this field blank to store shortened URLs indefinitely (although this is not recommended).') . ' ' . t('The default value is 1814400 (3 weeks).'),
      '#size' => 11,
      '#default_value' => \Drupal::config('shorten.settings')
        ->get('shorten_cache_duration'),
    );
    $form['shorten_cache_fail_duration'] = array(
      '#type' => 'textfield',
      '#title' => t('On failure, cache full URLs for'),
      '#field_suffix' => ' ' . t('seconds'),
      '#description' => t('When a shortener service is unavilable, the full URL will be cached temporarily to prevent more requests from overloading the server.') . ' ' . t('Enter the number of seconds for which you would like to store these full URLs when shortening the URL fails.') . ' ' . t('The default value is 1800 (30 minutes).'),
      '#size' => 11,
      '#required' => TRUE,
      '#default_value' => \Drupal::config('shorten.settings')
        ->get('shorten_cache_fail_duration'),
    );
    $form['shorten_cache_clear_all'] = array(
      '#type' => 'checkbox',
      '#title' => t('Clear Shorten URLs cache when all Drupal caches are cleared.'),
      '#description' => t('Sometimes Drupal automatically clears all caches, such as after running database updates.') . ' ' . t('However, regenerating the cache of shortened URLs can be performance-intensive, and the cache does not affect Drupal behaviors.') . ' ' . t('To avoid regenerating this cache after clearing all Drupal caches, un-check this option.') . ' ' . t('Note that if you need to completely clear this cache, un-checking this option will require that you do it manually.'),
      '#default_value' => \Drupal::config('shorten.settings')
        ->get('shorten_cache_clear_all'),
    );
    unset($services['none']);
    if (empty(unserialize(\Drupal::config('shorten.settings')
      ->get('shorten_invisible_services')))) {
      \Drupal::configFactory()
        ->getEditable('shorten.settings')
        ->set('shorten_invisible_services', serialize(array()))
        ->save();
    }
    $form['shorten_invisible_services'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Disallowed services'),
      '#description' => t('Checking the box next to a service will make it <strong>unavailable</strong> for use in the Shorten URLs block and page.') . ' ' . t('If you disallow all services, the primary service will be used.'),
      '#default_value' => unserialize(\Drupal::config('shorten.settings')
        ->get('shorten_invisible_services')),
      '#options' => $services,
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $this
      ->config('shorten.settings')
      ->set('shorten_www', $values['shorten_www'])
      ->set('shorten_method', $values['shorten_method'])
      ->set('shorten_service', $values['shorten_service'])
      ->set('shorten_service_backup', $values['shorten_service_backup'])
      ->set('shorten_show_service', $values['shorten_show_service'])
      ->set('shorten_use_alias', $values['shorten_use_alias'])
      ->set('shorten_timeout', $values['shorten_timeout'])
      ->set('shorten_cache_duration', $values['shorten_cache_duration'])
      ->set('shorten_cache_fail_duration', $values['shorten_cache_fail_duration'])
      ->set('shorten_cache_clear_all', $values['shorten_cache_clear_all'])
      ->set('shorten_invisible_services', serialize($values['shorten_invisible_services']))
      ->save();

    // Changed settings usually mean that different URLs should be used.
    // cache_clear_all('*', 'cache_shorten', TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $v = $form_state
      ->getValues();
    if ($v['shorten_service'] == $v['shorten_service_backup'] && $v['shorten_service_backup'] != 'none') {
      $form_state
        ->setErrorByName('shorten_service_backup', $this
        ->t('You must select a backup abbreviation service that is different than your primary service.'));
    }
    elseif ($v['shorten_service'] == 'bit.ly' && $v['shorten_service_backup'] == 'j.mp' || $v['shorten_service'] == 'j.mp' && $v['shorten_service_backup'] == 'bit.ly') {
      $form_state
        ->setErrorByName('shorten_service_backup', $this
        ->t('j.mp and bit.ly are the same service.') . ' ' . $this
        ->t('You must select a backup abbreviation service that is different than your primary service.'));
    }
    if ($v['shorten_service'] == 'none' && $v['shorten_service_backup'] != 'none') {
      $form_state
        ->setErrorByName($this
        ->t('You have selected a backup URL abbreviation service, but no primary service.') . ' ' . $this
        ->t('Your URLs will not be abbreviated with these settings.'));
    }
    if ($v['shorten_cache_duration'] !== '' && (!is_numeric($v['shorten_cache_duration']) || round($v['shorten_cache_duration']) != $v['shorten_cache_duration'] || $v['shorten_cache_duration'] < 0)) {
      $form_state
        ->setErrorByName('shorten_cache_duration', $this
        ->t('The cache duration must be a positive integer or left blank.'));
    }
    if (!is_numeric($v['shorten_cache_fail_duration']) || round($v['shorten_cache_fail_duration']) != $v['shorten_cache_fail_duration'] || $v['shorten_cache_fail_duration'] < 0) {
      $form_state
        ->setErrorByName('shorten_cache_fail_duration', $this
        ->t('The cache fail duration must be a positive integer.'));
    }
    if (!is_numeric($v['shorten_timeout']) || round($v['shorten_timeout']) != $v['shorten_timeout'] || $v['shorten_timeout'] < 0) {
      $form_state
        ->setErrorByName('shorten_timeout', $this
        ->t('The timeout duration must be a nonnegative integer.'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
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::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
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.
ShortenAdminForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ShortenAdminForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ShortenAdminForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ShortenAdminForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ShortenAdminForm::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.