diff --git a/core/modules/config/src/Tests/ConfigEntityFormOverrideTest.php b/core/modules/config/src/Tests/ConfigEntityFormOverrideTest.php index a07da59..632d2dc 100644 --- a/core/modules/config/src/Tests/ConfigEntityFormOverrideTest.php +++ b/core/modules/config/src/Tests/ConfigEntityFormOverrideTest.php @@ -31,6 +31,8 @@ public function testFormsWithOverrides() { $overridden_label = 'Overridden label'; $edited_label = 'Edited label'; + $config_test_storage = $this->container->get('entity.manager')->getStorage('config_test'); + // Set up an override. $settings['config']['config_test.dynamic.dotted.default']['label'] = (object) array( 'value' => $overridden_label, @@ -39,7 +41,7 @@ public function testFormsWithOverrides() { $this->writeSettings($settings); // Test that the overridden label is loaded with the entity. - $this->assertEqual(config_test_load('dotted.default')->label(), $overridden_label); + $this->assertEqual($config_test_storage->load('dotted.default')->label(), $overridden_label); // Test that the original label on the listing page is intact. $this->drupalGet('admin/structure/config_test'); @@ -67,7 +69,7 @@ public function testFormsWithOverrides() { $this->assertIdentical((string) $elements[0]['value'], $edited_label); // Test that the overridden label is still loaded with the entity. - $this->assertEqual(config_test_load('dotted.default')->label(), $overridden_label); + $this->assertEqual($config_test_storage->load('dotted.default')->label(), $overridden_label); } } diff --git a/core/modules/config/tests/config_test/config_test.module b/core/modules/config/tests/config_test/config_test.module index c17d688..9510aba 100644 --- a/core/modules/config/tests/config_test/config_test.module +++ b/core/modules/config/tests/config_test/config_test.module @@ -11,16 +11,6 @@ require_once dirname(__FILE__) . '/config_test.hooks.inc'; /** - * Loads a ConfigTest object. - * - * @param string $id - * The ID of the ConfigTest object to load. - */ -function config_test_load($id) { - return entity_load('config_test', $id); -} - -/** * Implements hook_cache_flush(). */ function config_test_cache_flush() { diff --git a/core/modules/config/tests/config_test/src/ConfigTestForm.php b/core/modules/config/tests/config_test/src/ConfigTestForm.php index 0967206..ac0bb17 100644 --- a/core/modules/config/tests/config_test/src/ConfigTestForm.php +++ b/core/modules/config/tests/config_test/src/ConfigTestForm.php @@ -8,7 +8,9 @@ namespace Drupal\config_test; use Drupal\Core\Entity\EntityForm; +use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Form\FormStateInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Form controller for the test config edit forms. @@ -16,6 +18,32 @@ class ConfigTestForm extends EntityForm { /** + * The entity query. + * + * @var \Drupal\Core\Entity\Query\QueryFactory + */ + protected $entityQuery; + + /** + * Constructs a new ConfigTestForm. + * + * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query + * The entity query. + */ + public function __construct(QueryFactory $entity_query) { + $this->entityQuery = $entity_query; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.query') + ); + } + + /** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { @@ -33,7 +61,7 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $entity->id(), '#required' => TRUE, '#machine_name' => array( - 'exists' => 'config_test_load', + 'exists' => [$this, 'exists'], 'replace_pattern' => '[^a-z0-9_.]+', ), ); @@ -142,4 +170,25 @@ 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 function exists($entity_id, array $element, FormStateInterface $form_state) { + /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */ + $entity = $form_state->getFormObject()->getEntity(); + return (bool) $this->entityQuery->get($entity->getEntityTypeId()) + ->condition($entity->getEntityType()->getKey('id'), $entity_id) + ->execute(); + } + } diff --git a/core/modules/config_translation/src/Tests/ConfigTranslationOverviewTest.php b/core/modules/config_translation/src/Tests/ConfigTranslationOverviewTest.php index 4c83a38..0916dcd 100644 --- a/core/modules/config_translation/src/Tests/ConfigTranslationOverviewTest.php +++ b/core/modules/config_translation/src/Tests/ConfigTranslationOverviewTest.php @@ -153,6 +153,8 @@ public function testListingPageWithOverrides() { $original_label = 'Default'; $overridden_label = 'Overridden label'; + $config_test_storage = $this->container->get('entity.manager')->getStorage('config_test'); + // Set up an override. $settings['config']['config_test.dynamic.dotted.default']['label'] = (object) array( 'value' => $overridden_label, @@ -161,7 +163,7 @@ public function testListingPageWithOverrides() { $this->writeSettings($settings); // Test that the overridden label is loaded with the entity. - $this->assertEqual(config_test_load('dotted.default')->label(), $overridden_label); + $this->assertEqual($config_test_storage->load('dotted.default')->label(), $overridden_label); // Test that the original label on the listing page is intact. $this->drupalGet('admin/config/regional/config-translation/config_test');