function public function Element::setButtonSize

8.x-3.x Element.php public Element::setButtonSize($class = NULL, $override = TRUE)

Adds Bootstrap button size class to the element.

Parameters

string $class: The full button size class to add. If none is provided, it will default to any set theme setting.

bool $override: Flag indicating if the passed $class should be forcibly set. Setting this to FALSE allows any existing set class to persist.

Return value

static

Class

Element
Provides helper methods for Drupal render elements.

Namespace

Drupal\bootstrap\Utility

Source src/Utility/Element.php (line 616)

public function setButtonSize($class = NULL, $override = TRUE) {
  // Immediately return if element is not a button.
  if (!$this->isButton()) {
    return $this;
  }

  // Retrieve the button size classes from the specific setting's options.
  static $classes;
  if (!isset($classes)) {
    $classes = [];
    if ($button_size = Bootstrap::getTheme()->getSettingPlugin('button_size')) {
      $classes = array_keys($button_size->getOptions());
    }
  }

  // Search for an existing class.
  if (!$class || !$override) {
    foreach ($classes as $value) {
      if ($this->hasClass($value)) {
        $class = $value;
        break;
      }
    }
  }

  // Attempt to get the default button size, if set.
  if (!$class) {
    $class = Bootstrap::getTheme()->getSetting('button_size');
  }

  // Remove any existing classes and add the specified class.
  if ($class) {
    $this->removeClass($classes)->addClass($class);
    if ($this->getProperty('split')) {
      $this->removeClass($classes, $this::SPLIT_BUTTON)->addClass($class, $this::SPLIT_BUTTON);
    }
  }

  return $this;
}