View source
<?php
namespace Drupal\ckeditor_bs_grid\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\Markup;
class Settings extends ConfigFormBase {
const CONFIG_NAME = 'ckeditor_bs_grid.settings';
protected function getEditableConfigNames() {
return [
self::CONFIG_NAME,
];
}
public function getFormId() {
return 'ckeditor_bs_grid.settings';
}
protected function getBreakpoints() {
$breakpoints = [
'xs' => [
'bs_label' => $this
->t('Extra Smaill (xs)'),
'prefix' => 'none',
],
'sm' => [
'bs_label' => $this
->t('Small (sm)'),
'prefix' => 'sm',
],
'md' => [
'bs_label' => $this
->t('Medium (md)'),
'prefix' => 'md',
],
'lg' => [
'bs_label' => $this
->t('Large (lg)'),
'prefix' => 'lg',
],
'xl' => [
'bs_label' => $this
->t('Extra large (xl)'),
'prefix' => 'xl',
],
'xxl' => [
'bs_label' => $this
->t('Extra extra large (xxl)'),
'prefix' => 'xxl',
],
];
return $breakpoints;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$breakpoints = $this
->getBreakpoints();
$config = $this
->configFactory()
->get(self::CONFIG_NAME)
->get('breakpoints');
$message = $this
->t('Breakpoints and Number of columns can be enabled/disabled per text format in the @link.', [
'@link' => Link::createFromRoute('editor settings page', 'filter.admin_overview')
->toString(),
]);
$form['prefix'] = [
'#type' => 'markup',
'#markup' => Markup::create("<div class='messages messages--warning'>" . $message . "</div>"),
'#weight' => -100,
];
$cols = [];
for ($i = 1; $i <= 12; $i++) {
$cols[$i] = $i;
}
$form['#tree'] = TRUE;
foreach ($breakpoints as $break => $data) {
$form[$break] = [
'#title' => $data['bs_label'],
'#type' => 'details',
'#open' => FALSE,
];
$form[$break]['label'] = [
'#title' => $this
->t('Label'),
'#type' => 'textfield',
'#default_value' => $config[$break]['label'],
'#required' => TRUE,
];
$form[$break]['prefix'] = [
'#title' => $this
->t('Prefix'),
'#type' => 'textfield',
'#default_value' => $data['prefix'],
'#disabled' => TRUE,
'#required' => TRUE,
];
$form[$break]['columns'] = [
'#title' => $this
->t('Available Column Layouts'),
'#type' => 'details',
'#description' => $this
->t('In the format of key|value, where the key is the attribute to add to each column, separated by a comma. Special tags are "auto" and "equal"'),
];
$options = [
'equal' => $this
->t('Equal'),
'auto' => $this
->t('Auto'),
] + $cols;
foreach ($cols as $col) {
$form[$break]['columns'][$col]['layouts'] = [
'#title' => $this
->t('@num Column Layout', [
'@num' => $col,
]),
'#type' => 'details',
'#prefix' => '<div id="fieldset-wrapper-' . $break . '-' . $col . '">',
'#suffix' => '</div>',
'#open' => FALSE,
];
$num_layouts = $form_state
->get('num_ ' . $break . '_' . $col);
if ($num_layouts === NULL) {
$num = count($config[$break]['columns'][$col]['layouts']);
$form_state
->set('num_ ' . $break . '_' . $col, $num);
$num_layouts = $num;
}
for ($i = 0; $i < $num_layouts; $i++) {
$form[$break]['columns'][$col]['layouts']['option_' . $i]['label'] = [
'#title' => $this
->t('Label'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => $config[$break]['columns'][$col]['layouts']['option_' . $i]['label'] ?? $this
->t('Equal Width'),
];
$form[$break]['columns'][$col]['layouts']['option_' . $i]['settings'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'container-inline',
],
],
];
for ($j = 1; $j <= $col; $j++) {
$form[$break]['columns'][$col]['layouts']['option_' . $i]['settings']['col-' . $j] = [
'#title' => $j,
'#type' => 'select',
'#options' => $options,
'#default_value' => $config[$break]['columns'][$col]['layouts']['option_' . $i]['settings']['col-' . $j],
];
}
if ($num_layouts > 1) {
$form[$break]['columns'][$col]['layouts']['option_' . $i]['settings']['remove'] = [
'#type' => 'submit',
'#value' => $this
->t('Remove'),
'#submit' => [
'::removeCallback',
],
'#ajax' => [
'callback' => '::ajaxCallback',
'wrapper' => 'fieldset-wrapper-' . $break . '-' . $col,
],
'#name' => 'remove-' . $break . '-' . $col,
];
}
}
$form[$break]['columns'][$col]['layouts']['actions'] = [
'#type' => 'actions',
];
$form[$break]['columns'][$col]['layouts']['add_name'] = [
'#type' => 'submit',
'#value' => $this
->t('Add More'),
'#submit' => [
'::addOne',
],
'#ajax' => [
'callback' => '::ajaxCallback',
'wrapper' => 'fieldset-wrapper-' . $break . '-' . $col,
],
'#name' => 'add-' . $break . '-' . $col,
];
}
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$values = $form_state
->cleanValues()
->getValues();
unset($values['actions']);
$this
->configFactory()
->getEditable(self::CONFIG_NAME)
->set('breakpoints', $values)
->save();
}
public function ajaxCallback(array &$form, FormStateInterface $form_state) {
$trigger = $this
->getTriggerKey($form_state);
$form[$trigger['break']]['columns'][$trigger['col']]['layouts']['#open'] = TRUE;
return $form[$trigger['break']]['columns'][$trigger['col']]['layouts'];
}
public function addOne(array &$form, FormStateInterface $form_state) {
$trigger = $this
->getTriggerKey($form_state);
$value_key = 'num_ ' . $trigger['break'] . '_' . $trigger['col'];
$num_layouts = $form_state
->get($value_key);
$form_state
->set($value_key, $num_layouts + 1);
$form_state
->setRebuild();
}
public function removeCallback(array &$form, FormStateInterface $form_state) {
$trigger = $this
->getTriggerKey($form_state);
$value_key = 'num_ ' . $trigger['break'] . '_' . $trigger['col'];
$num_layouts = $form_state
->get($value_key);
$form_state
->set($value_key, $num_layouts - 1);
$form_state
->setRebuild();
}
protected function getTriggerKey(FormStateInterface $form_state) {
$trigger = $form_state
->getTriggeringElement();
$parts = explode('-', $trigger['#name']);
return [
'break' => $parts[1],
'col' => $parts[2],
];
}
}