Browse Source

Added view filter.

merge-requests/4/head
Chi 7 years ago
parent
commit
2ae527f6ec
  1. 34
      src/TwigExtension.php
  2. 14
      tests/src/Functional/TwigTweakTest.php
  3. 3
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

34
src/TwigExtension.php

@ -4,7 +4,9 @@ namespace Drupal\twig_tweak;
use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Block\TitleBlockPluginInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Link;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
@ -55,6 +57,7 @@ class TwigExtension extends \Twig_Extension {
new \Twig_SimpleFilter('image_style', [$this, 'imageStyle']),
new \Twig_SimpleFilter('transliterate', [$this, 'transliterate']),
new \Twig_SimpleFilter('check_markup', [$this, 'checkMarkup']),
new \Twig_SimpleFilter('view', [$this, 'view']),
];
// PHP filter should be enabled in settings.php file.
if (Settings::get('twig_tweak_enable_php_filter')) {
@ -141,7 +144,7 @@ class TwigExtension extends \Twig_Extension {
* @param string $entity_type
* The entity type.
* @param mixed $id
* The ID of the entity to render.
* The ID of the entity to build.
* @param string $view_mode
* (optional) The view mode that should be used to render the entity.
* @param string $langcode
@ -574,6 +577,33 @@ class TwigExtension extends \Twig_Extension {
return check_markup($text, $format_id, $langcode, $filter_types_to_skip);
}
/**
* Returns a render array for entity, field list or field item.
*
* @param mixed $object
* The object to build a render array from.
* @param string $view_mode
* (optional) The view mode that should be used to render the field.
* @param string $langcode
* (optional) For which language the entity should be rendered, defaults to
* the current content language.
* @param bool $check_access
* (Optional) Indicates that access check is required.
*
* @return array
* A render array to represent the object.
*/
public function view($object, $view_mode = 'default', $langcode = NULL, $check_access = TRUE) {
if ($object instanceof FieldItemListInterface || $object instanceof FieldItemInterface) {
return $object->view($view_mode);
}
elseif ($object instanceof EntityInterface && (!$check_access || $object->access('view'))) {
return \Drupal::entityTypeManager()
->getViewBuilder($object->getEntityTypeId())
->view($object, $view_mode, $langcode);
}
}
/**
* Evaluates a string of PHP code.
*

14
tests/src/Functional/TwigTweakTest.php

@ -215,6 +215,20 @@ class TwigTweakTest extends BrowserTestBase {
// Test text format.
$xpath = '//div[@class = "tt-check-markup"]';
$this->assertEquals('<b>bold</b> strong', trim($this->xpath($xpath)[0]->getHtml()));
// Test node view.
$xpath = '//div[@class = "tt-node-view"]/article[contains(@class, "node--view-mode-default")]/h2[a/span[text() = "Beta"]]';
$xpath .= '/following-sibling::footer[//h4[text() = "Member for"]]';
$xpath .= '/following-sibling::div[@class = "node__content"]/div/p';
$this->assertByXpath($xpath);
// Field list view.
$xpath = '//div[@class = "tt-field-list-view"]/span[contains(@class, "field--name-title") and text() = "Beta"]';
$this->assertByXpath($xpath);
// Field item view.
$xpath = '//div[@class = "tt-field-item-view" and text() = "Beta"]';
$this->assertByXpath($xpath);
}
/**

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

@ -49,4 +49,7 @@
<div class="tt-image-style">{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}</div>
<div class="tt-transliterate">{{ 'Привет!' | transliterate('ru') }}</div>
<div class="tt-check-markup">{{ '<b>bold</b> <strong>strong</strong>' | check_markup('twig_tweak_test') }}</div>
<div class="tt-node-view">{{ node|view }}</div>
<div class="tt-field-list-view">{{ node.title|view }}</div>
<div class="tt-field-item-view">{{ node.title[0]|view }}</div>
</div>

Loading…
Cancel
Save