You are here

public static function Bootstrap::cssClassFromString in Express 8

Matches a Bootstrap class based on a string value.

Parameters

string|array $value: The string to match against to determine the class. Passed by reference in case it is a render array that needs to be rendered and typecast.

string $default: The default class to return if no match is found.

Return value

string The Bootstrap class matched against the value of $haystack or $default if no match could be made.

3 calls to Bootstrap::cssClassFromString()
Element::colorize in themes/contrib/bootstrap/src/Utility/Element.php
Adds a specific Bootstrap class to color a button based on its text value.
MenuLocalAction::preprocessElement in themes/contrib/bootstrap/src/Plugin/Preprocess/MenuLocalAction.php
Preprocess the variables array if an element is present.
_bootstrap_colorize_text in themes/contrib/bootstrap/deprecated.php
Matches a Bootstrap class based on a string value.

File

themes/contrib/bootstrap/src/Bootstrap.php, line 307
Contains \Drupal\bootstrap\Bootstrap.

Class

Bootstrap
The primary class for the Drupal Bootstrap base theme.

Namespace

Drupal\bootstrap

Code

public static function cssClassFromString(&$value, $default = '') {
  static $lang;
  if (!isset($lang)) {
    $lang = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
  }
  $theme = static::getTheme();
  $texts = $theme
    ->getCache('cssClassFromString', [
    $lang,
  ]);

  // Ensure it's a string value that was passed.
  $string = static::toString($value);
  if ($texts
    ->isEmpty()) {
    $data = [
      // Text that match these specific strings are checked first.
      'matches' => [
        // Primary class.
        t('Download feature')
          ->render() => 'primary',
        // Success class.
        t('Add effect')
          ->render() => 'success',
        t('Add and configure')
          ->render() => 'success',
        t('Save configuration')
          ->render() => 'success',
        t('Install and set as default')
          ->render() => 'success',
        // Info class.
        t('Save and add')
          ->render() => 'info',
        t('Add another item')
          ->render() => 'info',
        t('Update style')
          ->render() => 'info',
      ],
      // Text containing these words anywhere in the string are checked last.
      'contains' => [
        // Primary class.
        t('Confirm')
          ->render() => 'primary',
        t('Filter')
          ->render() => 'primary',
        t('Log in')
          ->render() => 'primary',
        t('Submit')
          ->render() => 'primary',
        t('Search')
          ->render() => 'primary',
        t('Settings')
          ->render() => 'primary',
        t('Upload')
          ->render() => 'primary',
        // Danger class.
        t('Delete')
          ->render() => 'danger',
        t('Remove')
          ->render() => 'danger',
        t('Uninstall')
          ->render() => 'danger',
        // Success class.
        t('Add')
          ->render() => 'success',
        t('Create')
          ->render() => 'success',
        t('Install')
          ->render() => 'success',
        t('Save')
          ->render() => 'success',
        t('Write')
          ->render() => 'success',
        // Warning class.
        t('Export')
          ->render() => 'warning',
        t('Import')
          ->render() => 'warning',
        t('Restore')
          ->render() => 'warning',
        t('Rebuild')
          ->render() => 'warning',
        // Info class.
        t('Apply')
          ->render() => 'info',
        t('Update')
          ->render() => 'info',
      ],
    ];

    // Allow sub-themes to alter this array of patterns.

    /** @var \Drupal\Core\Theme\ThemeManager $theme_manager */
    $theme_manager = \Drupal::service('theme.manager');
    $theme_manager
      ->alter('bootstrap_colorize_text', $data);
    $texts
      ->setMultiple($data);
  }

  // Iterate over the array.
  foreach ($texts as $pattern => $strings) {
    foreach ($strings as $text => $class) {
      switch ($pattern) {
        case 'matches':
          if ($string === $text) {
            return $class;
          }
          break;
        case 'contains':
          if (strpos(Unicode::strtolower($string), Unicode::strtolower($text)) !== FALSE) {
            return $class;
          }
          break;
      }
    }
  }

  // Return the default if nothing was matched.
  return $default;
}