RegionWells.php 2.89 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\bootstrap\Plugin\Setting\Components\Region;

use Drupal\bootstrap\Plugin\Setting\SettingBase;
6
use Drupal\bootstrap\Utility\Element;
7 8 9 10 11
use Drupal\Core\Form\FormStateInterface;

/**
 * The "region_wells" theme setting.
 *
12 13
 * @ingroup plugins_setting
 *
14 15 16 17 18
 * @BootstrapSetting(
 *   id = "region_wells",
 *   type = "container",
 *   description = @Translation("Enable the <code>.well</code>, <code>.well-sm</code> or <code>.well-lg</code> classes for specified regions."),
 *   defaultValue = {
19 20 21 22 23 24 25
 *     "navigation" = "",
 *     "navigation_collapsible" = "",
 *     "header" = "",
 *     "highlighted" = "",
 *     "help" = "",
 *     "content" = "",
 *     "sidebar_first" = "",
26
 *     "sidebar_second" = "well",
27
 *     "footer" = "",
28 29 30 31 32 33
 *   },
 *   groups = {
 *     "components" = @Translation("Components"),
 *     "region_wells" = @Translation("Region Wells"),
 *   },
 *   see = {
34
 *     "https://getbootstrap.com/docs/3.4/components/#wells" = @Translation("Bootstrap Wells"),
35 36 37 38 39 40 41 42
 *   },
 * )
 */
class RegionWells extends SettingBase {

  /**
   * {@inheritdoc}
   */
43 44
  public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
    parent::alterFormElement($form, $form_state, $form_id);
45

46 47
    $group = $this->getGroupElement($form, $form_state);
    $setting = $this->getSettingElement($form, $form_state);
48 49 50 51 52

    // Move description.
    $group->setProperty('description', $setting->getProperty('description'));

    // Retrieve the current default values.
53
    $default_values = $setting->getProperty('default_value', $this->getDefaultValue());
54 55 56 57 58 59 60 61 62 63

    $wells = [
      '' => t('None'),
      'well' => t('.well (normal)'),
      'well well-sm' => t('.well-sm (small)'),
      'well well-lg' => t('.well-lg (large)'),
    ];
    // Create dynamic well settings for each region.
    $regions = system_region_list($this->theme->getName());
    foreach ($regions as $name => $title) {
64 65 66
      if (in_array($name, ['page_top', 'page_bottom'])) {
        continue;
      }
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
      $setting->{'region_well-' . $name} = [
        '#title' => $title,
        '#type' => 'select',
        '#attributes' => [
          'class' => ['input-sm'],
        ],
        '#options' => $wells,
        '#default_value' => isset($default_values[$name]) ? $default_values[$name] : '',
      ];
    }
  }

  /**
   * {@inheritdoc}
   */
82
  public static function submitFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    $values = $form_state->getValues();

    // Extract the regions from individual dynamic settings.
    $regex = '/^region_well-/';
    $region_wells = [];
    foreach ($values as $key => $value) {
      if (!preg_match($regex, $key)) {
        continue;
      }
      $region_wells[preg_replace($regex, '', $key)] = $value;
      unset($values[$key]);
    }

    // Store the new values.
    $values['region_wells'] = $region_wells;
    $form_state->setValues($values);
  }

}