You are here

public function UltimenuManager::truncateRegionKey in Ultimenu 8

Simplify menu names or menu item titles for region key.

If region key is to use menu item title: Region key: ultimenu_LOOOOOOOOOOOONGMENUNAME_LOOOOOOOOOOOOOOOOOONGMENUITEM. If region key is to use unfriendly key UUID, we'll only care for menu name. Region key: ultimenu_LOOOOOOOOOOOOOONGMENUNAME_1c2d3e4.

Parameters

string $string: The Menu name or menu item title.

int $max_length: The amount of characters to truncate.

Return value

string The truncated menu properties ready to use for region key.

Overrides UltimenuManagerInterface::truncateRegionKey

2 calls to UltimenuManager::truncateRegionKey()
UltimenuManager::getRegionKey in src/UltimenuManager.php
Gets the region key.
UltimenuManager::getRegions in src/UltimenuManager.php
The array of available Ultimenu regions based on enabled menu items.

File

src/UltimenuManager.php, line 291

Class

UltimenuManager
Manages Ultimenu plugin.

Namespace

Drupal\ultimenu

Code

public function truncateRegionKey($string, $max_length = 28) {

  // Transliterate the string.
  $langcode = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $trans = \Drupal::transliteration();
  $transformed = $trans
    ->transliterate($string, $langcode);

  // Decode it.
  $transformed = Html::decodeEntities($transformed);
  $transformed = Unicode::strtolower(str_replace(array(
    'menu-',
    '-menu',
  ), '', $transformed));
  $transformed = preg_replace('/[\\W\\s]+/', '_', $transformed);

  // Trim trailing underscores.
  $transformed = trim($transformed, '_');
  $transformed = Unicode::truncate($transformed, $max_length, TRUE, FALSE);
  return $transformed;
}