View source
<?php
namespace Drupal\fontawesome\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\UrlHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Asset\LibraryDiscovery;
class SettingsForm extends ConfigFormBase {
protected $libraryDiscovery;
public function __construct(LibraryDiscovery $library_discovery) {
$this->libraryDiscovery = $library_discovery;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('library.discovery'));
}
public function getFormId() {
return 'fontawesome_admin_settings_form';
}
protected function getEditableConfigNames() {
return [
'fontawesome.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$fontawesome_config = $this
->config('fontawesome.settings');
$fontawesome_library = $this->libraryDiscovery
->getLibraryByName('fontawesome', 'fontawesome');
$fontawesome_cdn_library = $this->libraryDiscovery
->getLibraryByName('fontawesome', 'fontawesome.cdn');
$form['external'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('External file configuration'),
'#description' => $this
->t('These settings control the method by which the Font Awesome library is loaded. You can choose to use an external library by selecting a URL below, or you can use a local version of the file by leaving the box unchecked and downloading the library <a href=":remoteurl">:remoteurl</a> and installing locally at %installpath.', [
':remoteurl' => $fontawesome_library['remote'],
'%installpath' => '/libraries',
]),
'fontawesome_use_cdn' => [
'#type' => 'checkbox',
'#title' => $this
->t('Use external file?'),
'#description' => $this
->t('Checking this box will cause the Font Awesome library to be loaded externally rather than from the local library file.'),
'#default_value' => $fontawesome_config
->get('fontawesome_use_cdn'),
],
'fontawesome_external_location' => [
'#type' => 'textfield',
'#title' => $this
->t('External Library Location'),
'#default_value' => empty($fontawesome_config
->get('fontawesome_external_location')) ? $fontawesome_cdn_library['css'][0]['data'] : $fontawesome_config
->get('fontawesome_external_location'),
'#size' => 80,
'#description' => $this
->t('Enter a source URL for the external Font Awesome library you wish to use. This URL should point to the Font Awesome CSS file. Leave blank to use the default Font Awesome CDN.'),
'#states' => [
'disabled' => [
':input[name="fontawesome_use_cdn"]' => [
'checked' => FALSE,
],
],
],
],
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
if (!empty($values['fontawesome_external_location']) && !UrlHelper::isValid($values['fontawesome_external_location'])) {
$form_state
->setErrorByName('fontawesome_external_location', $this
->t('Invalid external library location.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$this->libraryDiscovery
->clearCachedDefinitions();
$this
->config('fontawesome.settings')
->set('fontawesome_use_cdn', $values['fontawesome_use_cdn'])
->set('fontawesome_external_location', (string) $values['fontawesome_external_location'])
->save();
parent::submitForm($form, $form_state);
}
}