class SettingsForm in Imagecache External 8
Defines a form that configures uploadcare settings.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\imagecache_external\Form\SettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SettingsForm
1 string reference to 'SettingsForm'
File
- src/
Form/ SettingsForm.php, line 18
Namespace
Drupal\imagecache_external\FormView source
class SettingsForm extends ConfigFormBase {
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a new SettingsForm.
*
* @param \Drupal\Core\File\FileSystemInterface $fileSystem
* The file system service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(FileSystemInterface $fileSystem, ConfigFactoryInterface $config_factory) {
$this->fileSystem = $fileSystem;
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('file_system'), $container
->get('config.factory'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'imagecache_external_admin_settings_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'imagecache_external.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('imagecache_external.settings');
$form['imagecache_directory'] = [
'#type' => 'textfield',
'#title' => t('Imagecache Directory'),
'#required' => TRUE,
'#description' => t('Where, within the files directory, should the downloaded images be stored?'),
'#default_value' => $config
->get('imagecache_directory'),
'#validate' => '::validateForm',
];
$form['imagecache_default_extension'] = [
'#type' => 'select',
'#options' => [
'' => 'none',
'.jpg' => 'jpg',
'.png' => 'png',
'.gif' => 'gif',
'.jpeg' => 'jpeg',
],
'#title' => t('Imagecache default extension'),
'#required' => FALSE,
'#description' => t('If no extension is provided by the external host, specify a default extension'),
'#default_value' => $config
->get('imagecache_default_extension'),
];
$form['imagecache_external_management'] = [
'#type' => 'radios',
'#title' => t('How should Drupal handle the files?'),
'#description' => t('Managed files can be re-used elsewhere on the site, for instance in the Media Library if you use the Media module. Unmanaged files are not saved to the database, but can be cached using Image Styles.'),
'#options' => [
'unmanaged' => t('Unmanaged: Only save the images to the files folder to be able to cache them. This is default.'),
'managed' => t('Managed: Download the images and save its metadata to the database.'),
],
'#default_value' => $config
->get('imagecache_external_management'),
];
$form['imagecache_external_use_whitelist'] = [
'#type' => 'checkbox',
'#title' => t('Use whitelist'),
'#description' => t('By default, all images are blocked except for images served from white-listed hosts. You can define hosts below.'),
'#default_value' => $config
->get('imagecache_external_use_whitelist'),
];
$form['imagecache_external_hosts'] = [
'#type' => 'textarea',
'#title' => t('Imagecache External hosts'),
'#description' => t('Add one host per line. You can use top-level domains to whitelist subdomains. Ex: staticflickr.com to whitelist farm1.staticflickr.com and farm2.staticflickr.com'),
'#default_value' => $config
->get('imagecache_external_hosts'),
'#states' => [
'visible' => [
':input[name="imagecache_external_use_whitelist"]' => [
'checked' => TRUE,
],
],
],
];
$form['imagecache_fallback_image'] = [
'#type' => 'managed_file',
'#name' => 'imagecache_fallback_image',
'#title' => t('Fallback image'),
'#description' => t("When an external image couldn't be found, use this image as a fallback."),
'#default_value' => $config
->get('imagecache_fallback_image'),
'#upload_location' => 'public://',
];
$form['imagecache_external_cron_flush_frequency'] = [
'#type' => 'number',
'#title' => $this
->t('Cron cache flush frequency'),
'#description' => $this
->t('The flush frequency, represented as the number of days, for flushing cached images during cron. Enter 0 to disable cron flushing.'),
'#field_suffix' => $this
->t('number of days'),
'#default_value' => $config
->get('imagecache_external_cron_flush_frequency', 0),
'#min' => 0,
'#required' => TRUE,
];
$form['#validate'][] = '::validateForm';
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$scheme = $this->configFactory
->get('system.file')
->get('default_scheme');
$directory = $scheme . '://' . $form_state
->getValue('imagecache_directory');
if (!$this->fileSystem
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY)) {
$error = $this
->t('The directory %directory does not exist or is not writable.', [
'%directory' => $directory,
]);
$form_state
->setErrorByName('imagecache_directory', $error);
$this
->logger('imagecache_external')
->error($error);
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$this
->config('imagecache_external.settings')
->set('imagecache_directory', $values['imagecache_directory'])
->set('imagecache_default_extension', $values['imagecache_default_extension'])
->set('imagecache_external_management', $values['imagecache_external_management'])
->set('imagecache_external_use_whitelist', $values['imagecache_external_use_whitelist'])
->set('imagecache_external_hosts', $values['imagecache_external_hosts'])
->set('imagecache_fallback_image', $values['imagecache_fallback_image'])
->set('imagecache_external_cron_flush_frequency', $values['imagecache_external_cron_flush_frequency'])
->save();
parent::submitForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
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 request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
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. | |
SettingsForm:: |
protected | property |
The config factory. Overrides FormBase:: |
|
SettingsForm:: |
protected | property | The file system service. | |
SettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
SettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
SettingsForm:: |
public | function |
Constructs a new SettingsForm. Overrides ConfigFormBase:: |
|
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. |