Skip to content

Commit 2f4df51

Browse files
committed
Fix entity retrieve from url.
1 parent 206391c commit 2f4df51

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/Codeception/Module/DrupalEntity.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Codeception\Module;
44

55
use Codeception\Module;
6-
use Drupal\Core\Entity\ContentEntityInterface;
76
use Drupal\Core\Url;
87
use Codeception\TestCase;
98

@@ -17,6 +16,9 @@
1716
* cleanup_test: true
1817
* cleanup_failed: false
1918
* cleanup_suite: true
19+
* entities:
20+
* - node
21+
* - taxonomy_term.
2022
*
2123
* @package Codeception\Module
2224
*/
@@ -31,6 +33,11 @@ class DrupalEntity extends Module {
3133
'cleanup_test' => TRUE,
3234
'cleanup_failed' => TRUE,
3335
'cleanup_suite' => TRUE,
36+
'entity_registry' => [
37+
'node',
38+
'taxonomy_term',
39+
'media',
40+
],
3441
];
3542

3643
/**
@@ -173,14 +180,18 @@ public function getEntityFromUrl($url) {
173180
if ($parameters = $url->getRouteParameters()) {
174181
// Determine if the current route represents an entity.
175182
foreach ($parameters as $name => $options) {
176-
if (isset($options['type']) && strpos($options['type'], 'entity:') === 0) {
177-
$entity = $url->getParameter($name);
178-
if ($entity instanceof ContentEntityInterface && $entity->hasLinkTemplate('canonical')) {
179-
return $entity;
183+
$entity = $url->getRouteParameters();
184+
if (in_array($name, $this->_getConfig('entities'))) {
185+
try {
186+
$storage = \Drupal::entityTypeManager()->getStorage($name);
187+
$entity = $storage->load($options);
188+
if ($entity) {
189+
return $entity;
190+
}
191+
}
192+
catch (\Exception $e) {
193+
continue;
180194
}
181-
182-
// Since entity was found, no need to iterate further.
183-
return FALSE;
184195
}
185196
}
186197
}

src/Codeception/Module/DrupalUser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
* - paragraph
2424
* cleanup_test: false
2525
* cleanup_failed: false
26-
* cleanup_suite: true
27-
*
26+
* cleanup_suite: true.
2827
*
2928
* @package Codeception\Module
3029
*/
@@ -69,6 +68,7 @@ class DrupalUser extends Module {
6968
*
7069
* @return \Drupal\user\Entity\User
7170
* User object.
71+
*
7272
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
7373
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
7474
* @throws \Drupal\Core\Entity\EntityStorageException

src/Codeception/Module/DrupalWatchdog.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Codeception\Module;
44

55
use Codeception\Module;
6-
use Codeception\TestCase;
76
use Drupal\Core\Logger\RfcLogLevel;
87
use Drupal\Component\Utility\Xss;
98

0 commit comments

Comments
 (0)