jfu-1.0.x-dev/src/Form/JfuConfigForm.php
src/Form/JfuConfigForm.php
<?php
namespace Drupal\jfu\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Utility\Environment;
use Drupal\Core\Form\FormStateInterface;
use Drupal\jfu\Services\JfuConfigServices;
/**
* Provides an config for json field utils.
*
* @internal
*/
class JfuConfigForm extends ConfigFormBase {
/**
* The jfu config service.
*
* @var Drupal\jfu\Services\JfuConfigServices
*/
protected $jfuConfig;
/**
* Constructs a form object for image dialog.
*
* @param Drupal\jfu\Services\JfuConfigServices $jfu_config_services
* The jfu config service.
*/
public function __construct(JfuConfigServices $jfu_config_services) {
$this->jfuConfig = $jfu_config_services;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('jfu_config.service')
);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
$attributes = $this->getRequest()->attributes->all();
return [
$this->jfuConfig->jfuConfigName($attributes['entity_type'], $attributes['bundle'], $attributes['field_name']),
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'jfu_config';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$attributes = $this->getRequest()->attributes->all();
$config_name = $this->jfuConfig->jfuConfigName($attributes['entity_type'], $attributes['bundle'], $attributes['field_name']);
$components = $this->jfuConfig->jfuListComponents();
$components_config = $this->jfuConfig->jfuComponents();
$jfu_config = $this->config($config_name);
$form['components'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Components list'),
'#options' => $components,
'#default_value' => !empty($jfu_config->get('components')) ? $jfu_config->get('components') : [],
'#required' => TRUE,
'#multiple' => TRUE,
];
$form['components_config'] = [
'#type' => 'fieldset',
'#title' => $this->t('Components configuration'),
];
foreach ($components_config as $key_config => $config) {
$default_value = $jfu_config->get('components_config');
$form['components_config'][$key_config] = [
'#type' => 'details',
'#title' => $this->t('Config ' . $key_config),
'#states' => [
'visible' => [
':input[value="' . $key_config . '"]' => ['checked' => TRUE],
],
],
];
// Components whitout image
if(isset($config['extensions'])) {
$form['components_config'][$key_config][$key_config . '_extensions'] = [
'#type' => 'textfield',
'#default_value' => !empty($default_value[$key_config]['extensions']) ? $default_value[$key_config]['extensions'] : $config['extensions'],
'#title' => $this->t('Allowed file extensions'),
'#description' => $this->t("Separate extensions with a comma or space. Each extension can contain alphanumeric characters, '.', and '_', and should start and end with an alphanumeric character."),
'#states' => [
'visible' => [
':input[value="' . $key_config . '"]' => ['checked' => TRUE],
],
],
];
}
if(isset($config['directory'])) {
$form['components_config'][$key_config][$key_config . '_directory'] = [
'#type' => 'textfield',
'#default_value' => !empty($default_value[$key_config]['directory']) ? $default_value[$key_config]['directory'] : $config['directory'],
'#title' => $this->t('Upload directory'),
'#description' => $this->t("A directory relative to Drupal's files directory where uploaded images will be stored."),
'#states' => [
'visible' => [
':input[value="' . $key_config . '"]' => ['checked' => TRUE],
],
],
];
}
if(isset($config['max_size'])) {
$default_max_size = \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.2.0', fn() => \Drupal\Core\StringTranslation\ByteSizeMarkup::create(Environment::getUploadMaxSize()), fn() => format_size(Environment::getUploadMaxSize()));
$form['components_config'][$key_config][$key_config . '_max_size'] = [
'#type' => 'textfield',
'#default_value' => !empty($default_value[$key_config]['max_size']) ? $default_value[$key_config]['max_size'] : $config['max_size'],
'#title' => $this->t('Maximum file size'),
'#description' => $this->t('If this is left empty, then the file size will be limited by the PHP maximum upload size of @size.', ['@size' => $default_max_size]),
'#maxlength' => 20,
'#size' => 10,
'#placeholder' => $default_max_size,
'#states' => [
'visible' => [
':input[value="' . $key_config . '"]' => ['checked' => TRUE],
],
],
];
}
if(isset($config['max_dimensions'])) {
$form['components_config'][$key_config][$key_config . '_max_dimensions'] = [
'#type' => 'item',
'#title' => $this->t('Maximum dimensions'),
'#description' => $this->t('Images larger than these dimensions will be scaled down.'),
'#states' => [
'visible' => [
':input[value="' . $key_config . '"]' => ['checked' => TRUE],
],
],
];
$form['components_config'][$key_config]['max_dimensions'][$key_config . '_width'] = [
'#title' => $this->t('Width'),
'#title_display' => 'invisible',
'#type' => 'number',
'#default_value' => !empty($default_value[$key_config]['max_dimensions']['width']) ? $default_value[$key_config]['max_dimensions']['width'] : $config['max_dimensions']['width'],
'#size' => 8,
'#maxlength' => 8,
'#min' => 1,
'#max' => 99999,
'#placeholder' => $this->t('width'),
'#field_suffix' => ' x ',
'#states' => [
'visible' => [
':input[value="' . $key_config . '"]' => ['checked' => TRUE],
],
],
'#prefix' => '<div class="form--inline clearfix">',
];
$form['components_config'][$key_config]['max_dimensions'][$key_config . '_height'] = [
'#title' => $this->t('Height'),
'#title_display' => 'invisible',
'#type' => 'number',
'#default_value' => !empty($default_value[$key_config]['max_dimensions']['height']) ? $default_value[$key_config]['max_dimensions']['height'] : $config['max_dimensions']['height'],
'#size' => 8,
'#maxlength' => 8,
'#min' => 1,
'#max' => 99999,
'#placeholder' => $this->t('height'),
'#field_suffix' => ' x ',
'#states' => [
'visible' => [
':input[value="' . $key_config . '"]' => ['checked' => TRUE],
],
],
'#suffix' => '</div>',
];
}
//Components whit responsive image.
if (isset($config['max_dimensions_responsive'])) {
$form['components_config'][$key_config]['max_dimensions_responsive'] = [
'#type' => 'item',
'#title' => t('Maximum dimensions responsive'),
'#description' => t('Images larger than these dimensions will be scaled down.'),
'#states' => [
'visible' => [
':input[value="' . $key_config . '"]' => ['checked' => TRUE],
],
],
];
$form['components_config'][$key_config]['max_dimensions_responsive'][$key_config . '_responsive_width'] = [
'#title' => t('Width'),
'#title_display' => 'invisible',
'#type' => 'number',
'#default_value' => !empty($default_value[$key_config]['max_dimensions_responsive']['width']) ? $default_value[$key_config]['max_dimensions_responsive']['width'] : $config['max_dimensions_responsive']['width'],
'#size' => 8,
'#maxlength' => 8,
'#min' => 1,
'#max' => 99999,
'#placeholder' => $this->t('width'),
'#field_suffix' => ' x ',
'#states' => [
'visible' => [
':input[value="' . $key_config . '"]' => ['checked' => TRUE],
],
],
'#prefix' => '<div class="form--inline clearfix">',
];
$form['components_config'][$key_config]['max_dimensions_responsive'][$key_config . '_responsive_height'] = [
'#title' => $this->t('Height'),
'#title_display' => 'invisible',
'#type' => 'number',
'#default_value' => !empty($default_value[$key_config]['max_dimensions_responsive']['height']) ? $default_value[$key_config]['max_dimensions_responsive']['height'] : $config['max_dimensions_responsive']['height'],
'#size' => 8,
'#maxlength' => 8,
'#min' => 1,
'#max' => 99999,
'#placeholder' => $this->t('height'),
'#field_suffix' => $this->t('pixels'),
'#states' => [
'visible' => [
':input[value="' . $key_config . '"]' => ['checked' => TRUE],
],
],
'#suffix' => '</div>',
];
}
if (isset($config['allow_classes'])) {
$form['components_config'][$key_config][$key_config . '_allow_classes'] = [
'#type' => 'checkbox',
'#title' => $this->t('Allows you to add classes to the components container'),
'#default_value' => !empty($default_value[$key_config]['allow_classes']) ? $default_value[$key_config]['allow_classes'] : $config['allow_classes'],
];
}
if (isset($config['allow_select_class'])) {
$form['components_config'][$key_config][$key_config . '_allow_select_class'] = [
'#type' => 'checkbox',
'#title' => $this->t('Allows defining custom classes for the component container'),
'#default_value' => !empty($default_value[$key_config]['allow_select_class']) ? $default_value[$key_config]['allow_select_class'] : $config['allow_select_class'],
];
}
if (isset($config['class_list'])) {
$form['components_config'][$key_config][$key_config . '_class_list'] = [
'#type' => 'textfield',
'#title' => $this->t('Add custom classes'),
'#description' => $this->t('Add classes separated by "," and without spaces. For example: class1,class-2,class_3.'),
'#default_value' => !empty($default_value[$key_config]['class_list']) ? $default_value[$key_config]['class_list'] : $config['class_list'],
'#states' => [
'visible' => [
':input[name="' . $key_config . '_allow_select_class"]' => ['checked' => TRUE],
],
],
];
}
if(isset($config['allow_group_items'])) {
$form['components_config'][$key_config][$key_config . '_allow_group_items'] = [
'#type' => 'checkbox',
'#title' => $this->t('Allows you to add group items to the components'),
'#default_value' => !empty($default_value[$key_config]['allow_group_items']) ? $default_value[$key_config]['allow_group_items'] : $config['allow_group_items'],
];
}
if(isset($config['module_name'])) {
$form['components_config'][$key_config][$key_config . '_module_name'] = [
'#value' => $config['module_name'],
'#type' => 'hidden',
];
}
}
$form['jfu_entity_type'] = [
'#value' => $attributes['entity_type'],
'#type' => 'hidden',
];
$form['jfu_bundle'] = [
'#value' => $attributes['bundle'],
'#type' => 'hidden',
];
$form['jfu_field_name'] = [
'#value' => $attributes['field_name'],
'#type' => 'hidden',
];
$form['actions']['save_config'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
'#submit' => [],
'#ajax' => [
'callback' => '::submitForm',
'event' => 'click',
],
];
$form['actions']['cancel_config'] = [
'#type' => 'button',
'#value' => $this->t('Cancel'),
'#ajax' => [
'callback' => '::submitCancel',
'event' => 'click',
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity_type = $form_state->getValue('jfu_entity_type');
$bundle = $form_state->getValue('jfu_bundle');
$field_name = $form_state->getValue('jfu_field_name');
$components = $form_state->getValue('components');
$config_name = $this->jfuConfig->jfuConfigName($entity_type, $bundle, $field_name);
$jfu_components = $this->jfuConfig->jfuComponents();
$components_config = [];
foreach ($jfu_components as $key_config => $config) {
if (isset($config['allow_classes'])) {
$components_config[$key_config]['allow_classes'] = $form_state->getValue($key_config . '_allow_classes');
}
if (isset($config['allow_select_class'])) {
$components_config[$key_config]['allow_select_class'] = $form_state->getValue($key_config . '_allow_select_class');
}
if (isset($config['class_list'])) {
$components_config[$key_config]['class_list'] = $form_state->getValue($key_config . '_class_list');
}
if (isset($config['allow_group_items'])) {
$components_config[$key_config]['allow_group_items'] = $form_state->getValue($key_config . '_allow_group_items');
}
if (isset($config['extensions'])) {
$components_config[$key_config]['extensions'] = $form_state->getValue($key_config . '_extensions');
}
if (isset($config['directory'])) {
$components_config[$key_config]['directory'] = $form_state->getValue($key_config . '_directory');
}
if (isset($config['max_size'])) {
$components_config[$key_config]['max_size'] = $form_state->getValue($key_config . '_max_size');
}
if (isset($config['max_dimensions'])) {
$components_config[$key_config]['max_dimensions']['width'] = $form_state->getValue($key_config . '_width');
$components_config[$key_config]['max_dimensions']['height'] = $form_state->getValue($key_config . '_height');
}
if (isset($config['max_dimensions_responsive'])) {
$components_config[$key_config]['max_dimensions_responsive']['width'] = $form_state->getValue($key_config . '_responsive_width');
$components_config[$key_config]['max_dimensions_responsive']['height'] = $form_state->getValue($key_config . '_responsive_height');
}
if (isset($config['module_name'])) {
$components_config[$key_config]['module_name'] = $form_state->getValue($key_config . '_module_name');
}
}
$this->config($config_name)->set('components', $components)->save();
$this->config($config_name)->set('components_config', $components_config)->save();
$response = new AjaxResponse();
$response->addCommand(new CloseModalDialogCommand());
return $response;
}
/**
* {@inheritdoc}
*/
public function submitCancel(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response->addCommand(new CloseModalDialogCommand());
return $response;
}
}
