MediaThumbnailConfigForm.php in Media Thumbnails 8
File
src/Form/MediaThumbnailConfigForm.php
View source
<?php
namespace Drupal\media_thumbnails\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MediaThumbnailConfigForm extends ConfigFormBase {
protected $moduleHandler;
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('module_handler'));
}
public function getFormId() {
return 'media_thumbnails_config_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['width'] = [
'#default_value' => $this
->config('media_thumbnails.settings')
->get('width'),
'#description' => $this
->t('The width for the generated thumbnails. Height will be calculated automatically.'),
'#title' => $this
->t('Thumbnail width'),
'#type' => 'number',
];
$form['bgcolor'] = [
'#collapsed' => FALSE,
'#collapsible' => TRUE,
'#title' => t('Background color'),
'#type' => 'fieldset',
];
$form['bgcolor']['bgcolor_active'] = [
'#default_value' => $this
->config('media_thumbnails.settings')
->get('bgcolor_active'),
'#title' => $this
->t('Add a custom background color. Uncheck to keep transparency.'),
'#type' => 'checkbox',
];
$form['bgcolor']['bgcolor_value'] = [
'#default_value' => $this
->config('media_thumbnails.settings')
->get('bgcolor_value'),
'#description' => $this
->t('Background color for transparent thumbnails, for plugins supporting this feature.'),
'#title' => $this
->t('Background color'),
'#type' => 'color',
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->config('media_thumbnails.settings')
->set('width', $form_state
->getValue('width'))
->set('bgcolor_active', $form_state
->getValue('bgcolor_active'))
->set('bgcolor_value', $form_state
->getValue('bgcolor_value'))
->save();
parent::submitForm($form, $form_state);
}
protected function getEditableConfigNames() {
return [
'media_thumbnails.settings',
];
}
}