You are here

public function CdnSettingsForm::buildForm in CDN 8.3

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

cdn_ui/src/Form/CdnSettingsForm.php, line 82

Class

CdnSettingsForm
Configure CDN settings for this site.

Namespace

Drupal\cdn_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('cdn.settings');
  $form['cdn_settings'] = [
    '#type' => 'vertical_tabs',
    '#default_tab' => 'edit-mapping',
    '#attached' => [
      'library' => [
        'cdn_ui/summaries',
      ],
    ],
  ];
  $form['status'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Status'),
    '#group' => 'cdn_settings',
  ];
  $form['status']['status'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Serve files from CDN'),
    '#description' => $this
      ->t('Better performance thanks to better caching of files by the visitor. When a file changes a different URL is used, to ensure instantaneous updates for your visitors.'),
    '#default_value' => $config
      ->get('status'),
  ];
  $form['mapping'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Mapping'),
    '#group' => 'cdn_settings',
    '#tree' => TRUE,
  ];
  $mapping_type_ui_string = $this
    ->t('Use @mapping-type mapping');
  [
    $mapping_type_ui_string_prefix,
    $mapping_type_ui_string_suffix,
  ] = explode('@mapping-type', (string) $mapping_type_ui_string, 2);
  $form['mapping']['type'] = [
    '#field_prefix' => $mapping_type_ui_string_prefix,
    '#field_suffix' => $mapping_type_ui_string_suffix,
    '#type' => 'select',
    '#title' => $this
      ->t('Mapping type'),
    '#title_display' => 'invisible',
    '#options' => [
      'simple' => $this
        ->t('simple'),
      'advanced' => $this
        ->t('advanced'),
    ],
    '#required' => TRUE,
    '#wrapper_attributes' => [
      'class' => [
        'container-inline',
      ],
    ],
    '#attributes' => [
      'class' => [
        'container-inline',
      ],
    ],
    '#default_value' => $config
      ->get('mapping.type') === 'simple' ?: 'advanced',
  ];
  $form['mapping']['simple'] = [
    '#type' => 'container',
    '#states' => [
      'visible' => [
        ':input[name="mapping[type]"]' => [
          'value' => 'simple',
        ],
      ],
    ],
    '#attributes' => [
      'class' => [
        'container-inline',
      ],
    ],
  ];
  $simple_mapping_ui_string = $this
    ->t('Serve @files-with-some-extension from @domain using @type-of-urls');
  [
    $simple_mapping_ui_string_part_one,
    $simple_mapping_ui_string_part_two,
    $simple_mapping_ui_string_part_three,
  ] = preg_split('/\\@[a-z\\-]+/', (string) $simple_mapping_ui_string, -1, PREG_SPLIT_NO_EMPTY);
  $form['mapping']['simple']['extensions_condition_toggle'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Limit by file extension'),
    '#title_display' => 'invisible',
    '#field_prefix' => $simple_mapping_ui_string_part_one,
    '#options' => [
      'all' => $this
        ->t('all files'),
      'nocssjs' => $this
        ->t('all files except CSS+JS'),
      'limited' => $this
        ->t('only files'),
    ],
    '#default_value' => $config
      ->get('mapping.conditions') === static::CONDITIONS_PRESET_NOCSSJS ? 'nocssjs' : (empty($config
      ->get('mapping.conditions.extensions')) ? 'all' : 'limited'),
  ];
  $form['mapping']['simple']['extensions_condition_value'] = [
    '#field_prefix' => $this
      ->t('with the extension'),
    '#type' => 'textfield',
    '#title' => $this
      ->t('Allowed file extensions'),
    '#title_display' => 'invisible',
    '#placeholder' => 'jpg jpeg png zip',
    '#size' => 30,
    '#default_value' => implode(' ', $config
      ->get('mapping.conditions.extensions') ?: []),
    '#states' => [
      'visible' => [
        ':input[name="mapping[simple][extensions_condition_toggle]"]' => [
          'value' => 'limited',
        ],
      ],
    ],
  ];
  $form['mapping']['simple']['domain'] = [
    '#field_prefix' => $simple_mapping_ui_string_part_two,
    '#type' => 'textfield',
    '#placeholder' => 'example.com',
    '#title' => $this
      ->t('Domain'),
    '#title_display' => 'FALSE',
    '#size' => 25,
    '#default_value' => $config
      ->get('mapping.domain'),
  ];
  $form['mapping']['advanced'] = [
    '#type' => 'item',
    '#markup' => '<em>' . $this
      ->t('Not configurable through the UI. Modify <code>cdn.settings.yml</code> directly, and <a href=":url">import it</a>. It is safe to edit all other settings via the UI.', [
      ':url' => 'https://www.drupal.org/documentation/administer/config',
    ]) . '</em>',
    '#states' => [
      'visible' => [
        ':input[name="mapping[type]"]' => [
          'value' => 'advanced',
        ],
      ],
    ],
  ];
  $form['mapping']['simple']['scheme'] = [
    '#field_prefix' => $simple_mapping_ui_string_part_three,
    '#type' => 'select',
    '#title' => $this
      ->t('Type of URLs'),
    '#title_display' => 'FALSE',
    '#options' => [
      '//' => $this
        ->t('scheme-relative URLs'),
      'https://' => $this
        ->t('HTTPS URLs'),
      'http://' => $this
        ->t('HTTP URLs'),
    ],
    '#default_value' => $config
      ->get('scheme'),
  ];
  $form['farfuture'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Forever cacheable files'),
    '#group' => 'cdn_settings',
    '#tree' => TRUE,
  ];
  $form['farfuture']['status'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Make files cacheable forever'),
    '#description' => $this
      ->t('Better performance thanks to better caching of files by the visitor. When a file changes a different URL is used, to ensure instantaneous updates for your visitors.'),
    '#default_value' => $config
      ->get('farfuture.status'),
  ];
  $visible_stream_wrappers = $this->streamWrapperManager
    ->getWrappers(StreamWrapperInterface::VISIBLE);
  $non_core_visible_stream_wrappers = array_filter($visible_stream_wrappers, function (array $metadata) {
    return strpos($metadata['class'], 'Drupal\\Core') !== 0;
  });
  $form['wrappers'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Stream wrappers'),
    '#group' => 'cdn_settings',
    '#tree' => TRUE,
    '#access' => !empty($non_core_visible_stream_wrappers),
  ];
  $checkboxes = $this
    ->buildStreamWrapperCheckboxes(array_keys($visible_stream_wrappers));
  $form['wrappers']['stream_wrappers'] = [
    '#type' => 'checkboxes',
    '#options' => array_combine(array_keys($checkboxes), array_keys($checkboxes)),
    '#default_value' => $config
      ->get('stream_wrappers'),
    '#description' => $this
      ->t('Stream wrappers whose files to serve from CDN. Any stream wrapper generating local file URLs is eligible.'),
  ];
  $form['wrappers']['stream_wrappers'] += $checkboxes;

  // Special case: private://.
  if (!empty($form['wrappers']['stream_wrappers']['private'])) {
    $form['wrappers']['stream_wrappers']['private']['#disabled'] = TRUE;
    $form['wrappers']['stream_wrappers']['private']['#title'] = '<del>' . $form['wrappers']['stream_wrappers']['private']['#title'] . '</del>';
    $form['wrappers']['stream_wrappers']['private']['#description'] = $this
      ->t('Private files require authentication and hence cannot be served from a CDN.');
  }
  return parent::buildForm($form, $form_state);
}