Skip to content
Snippets Groups Projects
Commit 3d2ea7ca authored by Antonio De Marco's avatar Antonio De Marco
Browse files

Add initial code for multiple sources handling #81

parent 6bfc96f9
No related branches found
No related tags found
No related merge requests found
......@@ -84,7 +84,19 @@ class Pattern extends RenderElement {
$fields = $element['#fields'];
unset($element['#fields']);
foreach ($fields as $name => $field) {
$element["#{$name}"] = $field;
$key = '#' . $name;
// This guarantees backward compatibility: single sources be single.
if (count($field) == 1) {
$element[$key] = reset($field);
}
else {
// Render multiple sources with "patterns_destination" template.
$element[$key]['#sources'] = $field;
$element[$key]['#context']['pattern'] = $element['#id'];
$element[$key]['#context']['field'] = $name;
$element[$key]['#theme'] = 'patterns_destination';
}
}
}
else {
......
{#
/**
* @file
* Render destination in case of multiple sources mapped to it.
*/
#}
{% if sources is not empty %}
{% for source in sources %}
{{ source }}
{% endfor %}
{% endif %}
......@@ -135,4 +135,14 @@ abstract class AbstractUiPatternsTest extends UnitTestCase {
return $loader;
}
/**
* Get fixtures base path.
*
* @return string
* Fixtures base path.
*/
protected function getFixturePath() {
return realpath(dirname(__FILE__) . '/../fixtures');
}
}
<?php
namespace Drupal\Tests\ui_patterns\Unit;
namespace Drupal\Tests\ui_patterns\Unit\Element;
use function bovigo\assert\assert;
use function bovigo\assert\predicate\equals;
use Drupal\Component\Serialization\Yaml;
use Drupal\Tests\ui_patterns\Unit\AbstractUiPatternsTest;
use Drupal\ui_patterns\Element\PatternPreview;
/**
......@@ -20,7 +21,7 @@ class PatternPreviewTest extends AbstractUiPatternsTest {
* @covers ::getPreviewMarkup
*/
public function testPreviewMarkup() {
$assertions = Yaml::decode(file_get_contents(dirname(__FILE__) . '/../fixtures/preview_markup.yml'));
$assertions = Yaml::decode(file_get_contents($this->getFixturePath() . '/preview_markup.yml'));
foreach ($assertions as $assertion) {
$result = PatternPreview::getPreviewMarkup($assertion['actual']);
assert($assertion['expected'], equals($result));
......
<?php
namespace Drupal\Tests\ui_patterns\Unit\Element;
use function bovigo\assert\assert;
use function bovigo\assert\predicate\equals;
use Drupal\Component\Serialization\Yaml;
use Drupal\Tests\ui_patterns\Unit\AbstractUiPatternsTest;
use Drupal\ui_patterns\Element\Pattern;
/**
* @coversDefaultClass \Drupal\ui_patterns\Element\Pattern
*
* @group ui_patterns
*/
class PatternTest extends AbstractUiPatternsTest {
/**
* Test getPreviewMarkup.
*
* @covers ::processFields
*
* @dataProvider fieldsProvider
*/
public function testProcessFields($actual, $expected) {
$result = Pattern::processFields($actual);
assert($result, equals($expected));
}
/**
* Data provider.
*/
public function fieldsProvider() {
return Yaml::decode(file_get_contents($this->getFixturePath() . '/pattern_element_fields.yml'));
}
}
-
actual:
'#fields':
pattern_field:
source:
'#markup': 'My markup.'
expected:
'#pattern_field':
'#markup': 'My markup.'
-
actual:
'#id': pattern_id
'#fields':
pattern_field:
source1:
'#markup': 'My markup 1.'
source2:
'#markup': 'My markup 2.'
expected:
'#id': pattern_id
'#pattern_field':
'#sources':
source1:
'#markup': 'My markup 1.'
source2:
'#markup': 'My markup 2.'
'#context':
pattern: pattern_id
field: pattern_field
'#theme': patterns_destination
......@@ -21,6 +21,9 @@ function ui_patterns_theme() {
'patterns_meta_information' => [
'variables' => ['pattern' => NULL],
],
'patterns_destination' => [
'variables' => ['sources' => NULL, 'context' => []],
],
] + UiPatterns::getManager()->hookTheme();
}
......@@ -33,6 +36,16 @@ function ui_patterns_theme_suggestions_alter(array &$suggestions, array $variabl
}
}
/**
* Implements hook_theme_suggestions_HOOK()
*/
function ui_patterns_theme_suggestions_patterns_destination(array $variables) {
$suggestions = [];
$suggestions[] = 'patterns_destination__' . $variables['context']['pattern'];
$suggestions[] = 'patterns_destination__' . $variables['context']['pattern'] . '__' . $variables['context']['field'];
return $suggestions;
}
/**
* Implements hook_library_info_build()
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment