Skip to content
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

Merged
merged 3 commits into from Oct 31, 2016
Merged

Conversation

jhedstrom
Copy link
Collaborator

@jhedstrom jhedstrom commented Oct 18, 2016

@jhedstrom
Copy link
Collaborator Author

@amitaibu ready for review!

Copy link
Member

@amitaibu amitaibu left a 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
Copy link
Member

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?

Copy link
Collaborator Author

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) {
Copy link
Member

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?

Copy link
Member

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? 😄

Copy link
Collaborator Author

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;
Copy link
Member

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

Copy link
Member

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)

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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
Copy link
Member

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?

Copy link
Collaborator Author

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.
Copy link
Member

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.
Copy link
Member

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

Copy link
Collaborator Author

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);
Copy link
Member

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?
Copy link
Member

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() {
Copy link
Member

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 😄

@jhedstrom
Copy link
Collaborator Author

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?

I didn't know either, but asked Alex Pott and he showed me that if you run, for instance drush config-export, the translations appear in a subdirectory called language. If you look at a translated message, you'll see that only the translatable parts are exported:

gP6UkhH6Auo_sNDWtBHqxnTRmoFo2K85SXIivKS3QinnkEg8hg/sync/language/he/message.template.bb.yml
description: translated text here
text:
  -
    value: "<p>TRANSLATION 1</p>\r\n"
    format: basic_html
  -
    value: "<p>TRANSLATION 2</p>\r\n"
    format: restricted_html

@jhedstrom
Copy link
Collaborator Author

@amitaibu should be ready for another review!

@amitaibu
Copy link
Member

I haven't forgotten about this, just missing the time. I'll pickup in a couple of days.

@jhedstrom
Copy link
Collaborator Author

Bump =)

@amitaibu
Copy link
Member

Sorry! Just hectic days. So in order for me not to block - go ahead and
merge all you awesome PRs. I can revisit if needed post-mortem.

On Oct 31, 2016 6:45 PM, "Jonathan Hedstrom" notifications@github.com
wrote:

Bump =)


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#140 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAHrC9wFykh89E9UbKCyRfxZgkzOVlOpks5q5hsIgaJpZM4KaJWO
.

@jhedstrom jhedstrom merged commit d36d044 into 8.x-1.x Oct 31, 2016
@jhedstrom
Copy link
Collaborator Author

Sounds good. File follow-ups as needed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants