You are here

public function WebformHandlerFormBase::getUniqueMachineName in Webform 8.5

Generates a unique translated machine name for a webform handler instance.

Parameters

\Drupal\webform\Plugin\WebformHandlerInterface $handler: The webform handler.

Return value

string Returns a unique machine based the handler's plugin label.

See also

\Drupal\Core\Render\Element\MachineName

\Drupal\system\MachineNameController::transliterate

1 call to WebformHandlerFormBase::getUniqueMachineName()
WebformHandlerFormBase::buildForm in src/Form/WebformHandlerFormBase.php
Form constructor.

File

src/Form/WebformHandlerFormBase.php, line 362

Class

WebformHandlerFormBase
Provides a base webform for webform handlers.

Namespace

Drupal\webform\Form

Code

public function getUniqueMachineName(WebformHandlerInterface $handler) {

  // Get label which default to the plugin's label for new instances.
  $label = (string) $this->webformHandler
    ->label();

  // Get current langcode.
  $langcode = $this->languageManager
    ->getCurrentLanguage()
    ->getId();

  // Get machine name.
  $suggestion = $this->transliteration
    ->transliterate($label, $langcode, '_', static::MACHINE_NAME_MAXLENGHTH);
  $suggestion = mb_strtolower($suggestion);
  $suggestion = preg_replace('@' . strtr('[^a-z0-9_]+', [
    '@' => '\\@',
    chr(0) => '',
  ]) . '@', '_', $suggestion);

  // Increment the machine name.
  $count = 1;
  $machine_default = $suggestion;
  $instance_ids = $this->webform
    ->getHandlers()
    ->getInstanceIds();
  while (isset($instance_ids[$machine_default])) {
    $machine_default = $suggestion . '_' . $count++;
  }
  return $machine_default;
}