diff --git a/core/lib/Drupal/Core/Entity/EntityForm.php b/core/lib/Drupal/Core/Entity/EntityForm.php index fb015c7..b104c01 100644 --- a/core/lib/Drupal/Core/Entity/EntityForm.php +++ b/core/lib/Drupal/Core/Entity/EntityForm.php @@ -409,4 +409,15 @@ public function setEntityManager(EntityManagerInterface $entity_manager) { return $this; } + /** + * {@inheritdoc} + */ + public static function exists($entity_id, array $element, FormStateInterface $form_state) { + /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */ + $entity_type = $form_state->getFormObject()->getEntity()->getEntityType(); + return (bool) \Drupal::entityQuery($entity_type->id()) + ->condition($entity_type->getKey('id'), $entity_id) + ->execute(); + } + } diff --git a/core/lib/Drupal/Core/Entity/EntityFormInterface.php b/core/lib/Drupal/Core/Entity/EntityFormInterface.php index 29b248b..b32877b 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityFormInterface.php @@ -153,4 +153,19 @@ public function setModuleHandler(ModuleHandlerInterface $module_handler); */ public function setEntityManager(EntityManagerInterface $entity_manager); + /** + * Determines if the entity already exists. + * + * @param string|int $entity_id + * The entity ID. + * @param array $element + * The form element. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @return bool + * TRUE if the entity exists, FALSE otherwise. + */ + public static function exists($entity_id, array $element, FormStateInterface $form_state); + } diff --git a/core/modules/action/src/ActionFormBase.php b/core/modules/action/src/ActionFormBase.php index 6d4be05..cfbd58d 100644 --- a/core/modules/action/src/ActionFormBase.php +++ b/core/modules/action/src/ActionFormBase.php @@ -50,7 +50,7 @@ public function form(array $form, FormStateInterface $form_state) { '#maxlength' => 64, '#description' => $this->t('A unique name for this action. It must only contain lowercase letters, numbers and underscores.'), '#machine_name' => array( - 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], + 'exists' => [get_class($this), 'exists'], ), ); $form['plugin'] = array( diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php index 78acf55..89d9e53 100644 --- a/core/modules/block/src/BlockForm.php +++ b/core/modules/block/src/BlockForm.php @@ -130,7 +130,7 @@ public function form(array $form, FormStateInterface $form_state) { '#description' => $this->t('A unique name for this block instance. Must be alpha-numeric and underscore separated.'), '#default_value' => !$entity->isNew() ? $entity->id() : $this->getUniqueMachineName($entity), '#machine_name' => array( - 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], + 'exists' => [get_class($this), 'exists'], 'replace_pattern' => '[^a-z0-9_.]+', 'source' => array('settings', 'label'), ), diff --git a/core/modules/block_content/src/BlockContentTypeForm.php b/core/modules/block_content/src/BlockContentTypeForm.php index f9a5eb2..e343eaf 100644 --- a/core/modules/block_content/src/BlockContentTypeForm.php +++ b/core/modules/block_content/src/BlockContentTypeForm.php @@ -40,7 +40,7 @@ public function form(array $form, FormStateInterface $form_state) { '#type' => 'machine_name', '#default_value' => $block_type->id(), '#machine_name' => array( - 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], + 'exists' => [get_class($this), 'exists'], ), '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#disabled' => !$block_type->isNew(), diff --git a/core/modules/comment/src/CommentTypeForm.php b/core/modules/comment/src/CommentTypeForm.php index e4f6066..ab642f1 100644 --- a/core/modules/comment/src/CommentTypeForm.php +++ b/core/modules/comment/src/CommentTypeForm.php @@ -88,7 +88,7 @@ public function form(array $form, FormStateInterface $form_state) { '#type' => 'machine_name', '#default_value' => $comment_type->id(), '#machine_name' => array( - 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], + 'exists' => [get_class($this), 'exists'], ), '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#disabled' => !$comment_type->isNew(), diff --git a/core/modules/config/tests/config_test/src/ConfigTestForm.php b/core/modules/config/tests/config_test/src/ConfigTestForm.php index 158e39b..c6723ab 100644 --- a/core/modules/config/tests/config_test/src/ConfigTestForm.php +++ b/core/modules/config/tests/config_test/src/ConfigTestForm.php @@ -142,23 +142,4 @@ public function save(array $form, FormStateInterface $form_state) { $form_state->setRedirectUrl($this->entity->urlInfo('collection')); } - /** - * Determines if the entity already exists. - * - * @param string|int $entity_id - * The entity ID. - * @param array $element - * The form element. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - * - * @return bool - * TRUE if the entity exists, FALSE otherwise. - */ - public static function exists($entity_id, array $element, FormStateInterface $form_state) { - /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */ - $entity = $form_state->getFormObject()->getEntity(); - return (bool) \Drupal::entityManager()->getStorage($entity->getEntityTypeId())->load($entity_id); - } - } diff --git a/core/modules/contact/src/ContactFormEditForm.php b/core/modules/contact/src/ContactFormEditForm.php index fa19c93..798b24f 100644 --- a/core/modules/contact/src/ContactFormEditForm.php +++ b/core/modules/contact/src/ContactFormEditForm.php @@ -77,7 +77,7 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $contact_form->id(), '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#machine_name' => array( - 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], + 'exists' => [get_class($this), 'exists'], ), '#disabled' => !$contact_form->isNew(), ); diff --git a/core/modules/field_ui/src/Form/EntityDisplayModeFormBase.php b/core/modules/field_ui/src/Form/EntityDisplayModeFormBase.php index 8596905..4cedb52 100644 --- a/core/modules/field_ui/src/Form/EntityDisplayModeFormBase.php +++ b/core/modules/field_ui/src/Form/EntityDisplayModeFormBase.php @@ -97,26 +97,14 @@ public function form(array $form, FormStateInterface $form_state) { } /** - * Determines if the display mode already exists. - * - * @param string|int $entity_id - * The entity ID. - * @param array $element - * The form element. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - * - * @return bool - * TRUE if the display mode exists, FALSE otherwise. + * {@inheritdoc} */ public static function exists($entity_id, array $element, FormStateInterface $form_state) { // Do not allow to add internal 'default' view mode. if ($entity_id == 'default') { return TRUE; } - /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */ - $entity = $form_state->getFormObject()->getEntity(); - return (bool) $entity::load($element['#field_prefix'] . $entity_id); + return parent::exists($element['#field_prefix'] . $entity_id, $element, $form_state); } /** diff --git a/core/modules/filter/src/FilterFormatFormBase.php b/core/modules/filter/src/FilterFormatFormBase.php index 4bc7a9d..b4001a0 100644 --- a/core/modules/filter/src/FilterFormatFormBase.php +++ b/core/modules/filter/src/FilterFormatFormBase.php @@ -68,7 +68,7 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $format->id(), '#maxlength' => 255, '#machine_name' => array( - 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], + 'exists' => [get_class($this), 'exists'], 'source' => array('name'), ), '#disabled' => !$format->isNew(), diff --git a/core/modules/image/src/Form/ImageStyleFormBase.php b/core/modules/image/src/Form/ImageStyleFormBase.php index 27c98b5..63fdd55 100644 --- a/core/modules/image/src/Form/ImageStyleFormBase.php +++ b/core/modules/image/src/Form/ImageStyleFormBase.php @@ -36,7 +36,7 @@ public function form(array $form, FormStateInterface $form_state) { $form['name'] = array( '#type' => 'machine_name', '#machine_name' => array( - 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], + 'exists' => [get_class($this), 'exists'], ), '#default_value' => $this->entity->id(), '#required' => TRUE, diff --git a/core/modules/node/src/NodeTypeForm.php b/core/modules/node/src/NodeTypeForm.php index a549527..cca2375 100644 --- a/core/modules/node/src/NodeTypeForm.php +++ b/core/modules/node/src/NodeTypeForm.php @@ -85,7 +85,7 @@ public function form(array $form, FormStateInterface $form_state) { '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#disabled' => $type->isLocked(), '#machine_name' => array( - 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], + 'exists' => [get_class($this), 'exists'], 'source' => array('name'), ), '#description' => t('A unique machine-readable name for this content type. It must only contain lowercase letters, numbers, and underscores. This name will be used for constructing the URL of the %node-add page, in which underscores will be converted into hyphens.', array( diff --git a/core/modules/responsive_image/src/ResponsiveImageStyleForm.php b/core/modules/responsive_image/src/ResponsiveImageStyleForm.php index 9d3e38a..556bf99 100644 --- a/core/modules/responsive_image/src/ResponsiveImageStyleForm.php +++ b/core/modules/responsive_image/src/ResponsiveImageStyleForm.php @@ -77,7 +77,7 @@ public function form(array $form, FormStateInterface $form_state) { '#type' => 'machine_name', '#default_value' => $responsive_image_style->id(), '#machine_name' => array( - 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], + 'exists' => [get_class($this), 'exists'], 'source' => array('label'), ), '#disabled' => (bool) $responsive_image_style->id() && $this->operation != 'duplicate', diff --git a/core/modules/search/src/Form/SearchPageFormBase.php b/core/modules/search/src/Form/SearchPageFormBase.php index e0d37a5..4aaa0aa 100644 --- a/core/modules/search/src/Form/SearchPageFormBase.php +++ b/core/modules/search/src/Form/SearchPageFormBase.php @@ -103,7 +103,7 @@ public function form(array $form, FormStateInterface $form_state) { '#disabled' => !$this->entity->isNew(), '#maxlength' => 64, '#machine_name' => array( - 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], + 'exists' => [get_class($this), 'exists'], ), ); $form['path'] = array( diff --git a/core/modules/shortcut/src/ShortcutSetForm.php b/core/modules/shortcut/src/ShortcutSetForm.php index 9708aee..d053b24 100644 --- a/core/modules/shortcut/src/ShortcutSetForm.php +++ b/core/modules/shortcut/src/ShortcutSetForm.php @@ -32,7 +32,7 @@ public function form(array $form, FormStateInterface $form_state) { $form['id'] = array( '#type' => 'machine_name', '#machine_name' => array( - 'exists' => '\Drupal\shortcut\Entity\ShortcutSet::load', + 'exists' => [get_class($this), 'exists'], 'source' => array('label'), 'replace_pattern' => '[^a-z0-9-]+', 'replace' => '-', diff --git a/core/modules/system/src/Form/DateFormatFormBase.php b/core/modules/system/src/Form/DateFormatFormBase.php index c1ed32c..9976517 100644 --- a/core/modules/system/src/Form/DateFormatFormBase.php +++ b/core/modules/system/src/Form/DateFormatFormBase.php @@ -62,22 +62,10 @@ public static function create(ContainerInterface $container) { } /** - * Checks for an existing date format. - * - * @param string|int $entity_id - * The entity ID. - * @param array $element - * The form element. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - * - * @return bool - * TRUE if this format already exists, FALSE otherwise. + * {@inheritdoc} */ public static function exists($entity_id, array $element, FormStateInterface $form_state) { - /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */ - $entity = $form_state->getFormObject()->getEntity(); - return (bool) $entity::load($element['#field_prefix'] . $entity_id); + return parent::exists($element['#field_prefix'] . $entity_id, $element, $form_state); } /** diff --git a/core/modules/taxonomy/src/VocabularyForm.php b/core/modules/taxonomy/src/VocabularyForm.php index c7f48b0..b8cc663 100644 --- a/core/modules/taxonomy/src/VocabularyForm.php +++ b/core/modules/taxonomy/src/VocabularyForm.php @@ -42,7 +42,7 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $vocabulary->id(), '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#machine_name' => array( - 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], + 'exists' => [get_class($this), 'exists'], 'source' => array('name'), ), ); diff --git a/core/modules/user/src/RoleForm.php b/core/modules/user/src/RoleForm.php index b82b8f6..1a02ef8 100644 --- a/core/modules/user/src/RoleForm.php +++ b/core/modules/user/src/RoleForm.php @@ -38,7 +38,7 @@ public function form(array $form, FormStateInterface $form_state) { '#size' => 30, '#maxlength' => 64, '#machine_name' => array( - 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], + 'exists' => [get_class($this), 'exists'], ), ); $form['weight'] = array(