You are here

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

Same name in this branch
  1. 1.0.x src/Plugin/BootstrapStyles/Style/Border.php \Drupal\bootstrap_styles\Plugin\BootstrapStyles\Style\Border::buildStyleFormElements()
  2. 1.0.x src/Plugin/BootstrapStyles/StylesGroup/Border.php \Drupal\bootstrap_styles\Plugin\BootstrapStyles\StylesGroup\Border::buildStyleFormElements()

Overrides StylePluginBase::buildStyleFormElements

File

src/Plugin/BootstrapStyles/Style/Border.php, line 227

Class

Border
Class Border.

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['border_type'] = [
    '#type' => 'radios',
    '#options' => [
      'border' => $this
        ->t('Border') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
        ->t('All') . '</div>',
      'border_left' => $this
        ->t('Left') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
        ->t('Left') . '</div>',
      'border_top' => $this
        ->t('Top') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
        ->t('Top') . '</div>',
      'border_right' => $this
        ->t('Right') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
        ->t('Right') . '</div>',
      'border_bottom' => $this
        ->t('Bottom') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
        ->t('Bottom') . '</div>',
    ],
    '#title' => $this
      ->t('Border type'),
    '#title_display' => 'invisible',
    '#default_value' => 'border',
    '#validated' => TRUE,
    '#attributes' => [
      'class' => [
        'bs_col--full',
        'bs_input-boxes',
        'bs_input-boxes--box-model',
        'bs_border--type',
      ],
    ],
    '#disable_live_preview' => TRUE,
  ];
  $form['border_style'] = [
    '#type' => 'radios',
    '#options' => $this
      ->getStyleOptions('border_style'),
    '#title' => $this
      ->t('Border style'),
    '#default_value' => $storage['border']['border_style']['class'] ?? NULL,
    '#validated' => TRUE,
    '#attributes' => [
      'class' => [
        'bs-field-border-style',
        'bs_input-circles',
      ],
    ],
    '#states' => [
      'visible' => [
        ':input.bs_border--type' => [
          'value' => 'border',
        ],
      ],
    ],
  ];
  $default_value = 0;
  if (isset($storage['border']['border_width']['class'])) {
    $default_value = $this
      ->getStyleOptionIndexByClass('border_width', $storage['border']['border_width']['class']);
  }
  $form['border_width'] = [
    '#title' => $this
      ->t('Border width'),
    '#type' => 'range',
    '#min' => 0,
    '#max' => $this
      ->getStyleOptionsCount('border_width'),
    '#step' => 1,
    '#default_value' => $default_value,
    '#validated' => TRUE,
    '#attributes' => [
      'class' => [
        'bs-field-border-width',
      ],
    ],
    '#states' => [
      'visible' => [
        ':input.bs_border--type' => [
          'value' => 'border',
        ],
      ],
    ],
  ];
  $form['border_color'] = [
    '#type' => 'radios',
    '#options' => $this
      ->getStyleOptions('border_color'),
    '#title' => $this
      ->t('Border color'),
    '#default_value' => $storage['border']['border_color']['class'] ?? NULL,
    '#validated' => TRUE,
    '#attributes' => [
      'class' => [
        'bs-field-border-color',
        'bs_input-circles',
      ],
    ],
    '#states' => [
      'visible' => [
        ':input.bs_border--type' => [
          'value' => 'border',
        ],
      ],
    ],
  ];
  for ($i = 0; $i < 4; $i++) {
    $form['border_' . $directions[$i] . '_style'] = [
      '#type' => 'radios',
      '#options' => $this
        ->getStyleOptions('border_' . $directions[$i] . '_style'),
      '#title' => $this
        ->t('Border style'),
      '#default_value' => $storage['border']['border_' . $directions[$i] . '_style']['class'] ?? NULL,
      '#validated' => TRUE,
      '#attributes' => [
        'class' => [
          'bs-field-border-style-' . $directions[$i],
          'bs_input-circles',
        ],
      ],
      '#states' => [
        'visible' => [
          ':input.bs_border--type' => [
            'value' => 'border_' . $directions[$i],
          ],
        ],
      ],
    ];
    $default_value = 0;
    if (isset($storage['border']['border_' . $directions[$i] . '_width']['class'])) {
      $default_value = $this
        ->getStyleOptionIndexByClass('border_' . $directions[$i] . '_width', $storage['border']['border_' . $directions[$i] . '_width']['class']);
    }
    $form['border_' . $directions[$i] . '_width'] = [
      '#type' => 'range',
      '#title' => $this
        ->t('Border @direction width', [
        '@direction' => $directions[$i],
      ]),
      '#min' => 0,
      '#max' => $this
        ->getStyleOptionsCount('border_' . $directions[$i] . '_width'),
      '#step' => 1,
      '#default_value' => $default_value,
      '#validated' => TRUE,
      '#attributes' => [
        'class' => [
          'bs-field-border-width-' . $directions[$i],
        ],
      ],
      '#states' => [
        'visible' => [
          ':input.bs_border--type' => [
            'value' => 'border_' . $directions[$i],
          ],
        ],
      ],
    ];
    $form['border_' . $directions[$i] . '_color'] = [
      '#type' => 'radios',
      '#options' => $this
        ->getStyleOptions('border_' . $directions[$i] . '_color'),
      '#title' => $this
        ->t('Border color'),
      '#default_value' => $storage['border']['border_' . $directions[$i] . '_color']['class'] ?? NULL,
      '#validated' => TRUE,
      '#attributes' => [
        'class' => [
          'bs-field-border-color-' . $directions[$i],
          'bs_input-circles',
        ],
      ],
      '#states' => [
        'visible' => [
          ':input.bs_border--type' => [
            'value' => 'border_' . $directions[$i],
          ],
        ],
      ],
    ];
  }

  // Rounded Corners.
  $corners = [
    'top_left' => 'Top Left',
    'top_right' => 'Top Right',
    'bottom_left' => 'Bottom Left',
    'bottom_right' => 'Bottom Right',
  ];
  $form['rounded_corners_description'] = [
    '#type' => 'inline_template',
    '#template' => "<strong>{% trans %}Border Radius <small>(Round Corners)</small>{% endtrans %}</strong>",
    '#prefix' => '<hr class="bs_divider"/>',
  ];
  $default_value = 0;
  if (isset($storage['border']['rounded_corners']['class'])) {
    $default_value = $this
      ->getStyleOptionIndexByClass('rounded_corners', $storage['border']['rounded_corners']['class']);
  }
  $icon_path = drupal_get_path('module', 'bootstrap_styles') . '/images/';
  $form['rounded_corners'] = [
    '#type' => 'range',
    '#title' => '<span class="sr-only">' . $this
      ->t('Corners') . '</span><div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
      ->t('All Corners') . '</div>',
    '#min' => 0,
    '#max' => $this
      ->getStyleOptionsCount('rounded_corners'),
    '#step' => 1,
    '#default_value' => $default_value,
    '#validated' => TRUE,
    '#attributes' => [
      'class' => [
        'bs-field-rounded-corners',
      ],
    ],
    '#description' => $this
      ->getSvgIconMarkup($icon_path . 'plugins/border/border-radius.svg'),
  ];
  foreach ($corners as $corner_key => $corner_value) {
    $default_value = 0;
    if (isset($storage['border']['rounded_corner_' . $corner_key]['class'])) {
      $default_value = $this
        ->getStyleOptionIndexByClass('rounded_corner_' . $corner_key, $storage['border']['rounded_corner_' . $corner_key]['class']);
    }
    $form['rounded_corner_' . $corner_key] = [
      '#type' => 'range',
      '#title' => '<span class="sr-only">' . $this
        ->t('@corner', [
        '@corner' => $corner_value,
      ]) . '</span><div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
        ->t('@corner', [
        '@corner' => $corner_value,
      ]) . '</div>',
      '#min' => 0,
      '#max' => $this
        ->getStyleOptionsCount('rounded_corner_' . $corner_key),
      '#step' => 1,
      '#default_value' => $default_value,
      '#validated' => TRUE,
      '#attributes' => [
        'class' => [
          'bs-field-rounded-corner-' . $corner_key,
        ],
      ],
      '#description' => $this
        ->getSvgIconMarkup($icon_path . 'plugins/border/border-radius-' . $corner_key . '.svg'),
    ];
  }

  // Pass border width and round corners options to drupal settings.
  $border_width_options = [];
  $border_width_options['border_width'] = array_keys($this
    ->getStyleOptions('border_width'));
  for ($i = 0; $i < 4; $i++) {
    $border_width_options['border_' . $directions[$i] . '_width'] = array_keys($this
      ->getStyleOptions('border_' . $directions[$i] . '_width'));
  }
  $rounded_corners_options = [];
  $rounded_corners_options['rounded_corners'] = array_keys($this
    ->getStyleOptions('rounded_corners'));
  foreach (array_keys($corners) as $corner_key) {
    $rounded_corners_options['rounded_corner_' . $corner_key] = array_keys($this
      ->getStyleOptions('rounded_corner_' . $corner_key));
  }
  $border_options = [
    'border_width' => $border_width_options,
    'rounded_corners' => $rounded_corners_options,
  ];
  $form['#attached']['drupalSettings']['bootstrap_styles']['border'] = $border_options;

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