You are here

public static function Bootstrap::glyphiconFromString in Express 8

Matches a Bootstrap Glyphicon based on a string value.

Parameters

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

array $default: The default render array to return if no match is found.

Return value

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

3 calls to Bootstrap::glyphiconFromString()
Element::setIcon in themes/contrib/bootstrap/src/Utility/Element.php
Adds an icon to button element 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_iconize_text in themes/contrib/bootstrap/deprecated.php
Matches a Bootstrap Glyphicon based on a string value.

File

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

Class

Bootstrap
The primary class for the Drupal Bootstrap base theme.

Namespace

Drupal\bootstrap

Code

public static function glyphiconFromString(&$value, $default = []) {
  static $lang;
  if (!isset($lang)) {
    $lang = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
  }
  $theme = static::getTheme();
  $texts = $theme
    ->getCache('glyphiconFromString', [
    $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' => [],
      // Text containing these words anywhere in the string are checked last.
      'contains' => [
        t('Manage')
          ->render() => 'cog',
        t('Configure')
          ->render() => 'cog',
        t('Settings')
          ->render() => 'cog',
        t('Download')
          ->render() => 'download',
        t('Export')
          ->render() => 'export',
        t('Filter')
          ->render() => 'filter',
        t('Import')
          ->render() => 'import',
        t('Save')
          ->render() => 'ok',
        t('Update')
          ->render() => 'ok',
        t('Edit')
          ->render() => 'pencil',
        t('Uninstall')
          ->render() => 'trash',
        t('Install')
          ->render() => 'plus',
        t('Write')
          ->render() => 'plus',
        t('Cancel')
          ->render() => 'remove',
        t('Delete')
          ->render() => 'trash',
        t('Remove')
          ->render() => 'trash',
        t('Search')
          ->render() => 'search',
        t('Upload')
          ->render() => 'upload',
        t('Preview')
          ->render() => 'eye-open',
        t('Log in')
          ->render() => 'log-in',
      ],
    ];

    // 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_iconize_text', $data);
    $texts
      ->setMultiple($data);
  }

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

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