/** * {@inheritdoc} */ public function preprocessElement(Element $element, Variables $variables) { $link = $element->getProperty('link'); $link += ['localized_options' => []]; $link['localized_options']['set_active_class'] = TRUE; $icon = Bootstrap::glyphiconFromString($link['title']); $options = isset($link['localized_options']) ? $link['localized_options'] : []; if (isset($link['url'])) { // Turn link into a mini-button and colorize based on title. $class = Bootstrap::cssClassFromString($link['title'], 'default'); if (!isset($options['attributes']['class'])) { $options['attributes']['class'] = []; } $string = is_string($options['attributes']['class']); if ($string) { $options['attributes']['class'] = explode(' ', $options['attributes']['class']); } $options['attributes']['class'][] = 'btn'; $options['attributes']['class'][] = 'btn-xs'; $options['attributes']['class'][] = 'btn-' . $class; if ($string) { $options['attributes']['class'] = implode(' ', $options['attributes']['class']); } $variables['link'] = ['#type' => 'link', '#title' => SafeMarkup::format(\Drupal::service('renderer')->render($icon) . '@text', ['@text' => $link['title']]), '#options' => $options, '#url' => $link['url']]; } else { $variables['link'] = ['#type' => 'link', '#title' => $link['title'], '#options' => $options, '#url' => $link['url']]; } }
/** * Matches a Bootstrap class based on a string value. * * @param string $string * The string to match classes against. * @param string $default * The default class to return if no match is found. * * @return string * The Bootstrap class matched against the value of $haystack or $default if * no match could be made. * * @deprecated Will be removed in a future release. * * @code * // Before. * $class = _bootstrap_colorize_text($string, $default); * * // After. * use Drupal\bootstrap\Bootstrap; * $class = Bootstrap::cssClassFromString($string, $default); * @endcode * * @see \Drupal\bootstrap\Bootstrap::cssClassFromString() */ function _bootstrap_colorize_text($string, $default = '') { Bootstrap::deprecated(); return Bootstrap::cssClassFromString($string, $default); }
/** * Adds a specific Bootstrap class to color a button based on its text value. * * @return $this */ public function colorize() { $button = $this->isButton(); // Do nothing if setting is disabled. if (!$button || !Bootstrap::getTheme()->getSetting('button_colorize')) { return $this; } // @todo refactor this more so it's not just "button" specific. $prefix = $button ? 'btn' : 'has'; // Don't add a class if one is already present in the array. $button_classes = ["{$prefix}-default", "{$prefix}-primary", "{$prefix}-success", "{$prefix}-info", "{$prefix}-warning", "{$prefix}-danger", "{$prefix}-link"]; foreach ($button_classes as $class) { if ($this->hasClass($class)) { return $this; } } $this->addClass("{$prefix}-" . Bootstrap::cssClassFromString($this->array['#value'], 'default')); return $this; }
/** * Adds a specific Bootstrap class to color a button based on its text value. * * @return $this */ public function colorize() { $button = $this->isButton(); // @todo refactor this more so it's not just "button" specific. $prefix = $button ? 'btn' : 'has'; // Don't add a class if one is already present in the array. $classes = ["{$prefix}-default", "{$prefix}-primary", "{$prefix}-success", "{$prefix}-info", "{$prefix}-warning", "{$prefix}-danger", "{$prefix}-link"]; foreach ($classes as $class) { if ($this->hasClass($class)) { if ($button && $this->getProperty('split')) { $this->addClass($class, $this::SPLIT_BUTTON); } return $this; } } // Do nothing if setting is disabled. if ($button && !Bootstrap::getTheme()->getSetting('button_colorize')) { $this->addClass('btn-default'); return $this; } if ($value = $this->getProperty('value', $this->getProperty('title'))) { $class = "{$prefix}-" . Bootstrap::cssClassFromString($value, $this->getProperty('button_type', 'default')); $this->addClass($class); if ($button && $this->getProperty('split')) { $this->addClass($class, $this::SPLIT_BUTTON); } } return $this; }