You are here

public function Margin::buildStyleFormElements in Bootstrap Styles 1.0.x

Overrides StylePluginBase::buildStyleFormElements

File

src/Plugin/BootstrapStyles/Style/Margin.php, line 91

Class

Margin
Class Margin.

Namespace

Drupal\bootstrap_styles\Plugin\BootstrapStyles\Style

Code

public function buildStyleFormElements(array &$form, FormStateInterface $form_state, $storage) {
  $directions = [
    'left',
    'top',
    'right',
    'bottom',
  ];

  // This only for frontend no storage needed for this field.
  $form['margin_type'] = [
    '#type' => 'radios',
    '#options' => [
      'margin' => $this
        ->t('Margin') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
        ->t('All') . '</div>',
      'margin_left' => $this
        ->t('Left') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
        ->t('Left') . '</div>',
      'margin_top' => $this
        ->t('Top') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
        ->t('Top') . '</div>',
      'margin_right' => $this
        ->t('Right') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
        ->t('Right') . '</div>',
      'margin_bottom' => $this
        ->t('Bottom') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
        ->t('Bottom') . '</div>',
    ],
    '#title' => $this
      ->t('margin type'),
    '#title_display' => 'invisible',
    '#default_value' => 'margin',
    '#validated' => TRUE,
    '#attributes' => [
      'class' => [
        'bs_col--full',
        'bs_input-boxes',
        'bs_input-boxes--box-model',
        'bs_margin--type',
      ],
    ],
    '#disable_live_preview' => TRUE,
  ];
  $default_value = 0;
  if (isset($storage['margin']['class'])) {
    $default_value = $this
      ->getStyleOptionIndexByClass('margin', $storage['margin']['class']);
  }
  $form['margin'] = [
    '#type' => 'range',
    '#title' => $this
      ->t('Margin'),
    '#min' => 0,
    '#max' => $this
      ->getStyleOptionsCount('margin'),
    '#step' => 1,
    '#default_value' => $default_value,
    '#attributes' => [
      'class' => [
        'bs-field-margin',
      ],
    ],
    '#states' => [
      'visible' => [
        ':input.bs_margin--type' => [
          'value' => 'margin',
        ],
      ],
    ],
  ];

  // Loop through the directions.
  for ($i = 0; $i < 4; $i++) {
    $default_value = 0;
    if (isset($storage['margin_' . $directions[$i]]['class'])) {
      $default_value = $this
        ->getStyleOptionIndexByClass('margin_' . $directions[$i], $storage['margin_' . $directions[$i]]['class']);
    }
    $form['margin_' . $directions[$i]] = [
      '#type' => 'range',
      '#title' => $this
        ->t('Margin @direction', [
        '@direction' => $directions[$i],
      ]),
      '#min' => 0,
      '#max' => $this
        ->getStyleOptionsCount('margin_' . $directions[$i]),
      '#step' => 1,
      '#default_value' => $default_value,
      '#attributes' => [
        'class' => [
          'bs-field-margin-' . $directions[$i],
        ],
      ],
      '#states' => [
        'visible' => [
          ':input.bs_margin--type' => [
            'value' => 'margin_' . $directions[$i],
          ],
        ],
      ],
    ];
  }

  // Pass margin options to drupal settings.
  $margin_options = [];
  $margin_options['margin'] = array_keys($this
    ->getStyleOptions('margin'));
  for ($i = 0; $i < 4; $i++) {
    $margin_options['margin_' . $directions[$i]] = array_keys($this
      ->getStyleOptions('margin_' . $directions[$i]));
  }
  $form['#attached']['drupalSettings']['bootstrap_styles']['spacing']['margin_classes_options'] = $margin_options;

  // Attach the Layout Builder form style for this plugin.
  $form['#attached']['library'][] = 'bootstrap_styles/plugin.margin.layout_builder_form';
  return $form;
}