diff --git a/core/modules/action/src/ActionAddForm.php b/core/modules/action/src/ActionAddForm.php index 6d8e4aa..ac4fb0e 100644 --- a/core/modules/action/src/ActionAddForm.php +++ b/core/modules/action/src/ActionAddForm.php @@ -9,7 +9,6 @@ use Drupal\Component\Utility\Crypt; use Drupal\Core\Action\ActionManager; -use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -28,14 +27,10 @@ class ActionAddForm extends ActionFormBase { /** * Constructs a new ActionAddForm. * - * @param \Drupal\Core\Entity\EntityStorageInterface $storage - * The action storage. * @param \Drupal\Core\Action\ActionManager $action_manager * The action plugin manager. */ - public function __construct(EntityStorageInterface $storage, ActionManager $action_manager) { - parent::__construct($storage); - + public function __construct(ActionManager $action_manager) { $this->actionManager = $action_manager; } @@ -44,7 +39,6 @@ public function __construct(EntityStorageInterface $storage, ActionManager $acti */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager')->getStorage('action'), $container->get('plugin.manager.action') ); } diff --git a/core/modules/action/src/ActionFormBase.php b/core/modules/action/src/ActionFormBase.php index 96bb63d..6d4be05 100644 --- a/core/modules/action/src/ActionFormBase.php +++ b/core/modules/action/src/ActionFormBase.php @@ -8,10 +8,8 @@ namespace Drupal\action; use Drupal\Core\Entity\EntityForm; -use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\PluginFormInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a base form for action forms. @@ -26,32 +24,6 @@ protected $plugin; /** - * The action storage. - * - * @var \Drupal\Core\Entity\EntityStorageInterface - */ - protected $storage; - - /** - * Constructs a new action form. - * - * @param \Drupal\Core\Entity\EntityStorageInterface $storage - * The action storage. - */ - public function __construct(EntityStorageInterface $storage) { - $this->storage = $storage; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('entity.manager')->getStorage('action') - ); - } - - /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { @@ -78,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' => array($this, 'exists'), + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], ), ); $form['plugin'] = array( @@ -98,20 +70,6 @@ public function form(array $form, FormStateInterface $form_state) { } /** - * Determines if the action already exists. - * - * @param string $id - * The action ID - * - * @return bool - * TRUE if the action exists, FALSE otherwise. - */ - public function exists($id) { - $action = $this->storage->load($id); - return !empty($action); - } - - /** * {@inheritdoc} */ protected function actions(array $form, FormStateInterface $form_state) { diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php index 08d7508..78acf55 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' => '\Drupal\block\Entity\Block::load', + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], '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 c0f9ec5..f9a5eb2 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' => '\Drupal\block_content\Entity\BlockContentType::load', + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], ), '#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 3c7ef6a..e4f6066 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' => '\Drupal\comment\Entity\CommentType::load', + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], ), '#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 56195e2..f762d58 100644 --- a/core/modules/config/tests/config_test/src/ConfigTestForm.php +++ b/core/modules/config/tests/config_test/src/ConfigTestForm.php @@ -33,7 +33,7 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $entity->id(), '#required' => TRUE, '#machine_name' => array( - 'exists' => 'config_test_load', + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], 'replace_pattern' => '[^a-z0-9_.]+', ), ); diff --git a/core/modules/contact/src/ContactFormEditForm.php b/core/modules/contact/src/ContactFormEditForm.php index 3300c59..fa19c93 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' => '\Drupal\contact\Entity\ContactForm::load', + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], ), '#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 0bdce49..8596905 100644 --- a/core/modules/field_ui/src/Form/EntityDisplayModeFormBase.php +++ b/core/modules/field_ui/src/Form/EntityDisplayModeFormBase.php @@ -88,7 +88,7 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $this->entity->id(), '#field_prefix' => $this->entity->isNew() ? $this->entity->getTargetType() . '.' : '', '#machine_name' => array( - 'exists' => array($this, 'exists'), + 'exists' => [get_class($this), 'exists'], 'replace_pattern' => '[^a-z0-9_.]+', ), ); @@ -103,19 +103,20 @@ public function form(array $form, FormStateInterface $form_state) { * 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. */ - public function exists($entity_id, array $element) { + 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; } - return (bool) $this->queryFactory - ->get($this->entity->getEntityTypeId()) - ->condition('id', $element['#field_prefix'] . $entity_id) - ->execute(); + /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */ + $entity = $form_state->getFormObject()->getEntity(); + return (bool) $entity::load($element['#field_prefix'] . $entity_id); } /** diff --git a/core/modules/filter/src/FilterFormatFormBase.php b/core/modules/filter/src/FilterFormatFormBase.php index cbc68d6..4bc7a9d 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' => array($this, 'exists'), + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], 'source' => array('name'), ), '#disabled' => !$format->isNew(), @@ -186,22 +186,6 @@ public function form(array $form, FormStateInterface $form_state) { } /** - * Determines if the format already exists. - * - * @param string $format_id - * The format ID - * - * @return bool - * TRUE if the format exists, FALSE otherwise. - */ - public function exists($format_id) { - return (bool) $this->queryFactory - ->get('filter_format') - ->condition('format', $format_id) - ->execute(); - } - - /** * {@inheritdoc} */ public function validate(array $form, FormStateInterface $form_state) { diff --git a/core/modules/image/src/Form/ImageStyleEditForm.php b/core/modules/image/src/Form/ImageStyleEditForm.php index b07090b..804dfa3 100644 --- a/core/modules/image/src/Form/ImageStyleEditForm.php +++ b/core/modules/image/src/Form/ImageStyleEditForm.php @@ -7,7 +7,6 @@ namespace Drupal\image\Form; -use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\image\ConfigurableImageEffectInterface; @@ -30,13 +29,10 @@ class ImageStyleEditForm extends ImageStyleFormBase { /** * Constructs an ImageStyleEditForm object. * - * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage - * The storage. * @param \Drupal\image\ImageEffectManager $image_effect_manager * The image effect manager service. */ - public function __construct(EntityStorageInterface $image_style_storage, ImageEffectManager $image_effect_manager) { - parent::__construct($image_style_storage); + public function __construct(ImageEffectManager $image_effect_manager) { $this->imageEffectManager = $image_effect_manager; } diff --git a/core/modules/image/src/Form/ImageStyleFormBase.php b/core/modules/image/src/Form/ImageStyleFormBase.php index f5e432c..27c98b5 100644 --- a/core/modules/image/src/Form/ImageStyleFormBase.php +++ b/core/modules/image/src/Form/ImageStyleFormBase.php @@ -8,9 +8,7 @@ namespace Drupal\image\Form; use Drupal\Core\Entity\EntityForm; -use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Form\FormStateInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base form for image style add and edit forms. @@ -25,32 +23,6 @@ protected $entity; /** - * The image style entity storage. - * - * @var \Drupal\Core\Entity\EntityStorageInterface - */ - protected $imageStyleStorage; - - /** - * Constructs a base class for image style add and edit forms. - * - * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage - * The image style entity storage. - */ - public function __construct(EntityStorageInterface $image_style_storage) { - $this->imageStyleStorage = $image_style_storage; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('entity.manager')->getStorage('image_style') - ); - } - - /** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { @@ -64,7 +36,7 @@ public function form(array $form, FormStateInterface $form_state) { $form['name'] = array( '#type' => 'machine_name', '#machine_name' => array( - 'exists' => array($this->imageStyleStorage, 'load'), + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], ), '#default_value' => $this->entity->id(), '#required' => TRUE, diff --git a/core/modules/node/src/NodeTypeForm.php b/core/modules/node/src/NodeTypeForm.php index 6dfc669..a549527 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' => ['Drupal\node\Entity\NodeType', 'load'], + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], '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 365f7a5..9d3e38a 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' => '\Drupal\responsive_image\Entity\ResponsiveImageStyle::load', + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], '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 95afb4b..e0d37a5 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' => array($this, 'exists'), + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], ), ); $form['path'] = array( @@ -126,22 +126,6 @@ public function form(array $form, FormStateInterface $form_state) { } /** - * Determines if the search page entity already exists. - * - * @param string $id - * The search configuration ID. - * - * @return bool - * TRUE if the search configuration exists, FALSE otherwise. - */ - public function exists($id) { - $entity = $this->entityQuery->get('search_page') - ->condition('id', $id) - ->execute(); - return (bool) $entity; - } - - /** * {@inheritdoc} */ public function validate(array $form, FormStateInterface $form_state) { diff --git a/core/modules/shortcut/src/Form/SwitchShortcutSet.php b/core/modules/shortcut/src/Form/SwitchShortcutSet.php index e7ee0e1..8d5a9e4 100644 --- a/core/modules/shortcut/src/Form/SwitchShortcutSet.php +++ b/core/modules/shortcut/src/Form/SwitchShortcutSet.php @@ -105,7 +105,7 @@ public function buildForm(array $form, FormStateInterface $form_state, UserInter $form['id'] = array( '#type' => 'machine_name', '#machine_name' => array( - 'exists' => array($this, 'exists'), + 'exists' => ['\Drupal\shortcut\Entity\ShortcutSet', 'load'], 'replace_pattern' => '[^a-z0-9-]+', 'replace' => '-', ), @@ -141,21 +141,6 @@ public function buildForm(array $form, FormStateInterface $form_state, UserInter } /** - * Determines if a shortcut set exists already. - * - * @param string $id - * The set ID to check. - * - * @return bool - * TRUE if the shortcut set exists, FALSE otherwise. - */ - public function exists($id) { - return (bool) $this->shortcutSetStorage->getQuery() - ->condition('id', $id) - ->execute(); - } - - /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { diff --git a/core/modules/system/src/Form/DateFormatFormBase.php b/core/modules/system/src/Form/DateFormatFormBase.php index c843ba2..c1ed32c 100644 --- a/core/modules/system/src/Form/DateFormatFormBase.php +++ b/core/modules/system/src/Form/DateFormatFormBase.php @@ -68,15 +68,16 @@ public static function create(ContainerInterface $container) { * 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. */ - public function exists($entity_id, array $element) { - return (bool) $this->dateFormatStorage - ->getQuery() - ->condition('id', $element['#field_prefix'] . $entity_id) - ->execute(); + 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); } /** @@ -121,7 +122,7 @@ public function form(array $form, FormStateInterface $form_state) { '#disabled' => !$this->entity->isNew(), '#default_value' => $this->entity->id(), '#machine_name' => array( - 'exists' => array($this, 'exists'), + 'exists' => array(get_class($this), 'exists'), 'replace_pattern' =>'([^a-z0-9_]+)|(^custom$)', 'error' => $this->t('The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".'), ), diff --git a/core/modules/taxonomy/src/VocabularyForm.php b/core/modules/taxonomy/src/VocabularyForm.php index ffedfda..c7f48b0 100644 --- a/core/modules/taxonomy/src/VocabularyForm.php +++ b/core/modules/taxonomy/src/VocabularyForm.php @@ -12,8 +12,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\language\Entity\ContentLanguageSettings; -use Drupal\taxonomy\VocabularyStorageInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base form for vocabulary edit forms. @@ -21,32 +19,6 @@ class VocabularyForm extends EntityForm { /** - * The vocabulary storage. - * - * @var \Drupal\taxonomy\VocabularyStorageInterface. - */ - protected $vocabularyStorage; - - /** - * Constructs a new vocabulary form. - * - * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage - * The vocabulary storage. - */ - public function __construct(VocabularyStorageInterface $vocabulary_storage) { - $this->vocabularyStorage = $vocabulary_storage; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('entity.manager')->getStorage('taxonomy_vocabulary') - ); - } - - /** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { @@ -70,7 +42,7 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $vocabulary->id(), '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#machine_name' => array( - 'exists' => array($this, 'exists'), + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], 'source' => array('name'), ), ); @@ -164,18 +136,4 @@ public function save(array $form, FormStateInterface $form_state) { $form_state->set('vid', $vocabulary->id()); } - /** - * Determines if the vocabulary already exists. - * - * @param string $id - * The vocabulary ID - * - * @return bool - * TRUE if the vocabulary exists, FALSE otherwise. - */ - public function exists($id) { - $action = $this->vocabularyStorage->load($id); - return !empty($action); - } - } diff --git a/core/modules/user/src/RoleForm.php b/core/modules/user/src/RoleForm.php index bb801ea..b82b8f6 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' => ['\Drupal\user\Entity\Role', 'load'], + 'exists' => [$this->entity->getEntityType()->getClass(), 'load'], ), ); $form['weight'] = array(