Skip to content
Snippets Groups Projects
Commit 0b057942 authored by Mikael Meulle's avatar Mikael Meulle
Browse files

Merge branch '3490150-2.0.0-rc1-should-stringproptype' into '2.0.x'

Resolve #3490150 "2.0.0 rc1 should stringproptype"

See merge request !309
parents 541fd150 b7d7d627
No related branches found
No related tags found
No related merge requests found
Pipeline #375155 passed
......@@ -152,8 +152,8 @@ _field_formatter_default_2:
output:
props:
string:
rendered_value: "<p>value_text_2</p>"
rendered_value_plain: "<p>value_text_2</p>"
rendered_value: "<p>value_text_2</p>"
rendered_value_plain: "value_text_2"
field_label_default_1:
component:
component_id: ui_patterns_test:test-component
......
......@@ -169,7 +169,7 @@ class AttributesPropType extends PropTypePluginBase {
/**
* {@inheritdoc}
*/
public static function preprocess(mixed $value): mixed {
public static function preprocess(mixed $value, ?array $definition = NULL): mixed {
/*
However, when they land in the template, it is safer to have them as
Attribute objects:
......
......@@ -309,7 +309,7 @@ class LinksPropType extends PropTypePluginBase implements ContainerFactoryPlugin
/**
* {@inheritdoc}
*/
public static function preprocess(mixed $value): mixed {
public static function preprocess(mixed $value, ?array $definition = NULL): mixed {
foreach ($value as $index => &$item) {
if (!is_array($item)) {
continue;
......
......@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\ui_patterns\Plugin\UiPatterns\PropType;
use Drupal\Core\Render\Markup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\ui_patterns\Attribute\PropType;
use Drupal\ui_patterns\PropTypeConversionTrait;
......@@ -57,7 +58,21 @@ class StringPropType extends PropTypePluginBase {
* {@inheritdoc}
*/
public static function normalize(mixed $value, ?array $definition = NULL): mixed {
return static::convertToString($value);
$value = static::convertToString($value);
$contentMediaType = $definition['contentMediaType'] ?? NULL;
return ($contentMediaType === 'text/plain') ? strip_tags($value) : $value;
}
/**
* {@inheritdoc}
*/
public static function preprocess(mixed $value, ?array $definition = NULL): mixed {
$value = parent::preprocess($value, $definition);
$contentMediaType = $definition['contentMediaType'] ?? NULL;
if ($contentMediaType !== 'text/plain') {
return Markup::create($value);
}
return $value;
}
}
......@@ -62,9 +62,14 @@ interface PropTypeInterface extends WithJsonSchemaInterface, PluginInspectionInt
* Called after the validation, before being sent to the template, in order to
* ease the work of template owners.
*
* @param mixed $value
* The value to preprocess.
* @param array|null $definition
* The prop type definition.
*
* @return mixed
* The processed prop type value.
*/
public static function preprocess(mixed $value): mixed;
public static function preprocess(mixed $value, ?array $definition = NULL): mixed;
}
......@@ -71,7 +71,7 @@ abstract class PropTypePluginBase extends PluginBase implements PropTypeInterfac
/**
* {@inheritdoc}
*/
public static function preprocess(mixed $value): mixed {
public static function preprocess(mixed $value, ?array $definition = NULL): mixed {
return $value;
}
......
......@@ -150,7 +150,7 @@ class TwigExtension extends AbstractExtension {
continue;
}
$prop_type = $props[$variable]['ui_patterns']['type_definition'];
$context[$variable] = $prop_type->preprocess($value);
$context[$variable] = $prop_type->preprocess($value, $props[$variable]);
}
}
......
......@@ -10,6 +10,10 @@ props:
string:
title: "String"
type: "string"
string_plain:
title: "String"
type: "string"
contentMediaType: "text/plain"
integer:
title: "Integer"
type: "integer"
......
......@@ -7,6 +7,9 @@
<div class="ui-patterns-props-string">
{{ string }}
</div>
<div class="ui-patterns-props-string_plain">
{{ string_plain }}
</div>
<div class="ui-patterns-props-integer">
{{ integer }}
</div>
......
......@@ -37,6 +37,15 @@ class StringPropTypeTest extends PropTypeNormalizationTestBase {
$this->runRenderPropTest('string', ["value" => $value, "rendered_value" => $rendered_value]);
}
/**
* Test rendered component with prop and contentMediaType.
*
* @dataProvider renderingTestsStringPlain
*/
public function testRenderingStringPlain(mixed $value, mixed $rendered_value) : void {
$this->runRenderPropTest('string_plain', ["value" => $value, "rendered_value" => $rendered_value]);
}
/**
* Provides data for testNormalization.
*/
......@@ -63,15 +72,39 @@ class StringPropTypeTest extends PropTypeNormalizationTestBase {
],
"string with link" => [
Link::fromTextAndUrl(Markup::create("test"), Url::fromUri("https://drupal.org")),
'<div class="ui-patterns-props-string">&lt;a href=&quot;https://drupal.org&quot;&gt;test&lt;/a&gt;</div>',
'<div class="ui-patterns-props-string"><a href="https://drupal.org">test</a></div>',
],
"html string" => [
'<form><input type="checkbox" /></form><b>test</b>',
'<div class="ui-patterns-props-string"><form><input type="checkbox" /></form><b>test</b></div>',
],
"html markup object" => [
Markup::create('<form><input type="checkbox" /></form><b>test</b>'),
'<div class="ui-patterns-props-string"><form><input type="checkbox" /></form><b>test</b></div>',
],
];
}
/**
* Provides data for testNormalization.
*/
public static function renderingTestsStringPlain() : array {
return [
"null value" => [
NULL,
'<div class="ui-patterns-props-string_plain"></div>',
],
"string with link" => [
Link::fromTextAndUrl(Markup::create("test"), Url::fromUri("https://drupal.org")),
'<div class="ui-patterns-props-string_plain">test</div>',
],
"html string" => [
"<b>test</b>",
'<div class="ui-patterns-props-string">&lt;b&gt;test&lt;/b&gt;</div>',
'<div class="ui-patterns-props-string_plain">test</div>',
],
"html markup object" => [
Markup::create("<b>test</b>"),
'<div class="ui-patterns-props-string">&lt;b&gt;test&lt;/b&gt;</div>',
'<div class="ui-patterns-props-string_plain">test</div>',
],
];
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment