Browse Source

Code clean-up

3.1.x
Chi 4 years ago
parent
commit
b0d20f011d
  1. 2
      src/CacheMetadataExtractor.php
  2. 11
      src/TwigTweakExtension.php
  3. 1
      src/UrlExtractor.php
  4. 11
      tests/src/Functional/TwigTweakTest.php
  5. 2
      tests/src/Kernel/CacheMetadataExtractorTest.php
  6. 2
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

2
src/CacheMetadataExtractor.php

@ -40,7 +40,7 @@ class CacheMetadataExtractor {
/** /**
* Extracts cache metadata from renders array. * Extracts cache metadata from renders array.
*/ */
private function extractFromArray(array $build): CacheableMetadata { private static function extractFromArray(array $build): CacheableMetadata {
$cache_metadata = CacheableMetadata::createFromRenderArray($build); $cache_metadata = CacheableMetadata::createFromRenderArray($build);
$keys = Element::children($build); $keys = Element::children($build);
foreach (array_intersect_key($build, array_flip($keys)) as $item) { foreach (array_intersect_key($build, array_flip($keys)) as $item) {

11
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 { 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)) { if (Uuid::isValid($selector)) {
$entities = \Drupal::entityTypeManager()->getStorage($entity_type)->loadByProperties(['uuid' => $selector]); $entities = $storage->loadByProperties(['uuid' => $selector]);
$entity = reset($entities); $entity = reset($entities);
} else { }
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($selector); // Fall back to entity ID.
else {
$entity = $storage->load($selector);
} }
if ($entity) { if ($entity) {

1
src/UrlExtractor.php

@ -9,7 +9,6 @@ use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\file\FileInterface; use Drupal\file\FileInterface;
use Drupal\link\LinkItemInterface; use Drupal\link\LinkItemInterface;
use Drupal\link\Plugin\Field\FieldType\LinkItem;
use Drupal\media\MediaInterface; use Drupal\media\MediaInterface;
use Drupal\media\Plugin\media\Source\OEmbedInterface; use Drupal\media\Plugin\media\Source\OEmbedInterface;

11
tests/src/Functional/TwigTweakTest.php

@ -73,6 +73,7 @@ final class TwigTweakTest extends BrowserTestBase {
$node_values = [ $node_values = [
'title' => 'Alpha', 'title' => 'Alpha',
'uuid' => 'ad1b902a-344f-41d1-8c61-a69f0366dbfa',
'field_image' => [ 'field_image' => [
'target_id' => $image_file->id(), 'target_id' => $image_file->id(),
'alt' => 'Alt text', 'alt' => 'Alt text',
@ -161,6 +162,16 @@ final class TwigTweakTest extends BrowserTestBase {
$xpath .= '/h2/a/span[text() = "Alpha"]'; $xpath .= '/h2/a/span[text() = "Alpha"]';
$this->assertXpath($xpath); $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). // -- Entity add form (unprivileged user).
$xpath = '//div[@class = "tt-entity-add-form"]/form'; $xpath = '//div[@class = "tt-entity-add-form"]/form';
$this->assertSession()->elementNotExists('xpath', $xpath); $this->assertSession()->elementNotExists('xpath', $xpath);

2
tests/src/Kernel/CacheMetadataExtractorTest.php

@ -92,7 +92,7 @@ final class CacheMetadataExtractorTest extends KernelTestBase {
// -- Wrong type. // -- Wrong type.
self::expectErrorMessage('The input should be either instance of Drupal\Core\Cache\CacheableDependencyInterface or array. stdClass was given.'); 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()); $extractor->extractCacheMetadata(new \stdClass());
} }

2
tests/twig_tweak_test/templates/twig-tweak-test.html.twig

@ -33,6 +33,8 @@
<div class="tt-region">{{ drupal_region('sidebar_first') }}</div> <div class="tt-region">{{ drupal_region('sidebar_first') }}</div>
<div class="tt-entity-default">{{ drupal_entity('node', 1) }}</div> <div class="tt-entity-default">{{ drupal_entity('node', 1) }}</div>
<div class="tt-entity-teaser">{{ drupal_entity('node', 1, 'teaser') }}</div> <div class="tt-entity-teaser">{{ drupal_entity('node', 1, 'teaser') }}</div>
<div class="tt-entity-uuid">{{ drupal_entity('node', 'ad1b902a-344f-41d1-8c61-a69f0366dbfa') }}</div>
<div class="tt-entity-uuid-missing">{{ drupal_entity('node', 'zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz') }}</div>
<div class="tt-entity-add-form">{{ drupal_entity_form('node', values={type: 'page'}) }}</div> <div class="tt-entity-add-form">{{ drupal_entity_form('node', values={type: 'page'}) }}</div>
<div class="tt-entity-edit-form">{{ drupal_entity_form('node', 1) }}</div> <div class="tt-entity-edit-form">{{ drupal_entity_form('node', 1) }}</div>
<div class="tt-field">{{ drupal_field('body', 'node', 1) }}</div> <div class="tt-field">{{ drupal_field('body', 'node', 1) }}</div>

Loading…
Cancel
Save