You are here

public function ShortenAdminForm::buildForm in Shorten URLs 8

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

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/ShortenAdminForm.php, line 33

Class

ShortenAdminForm
Settings form.

Namespace

Drupal\shorten\Form

Code

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);
}