You are here

public function Element::colorize in Express 8

Adds a specific Bootstrap class to color a button based on its text value.

Parameters

bool $override: Flag determining whether or not to override any existing set class.

Return value

$this

File

themes/contrib/bootstrap/src/Utility/Element.php, line 214
Contains \Drupal\bootstrap\Utility\Element.

Class

Element
Provides helper methods for Drupal render elements.

Namespace

Drupal\bootstrap\Utility

Code

public function colorize($override = TRUE) {
  $button = $this
    ->isButton();

  // @todo refactor this more so it's not just "button" specific.
  $prefix = $button ? 'btn' : 'has';

  // List of classes, based on the prefix.
  $classes = [
    "{$prefix}-primary",
    "{$prefix}-success",
    "{$prefix}-info",
    "{$prefix}-warning",
    "{$prefix}-danger",
    "{$prefix}-link",
    // Default should be last.
    "{$prefix}-default",
  ];

  // Set the class to "btn-default" if it shouldn't be colorized.
  $class = $button && !Bootstrap::getTheme()
    ->getSetting('button_colorize') ? 'btn-default' : FALSE;

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

  // Find a class based on the value of "value", "title" or "button_type".
  if (!$class) {
    $value = $this
      ->getProperty('value', $this
      ->getProperty('title', ''));
    $class = "{$prefix}-" . Bootstrap::cssClassFromString($value, $button ? $this
      ->getProperty('button_type', 'default') : 'default');
  }

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