diff --git a/src/Form/UbcCkeditorWidgetsSettingsForm.php b/src/Form/UbcCkeditorWidgetsSettingsForm.php index 1089531..d8967e8 100644 --- a/src/Form/UbcCkeditorWidgetsSettingsForm.php +++ b/src/Form/UbcCkeditorWidgetsSettingsForm.php @@ -12,118 +12,135 @@ * * @package Drupal\ubc_ckeditor_widgets\Form */ -class UbcCkeditorWidgetsSettingsForm extends ConfigFormBase { - - /** - * {@inheritdoc} - */ - public function getFormId() { - return 'ubc_ckeditor_widgets_settings_form'; - } - - /** - * {@inheritdoc} - */ - public function getEditableConfigNames() { - return ['ubc_ckeditor_widgets.settings']; - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, FormStateInterface $form_state) { - $config = $this->config('ubc_ckeditor_widgets.settings'); - - $form['background_colors'] = [ - '#type' => 'textarea', - '#title' => $this->t('Add a set of background color classes'), - '#default_value' => $config->get('background_colors'), - '#description' => $this->t('These classes will be applied as background colors.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of bg-, so format as bg-[myvalue].') - ]; - - $form['padding_styles'] = [ - '#type' => 'textarea', - '#title' => $this->t('Add a set of padding classes'), - '#default_value' => $config->get('padding_styles'), - '#description' => $this->t('These classes are intended to be added for adjusting the padding inside of the element.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of p-, so format as p-[myvalue].') - ]; - - $form['margin_styles'] = [ - '#type' => 'textarea', - '#title' => $this->t('Add a set of margin classes'), - '#default_value' => $config->get('margin_styles'), - '#description' => $this->t('These classes are intended to be added for adjusting the vertical margin outside of the element.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of my-, so format as my-[myvalue].') - ]; - - $form['gap_styles'] = [ - '#type' => 'textarea', - '#title' => $this->t('Add a set of classes for changing the gap property'), - '#default_value' => $config->get('gap_styles'), - '#description' => $this->t('These classes are intended to be added for adjusting the gap between elements (ie. columns, cards).
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of gap-, so format as gap-[myvalue].') - ]; - - $form['table_styles'] = [ - '#type' => 'textarea', - '#title' => $this->t('Add a set of classes for adjusting the style of tables'), - '#default_value' => $config->get('table_styles'), - '#description' => $this->t('These classes are intended to be added for adjusting the style of tables.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of table--, so format as table--[myvalue].') - ]; - - $form['three_column_layout_styles'] = [ - '#type' => 'textarea', - '#title' => $this->t('Add a set of classes for adjusting the layout of Three Columns'), - '#default_value' => $config->get('three_column_layout_styles'), - '#description' => $this->t('These classes are intended to be added for adjusting the layout of the three column widget.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of align-, so format as align-[myvalue].') - ]; - - $form['two_column_layout_styles'] = [ - '#type' => 'textarea', - '#title' => $this->t('Add a set of classes for adjusting the layout of Two Columns'), - '#default_value' => $config->get('two_column_layout_styles'), - '#description' => $this->t('These classes are intended to be added for adjusting the layout of the two column widget.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of align-, so format as align-[myvalue].') - ]; - - $form['width_styles'] = [ - '#type' => 'textarea', - '#title' => $this->t('Add a set of classes for adjusting the width of Table Columns'), - '#default_value' => $config->get('width_styles'), - '#description' => $this->t('These classes are intended to be added for adjusting the widths of tables.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of w-, so format as w-[myvalue].') - ]; - - return parent::buildForm($form, $form_state); - } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, FormStateInterface $form_state) { - $config = $this->config('ubc_ckeditor_widgets.settings'); - $values = $form_state->getValues(); - $config->set('background_colors', $values['background_colors']); - $config->set('gap_styles', $values['gap_styles']); - $config->set('margin_styles', $values['margin_styles']); - $config->set('padding_styles', $values['padding_styles']); - $config->set('table_styles', $values['table_styles']); - $config->set('three_column_layout_styles', $values['three_column_layout_styles']); - $config->set('two_column_layout_styles', $values['two_column_layout_styles']); - $config->set('width_styles', $values['width_styles']); - $config->save(); - - parent::submitForm($form, $form_state); - } +class UbcCkeditorWidgetsSettingsForm extends ConfigFormBase +{ + + /** + * {@inheritdoc} + */ + public function getFormId() + { + return 'ubc_ckeditor_widgets_settings_form'; + } + + /** + * {@inheritdoc} + */ + public function getEditableConfigNames() + { + return ['ubc_ckeditor_widgets.settings']; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) + { + $config = $this->config('ubc_ckeditor_widgets.settings'); + + $form['background_colors'] = [ + '#type' => 'textarea', + '#title' => $this->t('Add a set of background color classes'), + '#default_value' => $config->get('background_colors'), + '#description' => $this->t('These classes will be applied as background colors.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of bg-, so format as bg-[myvalue].') + ]; + + $form['padding_styles'] = [ + '#type' => 'textarea', + '#title' => $this->t('Add a set of padding classes'), + '#default_value' => $config->get('padding_styles'), + '#description' => $this->t('These classes are intended to be added for adjusting the padding inside of the element.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of p-, so format as p-[myvalue].') + ]; + + $form['margin_styles'] = [ + '#type' => 'textarea', + '#title' => $this->t('Add a set of margin classes'), + '#default_value' => $config->get('margin_styles'), + '#description' => $this->t('These classes are intended to be added for adjusting the vertical margin outside of the element.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of my-, so format as my-[myvalue].') + ]; + + $form['gap_styles'] = [ + '#type' => 'textarea', + '#title' => $this->t('Add a set of classes for changing the gap property'), + '#default_value' => $config->get('gap_styles'), + '#description' => $this->t('These classes are intended to be added for adjusting the gap between elements (ie. columns, cards).
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of gap-, so format as gap-[myvalue].') + ]; + + $form['table_styles'] = [ + '#type' => 'textarea', + '#title' => $this->t('Add a set of classes for adjusting the style of tables'), + '#default_value' => $config->get('table_styles'), + '#description' => $this->t('These classes are intended to be added for adjusting the style of tables.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of table--, so format as table--[myvalue].') + ]; + + $form['three_column_layout_styles'] = [ + '#type' => 'textarea', + '#title' => $this->t('Add a set of classes for adjusting the layout of Three Columns'), + '#default_value' => $config->get('three_column_layout_styles'), + '#description' => $this->t('These classes are intended to be added for adjusting the layout of the three column widget.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of align-, so format as align-[myvalue].') + ]; + + $form['two_column_layout_styles'] = [ + '#type' => 'textarea', + '#title' => $this->t('Add a set of classes for adjusting the layout of Two Columns'), + '#default_value' => $config->get('two_column_layout_styles'), + '#description' => $this->t('These classes are intended to be added for adjusting the layout of the two column widget.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of align-, so format as align-[myvalue].') + ]; + + $form['width_styles'] = [ + '#type' => 'textarea', + '#title' => $this->t('Add a set of classes for adjusting the width of Table Columns'), + '#default_value' => $config->get('width_styles'), + '#description' => $this->t('These classes are intended to be added for adjusting the widths of tables.
Enter one class on each line in the format: class|Label
* Note: this expects a prefix of w-, so format as w-[myvalue].') + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) + { + $config = $this->config('ubc_ckeditor_widgets.settings'); + $values = $form_state->getValues(); + $config->set('background_colors', $values['background_colors']); + $config->set('gap_styles', $values['gap_styles']); + $config->set('margin_styles', $values['margin_styles']); + $config->set('padding_styles', $values['padding_styles']); + $config->set('table_styles', $values['table_styles']); + $config->set('three_column_layout_styles', $values['three_column_layout_styles']); + $config->set('two_column_layout_styles', $values['two_column_layout_styles']); + $config->set('width_styles', $values['width_styles']); + $config->save(); + + parent::submitForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) + { + foreach ( + ['background_colors', 'gap_styles', 'margin_styles', 'padding_styles', 'table_styles', 'three_column_layout_styles', 'two_column_layout_styles', 'width_styles'] as $field_name) { + $value = $form_state->getValue($field_name); + if ($value) { + // Validate that there aren't two newlines. + if (preg_match('/\n\s*\n/', $value)) { + $form_state->setErrorByName($field_name, $this->t('The input for %field cannot contain double line breaks.', ['%field' => $form[$field_name]['#title']])); + } + + $pairs = explode("\n", $value); + foreach ($pairs as $pair) { + list($class, $key) = explode('|', $pair, 2); + // Validate the class. + if (!preg_match('/^[a-zA-Z][a-zA-Z0-9\-_]*$/', trim($class))) { + $form_state->setErrorByName($field_name, $this->t('The class "%class" is not valid. It must be a valid CSS class name without a leading dot.', ['%class' => $class])); + } + } + } + } + } } - -/** not sure how to best capture these - * refer to ubc_ckeditor_widgets.module for values - * - * defaultCardHorizontalStyles - * defaultCardVerticalOneStyles - * defaultCardVerticalTwoStyles - * defaultCardVerticalThreeStyles - * defaultColorBoxStyles - * defaultColumnsTwoStyles - * defaultColumnsThreeStyles - * defaultColumnsFourStyles - * - */