function public static function Bootstrap::cssClassFromString

8.x-3.x Bootstrap.php public static Bootstrap::cssClassFromString(&$value, $default = '')

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.

Class

Bootstrap
The primary class for the Drupal Bootstrap base theme.

Namespace

Drupal\bootstrap

Source src/Bootstrap.php (line 465)

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('Reset')->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;
}