Commit aecc5a4a authored by alexpott's avatar alexpott

Issue #2465751 by andypost, JeroenT, RavindraSingh, tim.plunkett, Berdir:...

Issue #2465751 by andypost, JeroenT, RavindraSingh, tim.plunkett, Berdir: Remove config_test_load() and replace with entity_load
parent 53f6c5a4
......@@ -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);
}
}
......@@ -10,16 +10,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().
*/
......
......@@ -8,13 +8,41 @@
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.
*/
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}
*/
......@@ -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();
}
}
......@@ -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');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment