Skip to content

Commit

Permalink
Fix entity retrieve from url.
Browse files Browse the repository at this point in the history
  • Loading branch information
guncha25 committed Sep 25, 2018
1 parent 206391c commit 2f4df51
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
27 changes: 19 additions & 8 deletions src/Codeception/Module/DrupalEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Codeception\Module;

use Codeception\Module;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Url;
use Codeception\TestCase;

Expand All @@ -17,6 +16,9 @@
* cleanup_test: true
* cleanup_failed: false
* cleanup_suite: true
* entities:
* - node
* - taxonomy_term.
*
* @package Codeception\Module
*/
Expand All @@ -31,6 +33,11 @@ class DrupalEntity extends Module {
'cleanup_test' => TRUE,
'cleanup_failed' => TRUE,
'cleanup_suite' => TRUE,
'entity_registry' => [
'node',
'taxonomy_term',
'media',
],
];

/**
Expand Down Expand Up @@ -173,14 +180,18 @@ public function getEntityFromUrl($url) {
if ($parameters = $url->getRouteParameters()) {
// Determine if the current route represents an entity.
foreach ($parameters as $name => $options) {
if (isset($options['type']) && strpos($options['type'], 'entity:') === 0) {
$entity = $url->getParameter($name);
if ($entity instanceof ContentEntityInterface && $entity->hasLinkTemplate('canonical')) {
return $entity;
$entity = $url->getRouteParameters();
if (in_array($name, $this->_getConfig('entities'))) {
try {
$storage = \Drupal::entityTypeManager()->getStorage($name);
$entity = $storage->load($options);
if ($entity) {
return $entity;
}
}
catch (\Exception $e) {
continue;
}

// Since entity was found, no need to iterate further.
return FALSE;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Codeception/Module/DrupalUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
* - paragraph
* cleanup_test: false
* cleanup_failed: false
* cleanup_suite: true
*
* cleanup_suite: true.
*
* @package Codeception\Module
*/
Expand Down Expand Up @@ -69,6 +68,7 @@ class DrupalUser extends Module {
*
* @return \Drupal\user\Entity\User
* User object.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
Expand Down
1 change: 0 additions & 1 deletion src/Codeception/Module/DrupalWatchdog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Codeception\Module;

use Codeception\Module;
use Codeception\TestCase;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Component\Utility\Xss;

Expand Down

0 comments on commit 2f4df51

Please sign in to comment.