diff --git a/src/CacheMetadataExtractor.php b/src/CacheMetadataExtractor.php index ce37e8f..ed5d4a4 100644 --- a/src/CacheMetadataExtractor.php +++ b/src/CacheMetadataExtractor.php @@ -40,7 +40,7 @@ class CacheMetadataExtractor { /** * Extracts cache metadata from renders array. */ - private function extractFromArray(array $build): CacheableMetadata { + private static function extractFromArray(array $build): CacheableMetadata { $cache_metadata = CacheableMetadata::createFromRenderArray($build); $keys = Element::children($build); foreach (array_intersect_key($build, array_flip($keys)) as $item) { diff --git a/src/TwigTweakExtension.php b/src/TwigTweakExtension.php index 73602ac..99094a4 100644 --- a/src/TwigTweakExtension.php +++ b/src/TwigTweakExtension.php @@ -157,12 +157,15 @@ class TwigTweakExtension extends AbstractExtension { */ public static function drupalEntity(string $entity_type, string $selector, string $view_mode = 'full', ?string $langcode = NULL, bool $check_access = TRUE): array { - // Load entity by id or uuid. + $storage = \Drupal::entityTypeManager()->getStorage($entity_type); + if (Uuid::isValid($selector)) { - $entities = \Drupal::entityTypeManager()->getStorage($entity_type)->loadByProperties(['uuid' => $selector]); + $entities = $storage->loadByProperties(['uuid' => $selector]); $entity = reset($entities); - } else { - $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($selector); + } + // Fall back to entity ID. + else { + $entity = $storage->load($selector); } if ($entity) { diff --git a/src/UrlExtractor.php b/src/UrlExtractor.php index 1fcb032..c6a03a4 100644 --- a/src/UrlExtractor.php +++ b/src/UrlExtractor.php @@ -9,7 +9,6 @@ use Drupal\Core\Field\FieldItemList; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\file\FileInterface; use Drupal\link\LinkItemInterface; -use Drupal\link\Plugin\Field\FieldType\LinkItem; use Drupal\media\MediaInterface; use Drupal\media\Plugin\media\Source\OEmbedInterface; diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index da403c0..b9964d8 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -73,6 +73,7 @@ final class TwigTweakTest extends BrowserTestBase { $node_values = [ 'title' => 'Alpha', + 'uuid' => 'ad1b902a-344f-41d1-8c61-a69f0366dbfa', 'field_image' => [ 'target_id' => $image_file->id(), 'alt' => 'Alt text', @@ -161,6 +162,16 @@ final class TwigTweakTest extends BrowserTestBase { $xpath .= '/h2/a/span[text() = "Alpha"]'; $this->assertXpath($xpath); + // -- Entity by UUID. + $xpath = '//div[@class = "tt-entity-uuid"]'; + $xpath .= '/article[contains(@class, "node")]'; + $xpath .= '/h2/a/span[text() = "Alpha"]'; + $this->assertXpath($xpath); + + // -- Entity by UUID (missing). + $xpath = '//div[@class = "tt-entity-uuid-missing" and . = ""]'; + $this->assertXpath($xpath); + // -- Entity add form (unprivileged user). $xpath = '//div[@class = "tt-entity-add-form"]/form'; $this->assertSession()->elementNotExists('xpath', $xpath); diff --git a/tests/src/Kernel/CacheMetadataExtractorTest.php b/tests/src/Kernel/CacheMetadataExtractorTest.php index f5ac1e1..7ceaf39 100644 --- a/tests/src/Kernel/CacheMetadataExtractorTest.php +++ b/tests/src/Kernel/CacheMetadataExtractorTest.php @@ -92,7 +92,7 @@ final class CacheMetadataExtractorTest extends KernelTestBase { // -- Wrong type. self::expectErrorMessage('The input should be either instance of Drupal\Core\Cache\CacheableDependencyInterface or array. stdClass was given.'); - /** @noinspection PhpParamsInspection */ + /* @noinspection PhpParamsInspection */ $extractor->extractCacheMetadata(new \stdClass()); } diff --git a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig index f08a755..3c3c2dd 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -33,6 +33,8 @@
{{ drupal_region('sidebar_first') }}
{{ drupal_entity('node', 1) }}
{{ drupal_entity('node', 1, 'teaser') }}
+
{{ drupal_entity('node', 'ad1b902a-344f-41d1-8c61-a69f0366dbfa') }}
+
{{ drupal_entity('node', 'zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz') }}
{{ drupal_entity_form('node', values={type: 'page'}) }}
{{ drupal_entity_form('node', 1) }}
{{ drupal_field('body', 'node', 1) }}