diff --git a/core/modules/shortcut/src/Entity/ShortcutSet.php b/core/modules/shortcut/src/Entity/ShortcutSet.php index 7b42310..3c22dc3 100644 --- a/core/modules/shortcut/src/Entity/ShortcutSet.php +++ b/core/modules/shortcut/src/Entity/ShortcutSet.php @@ -17,9 +17,8 @@ * "access" = "Drupal\shortcut\ShortcutSetAccessControlHandler", * "list_builder" = "Drupal\shortcut\ShortcutSetListBuilder", * "form" = { - * "default" = "Drupal\shortcut\ShortcutSetForm", - * "add" = "Drupal\shortcut\ShortcutSetForm", - * "edit" = "Drupal\shortcut\ShortcutSetForm", + * "add" = "Drupal\shortcut\Form\ShortcutSetAddForm", + * "edit" = "Drupal\shortcut\Form\ShortcutSetEditForm", * "customize" = "Drupal\shortcut\Form\SetCustomize", * "delete" = "Drupal\shortcut\Form\ShortcutSetDeleteForm" * } diff --git a/core/modules/shortcut/src/ShortcutSetForm.php b/core/modules/shortcut/src/Form/ShortcutSetAddForm.php similarity index 57% copy from core/modules/shortcut/src/ShortcutSetForm.php copy to core/modules/shortcut/src/Form/ShortcutSetAddForm.php index 012de5f..e381999 100644 --- a/core/modules/shortcut/src/ShortcutSetForm.php +++ b/core/modules/shortcut/src/Form/ShortcutSetAddForm.php @@ -1,14 +1,14 @@ entity; $form['label'] = array( '#type' => 'textfield', '#title' => t('Set name'), '#description' => t('The new set is created by copying items from your default shortcut set.'), '#required' => TRUE, - '#default_value' => $entity->label(), ); $form['id'] = array( '#type' => 'machine_name', @@ -32,14 +30,22 @@ public function form(array $form, FormStateInterface $form_state) { 'replace_pattern' => '[^a-z0-9-]+', 'replace' => '-', ), - '#default_value' => $entity->id(), // This id could be used for menu name. '#maxlength' => 23, ); - $form['actions']['submit']['#value'] = t('Create new set'); + return $form; + } - return $this->protectBundleIdElement($form); + /** + * {@inheritdoc} + */ + public function validate(array $form, FormStateInterface $form_state) { + parent::validate($form, $form_state); + // Check to prevent a duplicate title. + if (shortcut_set_title_exists($form_state->getValue('label'))) { + $form_state->setErrorByName('label', $this->t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state->getValue('label')))); + } } /** @@ -47,15 +53,8 @@ public function form(array $form, FormStateInterface $form_state) { */ public function save(array $form, FormStateInterface $form_state) { $entity = $this->entity; - $is_new = !$entity->getOriginalId(); $entity->save(); - - if ($is_new) { - drupal_set_message(t('The %set_name shortcut set has been created. You can edit it from this page.', array('%set_name' => $entity->label()))); - } - else { - drupal_set_message(t('Updated set name to %set-name.', array('%set-name' => $entity->label()))); - } + drupal_set_message(t('The %set_name shortcut set has been created. You can edit it from this page.', array('%set_name' => $entity->label()))); $form_state->setRedirectUrl($this->entity->urlInfo('customize-form')); } diff --git a/core/modules/shortcut/src/ShortcutSetForm.php b/core/modules/shortcut/src/Form/ShortcutSetEditForm.php similarity index 57% rename from core/modules/shortcut/src/ShortcutSetForm.php rename to core/modules/shortcut/src/Form/ShortcutSetEditForm.php index 012de5f..54c04cf 100644 --- a/core/modules/shortcut/src/ShortcutSetForm.php +++ b/core/modules/shortcut/src/Form/ShortcutSetEditForm.php @@ -1,14 +1,14 @@ '-', ), '#default_value' => $entity->id(), + '#disabled' => TRUE, // This id could be used for menu name. '#maxlength' => 23, ); - $form['actions']['submit']['#value'] = t('Create new set'); + return $form; + } - return $this->protectBundleIdElement($form); + /** + * {@inheritdoc} + */ + public function validate(array $form, FormStateInterface $form_state) { + parent::validate($form, $form_state); + $entity = $this->entity; + // Check to prevent a duplicate title. + if ($form_state->getValue('label') != $entity->label() && shortcut_set_title_exists($form_state->getValue('label'))) { + $form_state->setErrorByName('label', $this->t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state->getValue('label')))); + } } /** @@ -47,15 +58,8 @@ public function form(array $form, FormStateInterface $form_state) { */ public function save(array $form, FormStateInterface $form_state) { $entity = $this->entity; - $is_new = !$entity->getOriginalId(); $entity->save(); - - if ($is_new) { - drupal_set_message(t('The %set_name shortcut set has been created. You can edit it from this page.', array('%set_name' => $entity->label()))); - } - else { - drupal_set_message(t('Updated set name to %set-name.', array('%set-name' => $entity->label()))); - } + drupal_set_message(t('Updated set name to %set-name.', array('%set-name' => $entity->label()))); $form_state->setRedirectUrl($this->entity->urlInfo('customize-form')); }