Commit 64759e46 authored by Jacob Rockowitz's avatar Jacob Rockowitz Committed by Jacob Rockowitz
Browse files

Issue #3005493 by jrockowitz, facine:...

Issue #3005493 by jrockowitz, facine: WebformHandlerFormBase::getUniqueMachineName always returns an empty suggestion
parent e85ea4dc
Loading
Loading
Loading
Loading
+6 −2
Changes for src/Form/WebformHandlerAddForm.php: 6 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\webform\Form;

use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerManagerInterface;
use Drupal\webform\WebformInterface;
@@ -24,13 +25,15 @@ class WebformHandlerAddForm extends WebformHandlerFormBase {
  /**
   * Constructs a WebformHandlerAddForm.
   *
   * @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
   *   The transliteration helper.
   * @param \Drupal\webform\WebformTokenManagerInterface $token_manager
   *   The webform token manager.
   * @param \Drupal\webform\Plugin\WebformHandlerManagerInterface $webform_handler
   *   The webform handler manager.
   */
  public function __construct(WebformTokenManagerInterface $token_manager, WebformHandlerManagerInterface $webform_handler) {
    parent::__construct($token_manager);
  public function __construct(TransliterationInterface $transliteration, WebformTokenManagerInterface $token_manager, WebformHandlerManagerInterface $webform_handler) {
    parent::__construct($transliteration, $token_manager);
    $this->webformHandlerManager = $webform_handler;
  }

@@ -39,6 +42,7 @@ class WebformHandlerAddForm extends WebformHandlerFormBase {
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('transliteration'),
      $container->get('webform.token_manager'),
      $container->get('plugin.manager.webform.handler')
    );
+40 −8
Changes for src/Form/WebformHandlerFormBase.php: 40 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -2,10 +2,11 @@

namespace Drupal\webform\Form;

use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\webform\Plugin\WebformHandlerInterface;
use Drupal\webform\Utility\WebformFormHelper;
use Drupal\webform\WebformInterface;
@@ -20,6 +21,18 @@ abstract class WebformHandlerFormBase extends FormBase {

  use WebformDialogFormTrait;

  /**
   * Machine name maxlenght.
   */
  const MACHINE_NAME_MAXLENGHTH = 64;

  /**
   * The transliteration helper.
   *
   * @var \Drupal\Component\Transliteration\TransliterationInterface
   */
  protected $transliteration;

  /**
   * The token manager.
   *
@@ -51,10 +64,13 @@ abstract class WebformHandlerFormBase extends FormBase {
  /**
   * Constructs a WebformHandlerFormBase.
   *
   * @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
   *   The transliteration helper.
   * @param \Drupal\webform\WebformTokenManagerInterface $token_manager
   *   The webform token manager.
   */
  public function __construct(WebformTokenManagerInterface $token_manager) {
  public function __construct(TransliterationInterface $transliteration, WebformTokenManagerInterface $token_manager) {
    $this->transliteration = $transliteration;
    $this->tokenManager = $token_manager;
  }

@@ -63,6 +79,7 @@ abstract class WebformHandlerFormBase extends FormBase {
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('transliteration'),
      $container->get('webform.token_manager')
    );
  }
@@ -150,7 +167,7 @@ abstract class WebformHandlerFormBase extends FormBase {
    ];
    $form['general']['handler_id'] = [
      '#type' => 'machine_name',
      '#maxlength' => 64,
      '#maxlength' => static::MACHINE_NAME_MAXLENGHTH,
      '#description' => $this->t('A unique name for this handler instance. Must be alpha-numeric and underscore separated.'),
      '#default_value' => $this->webformHandler->getHandlerId() ?: $this->getUniqueMachineName($this->webformHandler),
      '#required' => TRUE,
@@ -310,24 +327,39 @@ abstract class WebformHandlerFormBase extends FormBase {
  }

  /**
   * Generates a unique machine name for a webform handler instance.
   * Generates a unique translated machine name for a webform handler instance.
   *
   * @param \Drupal\webform\Plugin\WebformHandlerInterface $handler
   *   The webform handler.
   *
   * @return string
   *   Returns the unique name.
   *   Returns a unique machine based the handler's plugin label.
   *
   * @see \Drupal\Core\Render\Element\MachineName
   * @see \Drupal\system\MachineNameController::transliterate
   */
  public function getUniqueMachineName(WebformHandlerInterface $handler) {
    $suggestion = $handler->getPluginId();
    //  Get label which default to the plugin's label for new instances.
    $label = (string) $this->webformHandler->label();

    // Get current langcode.
    $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();

    // Get machine name.
    /** @var \Drupal\Component\Transliteration\TransliterationInterface $transliteration */
    $transliteration = \Drupal::service('transliteration');
    $suggestion = $transliteration->transliterate($label, $langcode, '_', static::MACHINE_NAME_MAXLENGHTH);
    $suggestion = mb_strtolower($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++;
    }
    // Only return a suggestion if it is not the default plugin id.
    return ($machine_default != $handler->getPluginId()) ? $machine_default : '';

    return $machine_default;
  }

  /**