Commit f122f874 authored by alexpott's avatar alexpott

Issue #2227869 by joelpittet: Remove magic printing from Attribute class to...

Issue #2227869 by joelpittet: Remove magic printing from Attribute class to make Twig 'without' filter work for attributes.
parent f9f3e7f2
......@@ -112,11 +112,9 @@ public function offsetExists($name) {
public function __toString() {
$return = '';
foreach ($this->storage as $name => $value) {
if (!$value->printed()) {
$rendered = $value->render();
if ($rendered) {
$return .= ' ' . $rendered;
}
$rendered = $value->render();
if ($rendered) {
$return .= ' ' . $rendered;
}
}
return $return;
......
......@@ -66,7 +66,6 @@ public function offsetExists($offset) {
* Implements the magic __toString() method.
*/
public function __toString() {
$this->printed = TRUE;
return String::checkPlain(implode(' ', $this->value));
}
......
......@@ -42,7 +42,6 @@ public function render() {
* Implements the magic __toString() method.
*/
public function __toString() {
$this->printed = TRUE;
return $this->value === FALSE ? '' : String::checkPlain($this->name);
}
......
......@@ -30,7 +30,6 @@ class AttributeString extends AttributeValueBase {
* Implements the magic __toString() method.
*/
public function __toString() {
$this->printed = TRUE;
return String::checkPlain($this->value);
}
......
......@@ -16,13 +16,6 @@
*/
abstract class AttributeValueBase {
/**
* Whether this attribute hsa been printed already.
*
* @var bool
*/
protected $printed = FALSE;
/**
* The value itself.
*
......@@ -60,16 +53,6 @@ public function render() {
}
}
/**
* Whether this attribute hsa been printed already.
*
* @return bool
* TRUE if this attribute has been printed, FALSE otherwise.
*/
public function printed() {
return $this->printed;
}
/**
* Implements the magic __toString() method.
*/
......
......@@ -75,7 +75,7 @@
* @ingroup themeable
*/
#}
<article id="node-{{ node.id }}" class="{{ attributes.class }} clearfix"{{ attributes }}>
<article id="node-{{ node.id }}" class="{{ attributes.class }} clearfix"{{ attributes|without('id', 'class') }}>
{{ title_prefix }}
{% if not page %}
......
......@@ -8,6 +8,7 @@
namespace Drupal\system\Tests\Theme;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Template\Attribute;
/**
* Tests Drupal's Twig filters.
......@@ -19,9 +20,7 @@ class TwigFilterTest extends WebTestBase {
*
* @var array
*/
public static $modules = array(
'twig_theme_test',
);
public static $modules = array('twig_theme_test');
/**
* {@inheritdoc}
......@@ -38,7 +37,21 @@ public static function getInfo() {
* Test Twig "without" filter.
*/
public function testTwigWithoutFilter() {
$this->drupalGet('/twig-theme-test/filter');
$filter_test = array(
'#theme' => 'twig_theme_test_filter',
'#quote' => array(
'content' => array('#markup' => 'You can only find truth with logic if you have already found truth without it.'),
'author' => array('#markup' => 'Gilbert Keith Chesterton'),
'date' => array('#markup' => '1874-1936'),
),
'#attributes' => array(
'id' => 'quotes',
'checked' => TRUE,
'class' => array('red', 'green', 'blue'),
),
);
$rendered = drupal_render($filter_test);
$this->drupalSetContent($rendered);
$elements = array(
array(
......@@ -75,6 +88,38 @@ public function testTwigWithoutFilter() {
</blockquote>',
'message' => '"Marked-up quote" was successfully rendered.',
),
array(
'expected' => '<div><span id="quotes" checked class="red green blue">All attributes:</span></div>',
'message' => 'All attributes printed.',
),
array(
'expected' => '<div><span class="red green blue" id="quotes" checked>Class attributes in front, remainder at the back:</span></div>',
'message' => 'Class attributes printed in the front, the rest in the back.',
),
array(
'expected' => '<div><span id="quotes" checked data-class="red green blue">Class attributes in back, remainder at the front:</span></div>',
'message' => 'Class attributes printed in the back, the rest in the front.',
),
array(
'expected' => '<div><span class="red green blue">Class attributes only:</span></div>',
'message' => 'Class attributes only printed.',
),
array(
'expected' => '<div><span checked id="quotes" class="red green blue">Without boolean attribute.</span></div>',
'message' => 'Boolean attribute printed in the front.',
),
array(
'expected' => '<div><span data-id="quotes" checked class="red green blue">Without string attribute.</span></div>',
'message' => 'Without string attribute in the front.',
),
array(
'expected' => '<div><span checked>Without either nor class attributes.</span></div>',
'message' => 'Attributes printed without id nor class attributes.',
),
array(
'expected' => '<div><span id="quotes" checked class="red green blue">All attributes again.</span></div>',
'message' => 'All attributes printed again.',
),
);
foreach ($elements as $element) {
......
......@@ -19,7 +19,7 @@
{{ styles }}
{{ scripts }}
</head>
<body class="{{ attributes.class }}">
<body{{ attributes }}>
<div class="l-container">
......
......@@ -28,19 +28,4 @@ public function transBlockRender() {
);
}
/**
* Menu callback for filters in a Twig template.
*/
public function testFilterRender() {
return array(
'#theme' => 'twig_theme_test_filter',
'#quote' => array(
'content' => array('#markup' => 'You can only find truth with logic if you have already found truth without it.'),
'author' => array('#markup' => 'Gilbert Keith Chesterton'),
'date' => array('#markup' => '1874-1936'),
),
);
}
}
......@@ -12,3 +12,11 @@
</footer>
</blockquote>
</div>
<div><span{{ attributes }}>All attributes:</span></div>
<div><span class="{{ attributes.class }}"{{ attributes|without('class') }}>Class attributes in front, remainder at the back:</span></div>
<div><span{{ attributes|without('class') }} data-class="{{ attributes.class }}">Class attributes in back, remainder at the front:</span></div>
<div><span class="{{ attributes.class }}">Class attributes only:</span></div>
<div><span {{ attributes.checked }}{{ attributes|without('checked') }}>Without boolean attribute.</span></div>
<div><span data-id="{{ attributes.id }}"{{ attributes|without('id') }}>Without string attribute.</span></div>
<div><span{{ attributes|without('id', 'class') }}>Without either nor class attributes.</span></div>
<div><span{{ attributes }}>All attributes again.</span></div>
......@@ -5,7 +5,7 @@
*/
function twig_theme_test_theme($existing, $type, $theme, $path) {
$items['twig_theme_test_filter'] = array(
'variables' => array('quote' => array()),
'variables' => array('quote' => array(), 'attributes' => array()),
'template' => 'twig_theme_test.filter',
);
$items['twig_theme_test_php_variables'] = array(
......
......@@ -11,10 +11,3 @@ twig_theme_test.trans:
_content: '\Drupal\twig_theme_test\TwigThemeTestController::transBlockRender'
requirements:
_access: 'TRUE'
twig_theme_test.filter:
path: '/twig-theme-test/filter'
defaults:
_content: '\Drupal\twig_theme_test\TwigThemeTestController::testFilterRender'
requirements:
_access: 'TRUE'
......@@ -62,7 +62,7 @@
* @see template_preprocess_comment()
*/
#}
<article class="{{ attributes.class }} clearfix"{{ attributes }} role="article">
<article class="{{ attributes.class }} clearfix" role="article"{{ attributes|without('class', 'role') }}>
<header class="comment-header">
<div class="attribution">
......
......@@ -16,7 +16,7 @@
* @see bartik_preprocess_field()
*/
#}
<div class="{{ attributes.class }} clearfix"{{ attributes }}>
<div class="{{ attributes.class }} clearfix"{{ attributes|without('class') }}>
<h3{{ label_attributes }}>{{ label }}: </h3>
<ul class="links">
{% for delta, item in items %}
......
......@@ -16,7 +16,7 @@
{{ styles }}
{{ scripts }}
</head>
<body class="{{ attributes.class }}"{{ attributes }}>
<body{{ attributes }}>
<a href="#main-content" class="visually-hidden focusable skip-link">
{{ 'Skip to main content'|t }}
......
......@@ -69,7 +69,7 @@
* @see template_preprocess_node()
*/
#}
<article id="node-{{ node.id }}" class="{{ attributes.class}} clearfix"{{ attributes }} role="article">
<article id="node-{{ node.id }}" class="{{ attributes.class }} clearfix" role="article"{{ attributes|without('id', 'class', 'role') }}>
<header>
{{ title_prefix }}
......
......@@ -164,7 +164,12 @@ function twig_render_var($arg) {
* The filtered renderable array.
*/
function twig_without($element) {
$filtered_element = $element;
if ($element instanceof ArrayAccess) {
$filtered_element = clone $element;
}
else {
$filtered_element = $element;
}
$args = func_get_args();
unset($args[0]);
foreach ($args as $arg) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment