New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Store input format for message templates #140
Conversation
03b66a1
to
e963c05
Compare
@amitaibu ready for review! |
b8a4404
to
a4cc1a3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for providing such awesome PRs!
I've added a few questions and comments.
I have tried to create a message-template in Hebrew. It worked, however I didn't find how to export the Hebrew version. What am I'm missing?
@@ -253,9 +262,23 @@ public function getText($langcode = Language::LANGCODE_NOT_SPECIFIED, $delta = N | |||
$text = $translated_text ?: []; | |||
} | |||
|
|||
// Process text format. | |||
foreach ($text as $key => $item) { | |||
// Call the renderer directly instead of adding a dependency on the Filter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this to avoid the a hard dependency on filter module?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, although I've added the text
module as a dependency, which in turn depends on filter
, but I'm inclined to leave this as-is for easier unit testing (otherwise, for any unit test, we'd need to redeclare that function in the test).
@@ -282,7 +305,9 @@ public static function create(array $values = []) { | |||
* {@inheritdoc} | |||
*/ | |||
public function preSave(EntityStorageInterface $storage) { | |||
$this->text = array_filter($this->text); | |||
$this->text = array_filter($this->text, function ($a) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not totaly clear what we do here. also I think $a
should get a more descriptive name. maybe $delta
, or $partial
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, would that be a good time to rename the word partial
to delta
? 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've fixed this (not yet pushed). I think partial
makes more sense than delta
, which is fairly generic.
}); | ||
// Do not store weight, as these are now sorted. | ||
$text = array_map(function ($a) { | ||
unset($a['_weight']); return $a; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be in separate lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a common practice? What's the down side of keeping the weight
(just asking, sorting before saving looks fine)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially during testing, I was having problems storing weight (without running into schema exceptions). Since the text_format
property is a compound one (consisting of value
and format
), I wasn't immediately clear on how to store a third property side-by-side with those in terms of schema declaration. I could add a @todo
here to eventually store weight? However, since they are sorted prior to storage, I'm not sure storing weight buys us anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and also, using _weight
added some magic to the form dragging--when I renamed it to just weight
, the dragging broke, and I didn't dive into why that was the case.
* @param int $delta | ||
* Delta for the element. | ||
* @param array $text | ||
* Array containing 'value' and optionally 'format' for a text_format |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When is the case we won't a format
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When adding a new template, there won't be an existing format
set.
// Increase number of elements if requested, or none exist.
$trigger_element = $form_state->getTriggeringElement();
if (!empty($trigger_element['#add_more']) || !$start_key) {
$form['text'][] = $this->singleElement($start_key + 1, ['value' => '']);
}
The call to filter_default_format()
could be in this code instead of in the singleElement
method...
* element. | ||
* | ||
* @return array | ||
* The single form element. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: A Single ..
@@ -35,6 +35,20 @@ protected function createMessageTemplate($template, $label, $description, array | |||
'clear' => FALSE, | |||
], | |||
]; | |||
|
|||
// If the $text array is simple text values, transform to text + format. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not clear why $detail['format']
can be empty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, we could require all callers to specify a format
, but since this is a trait for making testing easier, I initially decided to support the legacy input of just an array of text strings:
$text = [
'text string one',
'text string two',
];
as opposed to updating all callers to use the now more verbose input:
$text = [
[
'value' => 'text string one',
'format' => 'some_format',
],
...
];
// Language without translation should return empty array. | ||
// @todo Is this correct? Why not return untranslated text? | ||
$default_language = $this->prophesize(Language::class); | ||
$mock_template = $this->prophesize(MessageTemplate::class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$mock_template
=> $message_template
$this->assertEquals($expected, $this->messageTemplate->getText()); | ||
|
||
// Language without translation should return empty array. | ||
// @todo Is this correct? Why not return untranslated text? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behavior is probably an edge case, but I think it right. If for example you have activity stream in English and Hebrew, and the dev didn't translate a template from Hebrew (default language) to English - you, as a non-Heberw speaker, will prefer not to see anything rather than the hebrew.
On the other hand, I can teach you some Hebrew 😜
* | ||
* @covers ::getText | ||
*/ | ||
public function testGetText() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, it's really nice to see this as unit test!
I think it's worth spliting it to smaller methods, as right now i kind of checks everything 😄
I didn't know either, but asked Alex Pott and he showed me that if you run, for instance
|
a4cc1a3
to
f6b8d93
Compare
f6b8d93
to
1d29686
Compare
@amitaibu should be ready for another review! |
I haven't forgotten about this, just missing the time. I'll pickup in a couple of days. |
Bump =) |
Sorry! Just hectic days. So in order for me not to block - go ahead and On Oct 31, 2016 6:45 PM, "Jonathan Hedstrom" notifications@github.com
|
Sounds good. File follow-ups as needed :) |
Related #139