trait MessageTemplateCreateTrait in Message 8
Trait to assist message template creation for tests.
Hierarchy
- trait \Drupal\Tests\message\Kernel\MessageTemplateCreateTrait
2 files declare their use of MessageTemplateCreateTrait
- MessageCheckAndDeleteWorkerTest.php in tests/
src/ Kernel/ Plugin/ QueueWorker/ MessageCheckAndDeleteWorkerTest.php - MessageTestBase.php in tests/
src/ Functional/ MessageTestBase.php
File
- tests/
src/ Kernel/ MessageTemplateCreateTrait.php, line 11
Namespace
Drupal\Tests\message\KernelView source
trait MessageTemplateCreateTrait {
/**
* Helper function to create and save a message template entity.
*
* @param string $template
* The message template.
* @param string $label
* The message template label.
* @param string $description
* The message template description.
* @param array $text
* The text array for the message template.
* @param array $settings
* Data overrides.
* @param string $langcode
* The language to use.
*
* @return \Drupal\message\MessageTemplateInterface
* A saved message template entity.
*/
protected function createMessageTemplate($template = NULL, $label = NULL, $description = NULL, array $text = [], array $settings = [], $langcode = Language::LANGCODE_NOT_SPECIFIED) {
$settings += [
'token options' => [
'token replace' => TRUE,
'clear' => FALSE,
],
];
$template = $template ?: mb_strtolower($this
->randomMachineName());
$label = $label ?: $this
->randomString();
$description = $description ?: $this
->randomString();
$text = $text ?: [
$this
->randomString(),
];
// If the $text array is simple text values, transform to text + format.
foreach ($text as $key => $detail) {
if (!is_array($detail)) {
$text[$key] = [
'value' => $detail,
'format' => filter_default_format(),
];
}
elseif (!isset($detail['format'])) {
$text[$key]['format'] = 'plain_text';
}
}
$message_template = MessageTemplate::Create([
'template' => $template,
'label' => $label,
'description' => $description,
'text' => $text,
'settings' => $settings,
'langcode' => $langcode,
]);
$message_template
->save();
return $message_template;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MessageTemplateCreateTrait:: |
protected | function | Helper function to create and save a message template entity. |