View source
<?php
namespace Drupal\bootstrap_styles;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
trait ResponsiveTrait {
use HelperTrait;
protected function getBreakpoints() {
return [
'desktop' => $this
->t('Desktop'),
'laptop' => $this
->t('Laptop'),
'tablet' => $this
->t('Tablet'),
'mobile' => $this
->t('Mobile'),
];
}
protected function buildResponsivePreviewer(array &$form) {
$icon_path = drupal_get_path('module', 'bootstrap_styles') . '/images/';
$form['bs_responsive'] = [
'#type' => 'radios',
'#options' => [
'all' => $this
->getSvgIconMarkup($icon_path . 'responsive/device-all.svg'),
],
'#title' => $this
->t('Responsive'),
'#title_display' => 'invisible',
'#default_value' => 'all',
'#validated' => TRUE,
'#attributes' => [
'class' => [
'bs_col--full',
'bs_responsive',
'bs_responsive_bar',
],
],
'#disable_live_preview' => TRUE,
];
foreach ($this
->getBreakpoints() as $breakpoint_key => $breakpoint_value) {
$form['bs_responsive']['#options'][$breakpoint_key] = $this
->getSvgIconMarkup($icon_path . 'responsive/device-' . $breakpoint_key . '.svg');
}
$form['#attached']['library'][] = 'bootstrap_styles/bs_responsive';
}
protected function buildBreakpointsFields(array &$form, $group_name) {
$icon_path = drupal_get_path('module', 'bootstrap_styles') . '/images/';
$form['bs_responsive_' . $group_name] = [
'#type' => 'radios',
'#options' => [
'all' => $this
->getSvgIconMarkup($icon_path . 'responsive/device-all.svg'),
],
'#title' => $this
->t('Responsive'),
'#title_display' => 'invisible',
'#default_value' => 'all',
'#validated' => TRUE,
'#attributes' => [
'class' => [
'bs_col--full',
'bs_responsive',
'bs_responsive_' . $group_name,
],
],
'#disable_live_preview' => TRUE,
];
foreach ($this
->getBreakpoints() as $breakpoint_key => $breakpoint_value) {
$form['bs_responsive_' . $group_name]['#options'][$breakpoint_key] = $this
->getSvgIconMarkup($icon_path . 'responsive/device-' . $breakpoint_key . '.svg');
}
}
protected function getBreakpointsKeys() {
return array_keys($this
->getBreakpoints());
}
protected function getBreakpointTitle($breakpoint_key) {
return $this
->getBreakpoints()[$breakpoint_key];
}
protected function createBreakpointFormField(array &$form, string $breakpoint_key, string $field_name, array $original_parents, array $new_parents = []) {
$field = NestedArray::getValue($form, array_merge($original_parents, [
$field_name,
]));
$field['#title'] .= ' - ' . $this
->getBreakpointTitle($breakpoint_key);
$field['#default_value'] = $this
->config()
->get($field_name . '_' . $breakpoint_key);
$target_parents = $original_parents;
if ($new_parents) {
$target_parents = $new_parents;
}
NestedArray::setValue($form, array_merge($target_parents, [
$field_name . '_' . $breakpoint_key,
]), $field);
}
protected function buildBreakpointsConfigurationForm(array &$form, array $fields) {
foreach ($fields as $field_name => $parents) {
foreach ($this
->getBreakpoints() as $breakpoint_key => $breakpoint_value) {
$field = NestedArray::getValue($form, array_merge($parents, [
$field_name,
]));
$field['#title'] .= ' - ' . $breakpoint_value;
$field['#default_value'] = $this
->config()
->get($field_name . '_' . $breakpoint_key);
NestedArray::setValue($form, array_merge($parents, [
$field_name . '_' . $breakpoint_key,
]), $field);
}
}
}
protected function submitBreakpointsConfigurationForm(FormStateInterface $form_state, array $fields) {
foreach ($fields as $field_name) {
foreach ($this
->getBreakpointsKeys() as $breakpoint) {
$this
->config()
->set($field_name . '_' . $breakpoint, $form_state
->getValue($field_name . '_' . $breakpoint))
->save();
}
}
}
protected function createBreakpointStyleFormField(array &$form, string $field_name, string $breakpoint_key, string $group_id, array $storage, string $storage_key = NULL) {
$form[$field_name . '_' . $breakpoint_key] = $form[$field_name];
$form[$field_name . '_' . $breakpoint_key]['#options'] = $this
->getStyleOptions($storage_key . '_' . $breakpoint_key);
$form[$field_name . '_' . $breakpoint_key]['#default_value'] = $storage[$field_name . '_' . $breakpoint_key]['class'] ?? NULL;
$form[$field_name . '_' . $breakpoint_key]['#validated'] = TRUE;
$form[$field_name . '_' . $breakpoint_key]['#states']['visible'][':input.bs_responsive_' . $group_id]['value'] = $breakpoint_key;
$form[$field_name]['#states']['visible'][':input.bs_responsive_' . $group_id]['value'] = 'all';
}
protected function createBreakpointsStyleFormFields(array &$form, string $field_name, string $group_id, array $storage, string $storage_key = NULL) {
foreach ($this
->getBreakpointsKeys() as $breakpoint_key) {
$this
->createBreakpointStyleFormField($form, $field_name, $breakpoint_key, $group_id, $storage, $storage_key);
}
}
protected function createBreakpointStyleFormClassIndexBasedField(array &$form, string $field_name, string $breakpoint_key, string $group_id, array $storage, string $storage_key = NULL) {
$breakpoint_default_value = 0;
if (isset($storage[$field_name . '_' . $breakpoint_key]['class'])) {
$breakpoint_default_value = $this
->getStyleOptionIndexByClass($field_name . '_' . $breakpoint_key, $storage[$field_name . '_' . $breakpoint_key]['class']);
}
$form[$field_name . '_' . $breakpoint_key] = $form[$field_name];
$form[$field_name . '_' . $breakpoint_key]['#options'] = $this
->getStyleOptions($field_name . '_' . $breakpoint_key);
$form[$field_name . '_' . $breakpoint_key]['#max'] = $this
->getStyleOptionsCount($field_name . '_' . $breakpoint_key);
$form[$field_name . '_' . $breakpoint_key]['#default_value'] = $breakpoint_default_value;
$form[$field_name . '_' . $breakpoint_key]['#validated'] = TRUE;
if (isset($form[$field_name . '_' . $breakpoint_key]['#attributes']['class'])) {
foreach ($form[$field_name . '_' . $breakpoint_key]['#attributes']['class'] as $key => $value) {
$form[$field_name . '_' . $breakpoint_key]['#attributes']['class'][$key] = $value . '-' . $breakpoint_key;
}
}
$form[$field_name . '_' . $breakpoint_key]['#states']['visible'][':input.bs_responsive_' . $group_id]['value'] = $breakpoint_key;
$form[$field_name]['#states']['visible'][':input.bs_responsive_' . $group_id]['value'] = 'all';
}
protected function createBreakpointsStyleFormClassIndexBasedFields(array &$form, string $field_name, string $group_id, array $storage, string $storage_key = NULL) {
foreach ($this
->getBreakpointsKeys() as $breakpoint_key) {
$this
->createBreakpointStyleFormClassIndexBasedField($form, $field_name, $breakpoint_key, $group_id, $storage, $storage_key);
}
}
protected function buildBreakpointsStyleFormElements(array &$form, array $fields, array $storage, string $group_id) {
foreach ($fields as $field_name => $style_options_key) {
foreach ($this
->getBreakpointsKeys() as $breakpoint) {
$form[$field_name . '_' . $breakpoint] = $form[$field_name];
$form[$field_name . '_' . $breakpoint]['#options'] = $this
->getStyleOptions($style_options_key . '_' . $breakpoint);
$form[$field_name . '_' . $breakpoint]['#default_value'] = $storage[$field_name . '_' . $breakpoint]['class'] ?? NULL;
$form[$field_name . '_' . $breakpoint]['#states']['visible'][':input.bs_responsive_' . $group_id]['value'] = $breakpoint;
$form[$field_name]['#states']['visible'][':input.bs_responsive_' . $group_id]['value'] = 'all';
}
}
}
protected function saveBreakpointsStyleFormFields(array $group_elements, array &$storage, array $fields) {
foreach ($fields as $field_name) {
foreach ($this
->getBreakpointsKeys() as $breakpoint) {
$storage[$field_name . '_' . $breakpoint] = [
'class' => $group_elements[$field_name . '_' . $breakpoint],
];
}
}
}
protected function saveBreakpointsStyleFormClassIndexBasedFields(array $group_elements, array &$storage, array $fields) {
foreach ($fields as $field_name) {
foreach ($this
->getBreakpointsKeys() as $breakpoint) {
$storage[$field_name . '_' . $breakpoint] = [
'class' => $this
->getStyleOptionClassByIndex($field_name . '_' . $breakpoint, $group_elements[$field_name . '_' . $breakpoint]),
];
}
}
}
protected function buildBreakpoints(array &$classes, array $storage, array $fields) {
foreach ($fields as $field_name) {
foreach ($this
->getBreakpointsKeys() as $breakpoint) {
if (isset($storage[$field_name . '_' . $breakpoint]['class'])) {
$classes[] = $storage[$field_name . '_' . $breakpoint]['class'];
}
}
}
}
}