You are here

public function StringTransformTrait::getIdentifier in Drupal 7 to 8/9 Module Upgrader 8

Generates an identifier (prefixed with the module name, if $this->module exists) from an arbitrary string.

Parameters

$string: The input string.

Return value

string The identifier.

1 call to StringTransformTrait::getIdentifier()
StringTransformTrait::getIdentifierFromPath in src/Utility/StringTransformTrait.php
Generates an identifier from a path.

File

src/Utility/StringTransformTrait.php, line 126

Class

StringTransformTrait
Contains methods for transforming strings in various helpful ways.

Namespace

Drupal\drupalmoduleupgrader\Utility

Code

public function getIdentifier($string) {

  // Replace all non-alphanumeric character sequences with an underscore.
  $id = preg_replace('/[^a-zA-Z0-9_]+/', '_', $string);
  if (isset($this->module)) {

    // If the name begins with MODULE_, replace that underscore with a period. Otherwise,
    // prefix the key with the module's machine name. We want all routes to look like
    // MODULE.route.
    if (stripos($id, $this->module
      ->getMachineName() . '_') === 0) {
      $id = preg_replace('/_/', '.', $id, 1);
    }
    else {
      $id = $this->module
        ->getMachineName() . '.' . $id;
    }
  }
  return $id;
}