Skip to content

Commit 137ae49

Browse files
chaitanya17paul-m
chaitanya17
authored andcommitted
Issue #2897889 by chaitanya17, Mile23: method/Class QueryFactory is deprecated
1 parent 8ee1a98 commit 137ae49

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

config_entity_example/src/Form/RobotFormBase.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Drupal\config_entity_example\Form;
44

55
use Drupal\Core\Entity\EntityForm;
6-
use Drupal\Core\Entity\Query\QueryFactory;
6+
use Drupal\Core\Entity\EntityStorageInterface;
77
use Drupal\Core\Form\FormStateInterface;
88
use Drupal\Core\Link;
99
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -21,9 +21,9 @@
2121
class RobotFormBase extends EntityForm {
2222

2323
/**
24-
* @var \Drupal\Core\Entity\Query\QueryFactory
24+
* @var \Drupal\Core\Entity\EntityStorageInterface
2525
*/
26-
protected $entityQueryFactory;
26+
protected $entityStorage;
2727

2828
/**
2929
* Construct the RobotFormBase.
@@ -33,11 +33,11 @@ class RobotFormBase extends EntityForm {
3333
* from the container. We later use this query factory to build an entity
3434
* query for the exists() method.
3535
*
36-
* @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
36+
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
3737
* An entity query factory for the robot entity type.
3838
*/
39-
public function __construct(QueryFactory $query_factory) {
40-
$this->entityQueryFactory = $query_factory;
39+
public function __construct(EntityStorageInterface $entity_storage) {
40+
$this->entityStorage = $entity_storage;
4141
}
4242

4343
/**
@@ -55,7 +55,7 @@ public function __construct(QueryFactory $query_factory) {
5555
* pass the factory to our class as a constructor parameter.
5656
*/
5757
public static function create(ContainerInterface $container) {
58-
return new static($container->get('entity.query'));
58+
return new static($container->get('entity_type.manager')->getStorage('robot'));
5959
}
6060

6161
/**
@@ -126,7 +126,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
126126
*/
127127
public function exists($entity_id, array $element, FormStateInterface $form_state) {
128128
// Use the query factory to build a new robot entity query.
129-
$query = $this->entityQueryFactory->get('robot');
129+
$query = $this->entityStorage->getQuery();
130130

131131
// Query the entity ID to see if its in use.
132132
$result = $query->condition('id', $element['#field_prefix'] . $entity_id)

config_entity_example/tests/src/Functional/ConfigEntityExampleTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

33
namespace Drupal\Tests\config_entity_example\Functional;
4+
45
use Drupal\config_entity_example\Entity\Robot;
6+
use Drupal\Core\Url;
57
use Drupal\Tests\BrowserTestBase;
68

79
/**
@@ -131,7 +133,21 @@ public function testConfigEntityExample() {
131133
$assert->pageTextContains($robby_label);
132134
$assert->pageTextContains($robby_machine_name);
133135

136+
// Try to re-submit the same robot, and verify that we see an error message
137+
// and not a PHP error.
138+
$this->drupalPostForm(
139+
Url::fromRoute('entity.robot.add_form'),
140+
[
141+
'label' => $robby_label,
142+
'id' => $robby_machine_name,
143+
'floopy' => TRUE,
144+
],
145+
'Create Robot'
146+
);
147+
$assert->pageTextContains('The machine-readable name is already in use.');
148+
134149
// 6) Verify that required links are present on respective paths.
150+
$this->drupalGet(Url::fromRoute('entity.robot.list'));
135151
$this->assertLinkByHref('/examples/config-entity-example/add');
136152
$this->assertLinkByHref('/examples/config-entity-example/manage/robby_machine_name');
137153
$this->assertLinkByHref('/examples/config-entity-example/manage/robby_machine_name/delete');
@@ -153,6 +169,20 @@ public function testConfigEntityExample() {
153169
[':path' => '/examples/config-entity-example']
154170
);
155171
$this->assertEqual(count($cancel_button), 1, 'Found cancel button linking to list page.');
172+
173+
// Try to submit a robot with a machine name of 'custom'. This is a reserved
174+
// keyword we've disallowed in the form.
175+
$this->drupalPostForm(
176+
Url::fromRoute('entity.robot.add_form'),
177+
[
178+
'label' => 'Custom',
179+
'id' => 'custom',
180+
'floopy' => TRUE,
181+
],
182+
'Create Robot'
183+
);
184+
$assert->pageTextContains('Additionally, it can not be the reserved word "custom".');
185+
156186
}
157187

158188
/**

0 commit comments

Comments
 (0)