You are here

protected function MessageTemplateCreateTrait::createMessageTemplate in Message 8

Helper function to create and save a message template entity.

Parameters

string $template: The message template.

string $label: The message template label.

string $description: The message template description.

array $text: The text array for the message template.

array $settings: Data overrides.

string $langcode: The language to use.

Return value

\Drupal\message\MessageTemplateInterface A saved message template entity.

12 calls to MessageTemplateCreateTrait::createMessageTemplate()
MessageCheckAndDeleteWorkerTest::testProcessItem in tests/src/Kernel/Plugin/QueueWorker/MessageCheckAndDeleteWorkerTest.php
Tests that items are only deleted when appropriate.
MessageCreateTest::testMessageCreateDefaultValues in tests/src/Functional/MessageCreateTest.php
Tests if message create sets the default uid to currently logged in user.
MessageEntityDelete::setUp in tests/src/Functional/MessageEntityDelete.php
MessageTemplateCrudTest::testCrudEntityType in tests/src/Functional/MessageTemplateCrudTest.php
Creating/reading/updating/deleting the message template entity and test it.
MessageTemplateSuggestionsTest::testMessageThemeHookSuggestions in tests/src/Functional/MessageTemplateSuggestionsTest.php
Tests if template_preprocess_message() generates the correct suggestions.

... See full list

File

tests/src/Kernel/MessageTemplateCreateTrait.php, line 32

Class

MessageTemplateCreateTrait
Trait to assist message template creation for tests.

Namespace

Drupal\Tests\message\Kernel

Code

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;
}