function public static function Bootstrap::glyphiconFromString

8.x-3.x Bootstrap.php public static Bootstrap::glyphiconFromString(&$value, array $default = [])

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

array The Bootstrap icon 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 841)

public static function glyphiconFromString(&$value, array $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('Reset')->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;
}