class MessageTemplate in Message 8
Defines the Message template entity class.
Plugin annotation
@ConfigEntityType(
id = "message_template",
label = @Translation("Message template"),
label_plural = @Translation("message templates"),
label_singular = @Translation("message template"),
label_count = @PluralTranslation(
singular="@count message template",
plural="@count message templates"
),
config_prefix = "template",
bundle_of = "message",
entity_keys = {
"id" = "template",
"label" = "label",
"langcode" = "langcode",
},
admin_permission = "administer message templates",
handlers = {
"form" = {
"add" = "Drupal\message\Form\MessageTemplateForm",
"edit" = "Drupal\message\Form\MessageTemplateForm",
"delete" = "Drupal\message\Form\MessageTemplateDeleteConfirm"
},
"list_builder" = "Drupal\message\MessageTemplateListBuilder",
"view_builder" = "Drupal\message\MessageViewBuilder",
},
links = {
"add-form" = "/admin/structure/message/template/add",
"edit-form" = "/admin/structure/message/manage/{message_template}",
"delete-form" = "/admin/structure/message/delete/{message_template}"
},
config_export = {
"template",
"label",
"langcode",
"description",
"text",
"settings",
"status"
}
)
Hierarchy
- class \Drupal\Core\Entity\EntityBase implements EntityInterface uses RefinableCacheableDependencyTrait, DependencySerializationTrait
- class \Drupal\Core\Config\Entity\ConfigEntityBase implements ConfigEntityInterface uses SynchronizableEntityTrait, PluginDependencyTrait
- class \Drupal\Core\Config\Entity\ConfigEntityBundleBase
- class \Drupal\message\Entity\MessageTemplate implements MessageTemplateInterface
- class \Drupal\Core\Config\Entity\ConfigEntityBundleBase
- class \Drupal\Core\Config\Entity\ConfigEntityBase implements ConfigEntityInterface uses SynchronizableEntityTrait, PluginDependencyTrait
Expanded class hierarchy of MessageTemplate
10 files declare their use of MessageTemplate
- DaysTest.php in tests/
src/ Kernel/ Plugin/ MessagePurge/ DaysTest.php - message.module in ./
message.module - API functions to manipulate messages.
- MessageCron.php in tests/
src/ Functional/ MessageCron.php - MessageTemplateCreateTrait.php in tests/
src/ Kernel/ MessageTemplateCreateTrait.php - MessageTemplateDependenciesTest.php in tests/
src/ Kernel/ MessageTemplateDependenciesTest.php
File
- src/
Entity/ MessageTemplate.php, line 58
Namespace
Drupal\message\EntityView source
class MessageTemplate extends ConfigEntityBundleBase implements MessageTemplateInterface {
/**
* The ID of this message template.
*
* @var string
*/
protected $template;
/**
* The UUID of the message template.
*
* @var string
*/
protected $uuid;
/**
* The human-readable name of the message template.
*
* @var string
*/
protected $label;
/**
* A brief description of this message template.
*
* @var string
*/
protected $description;
/**
* The serialised text of the message template.
*
* @var array
*/
protected $text = [];
/**
* Array with the arguments and their replacement value, or callbacks.
*
* The argument keys will be replaced when rendering the message, and it
* should be prefixed by @, %, ! - similar to way it's done in Drupal
* core's t() function.
*
* @var array
*
* @code
*
* // Assuming out message-text is:
* // %user-name created <a href="@message-url">@message-title</a>
*
* $message_template->arguments = [
* // Hard code the argument.
* '%user-name' => 'foo',
*
* // Use a callback, and provide callbacks arguments.
* // The following example will call Drupal core's url() function to
* // get the most up-to-date path of message ID 1.
* '@message-url' => [
* 'callback' => 'url',
* 'callback arguments' => ['message/1'],
* ],
*
* // Use callback, but instead of passing callback argument, we will
* // pass the Message entity itself.
* '@message-title' => [
* 'callback' => 'example_bar',
* 'pass message' => TRUE,
* ],
* ];
* @endcode
*
* Arguments assigned to message-template can be overridden by the ones
* assigned to the message.
*/
public $arguments = [];
/**
* Serialized array with misc options.
*
* Purge settings:
* - 'purge_override': TRUE or FALSE override the global behavior.
* "Message settings" will apply. Defaults to FALSE.
* - 'purge_methods': An array of purge method plugin configuration, keyed by
* the plugin ID. An empty array indicates no purge is enabled (although
* global settings will be used unless 'purge_override' is TRUE).
*
* Token settings:
* - 'token replace': Indicate if message's text should be passed
* through token_replace(). defaults to TRUE.
* - 'token options': Array with options to be passed to
* token_replace().
*
* Tokens settings assigned to message-template can be overriden by the ones
* assigned to the message.
*
* @var array
*/
protected $settings = [];
/**
* {@inheritdoc}
*/
public function id() {
return $this->template;
}
/**
* {@inheritdoc}
*/
public function setSettings(array $settings) {
$this->settings = $settings;
return $this;
}
/**
* {@inheritdoc}
*/
public function getSettings() {
return $this->settings;
}
/**
* {@inheritdoc}
*/
public function getSetting($key, $default_value = NULL) {
if (isset($this->settings[$key])) {
return $this->settings[$key];
}
return $default_value;
}
/**
* {@inheritdoc}
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->description;
}
/**
* {@inheritdoc}
*/
public function setLabel($label) {
$this->label = $label;
return $this;
}
/**
* {@inheritdoc}
*/
public function getLabel() {
return $this->label;
}
/**
* {@inheritdoc}
*/
public function setTemplate($template) {
$this->template = $template;
return $this;
}
/**
* {@inheritdoc}
*/
public function getTemplate() {
return $this->template;
}
/**
* {@inheritdoc}
*/
public function setUuid($uuid) {
$this->uuid = $uuid;
return $this;
}
/**
* {@inheritdoc}
*/
public function getUuid() {
return $this->uuid;
}
/**
* {@inheritdoc}
*/
public function getRawText() {
return $this->text;
}
/**
* {@inheritdoc}
*/
public function getText($langcode = Language::LANGCODE_NOT_SPECIFIED, $delta = NULL) {
$text = $this->text;
$language_manager = \Drupal::languageManager();
if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
if ($langcode == Language::LANGCODE_NOT_SPECIFIED) {
// Get the default language code when not specified.
$langcode = $language_manager
->getDefaultLanguage()
->getId();
}
if ($this->langcode !== $langcode) {
$config_translation = $language_manager
->getLanguageConfigOverride($langcode, 'message.template.' . $this
->id());
$translated_text = $config_translation
->get('text');
// If there was no translated text, we return nothing instead of falling
// back to the default language.
$text = $translated_text ?: [];
}
}
// Process text format.
foreach ($text as $key => $item) {
// Call the renderer directly instead of adding a dependency on the Filter
// module's check_markup() function.
// @see check_markup()
$build = [
'#type' => 'processed_text',
'#text' => isset($item['value']) ? $item['value'] : '',
'#format' => $item['format'],
'#langcode' => $langcode,
];
$text[$key] = \Drupal::service('renderer')
->renderPlain($build);
}
if (isset($delta)) {
// Return just the delta if it exists. Always wrap in an array here to
// ensure compatibility with methods calling getText.
return isset($text[$delta]) ? [
$text[$delta],
] : [];
}
return $text;
}
/**
* {@inheritdoc}
*/
public function isLocked() {
return !$this
->isNew();
}
/**
* {@inheritdoc}
*
* @return \Drupal\message\MessageTemplateInterface
* A message template object ready to be save.
*/
public static function create(array $values = []) {
return parent::create($values);
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
// Don't do anything during config sync.
if (\Drupal::isConfigSyncing()) {
return;
}
$this->text = array_filter($this->text, function ($partial) {
// Filter out any partials with an empty `value`.
return !empty($partial['value']);
});
$language_manager = \Drupal::languageManager();
if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
// Set the values for the default site language.
$config_translation = $language_manager
->getLanguageConfigOverride($language_manager
->getDefaultLanguage()
->getId(), 'message.template.' . $this
->id());
$config_translation
->set('text', $this->text);
$config_translation
->save();
}
parent::preSave($storage);
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
parent::calculateDependencies();
$text_format_ids = [];
foreach ($this->text as $item) {
if (!empty($item['format']) && !in_array($item['format'], $text_format_ids, TRUE)) {
$text_format_ids[] = $item['format'];
}
}
// The template depends on the text text format config entities, if any.
if ($text_format_ids) {
$text_format_storage = $this
->entityTypeManager()
->getStorage('filter_format');
foreach ($text_format_storage
->loadMultiple($text_format_ids) as $id => $text_format) {
$this
->addDependency($text_format
->getConfigDependencyKey(), $text_format
->getConfigDependencyName());
}
}
return $this;
}
/**
* {@inheritdoc}
*/
public function onDependencyRemoval(array $dependencies) {
$changed = parent::onDependencyRemoval($dependencies);
foreach ($dependencies['config'] as $text_format) {
if ($text_format instanceof FilterFormatInterface) {
foreach ($this->text as &$item) {
if (!empty($item['format']) && $item['format'] === $text_format
->id()) {
// If a text format, in use by this template, is removed, prevent
// deleting the whole message template and replace the deleted text
// format with the fallback format.
$item['format'] = filter_fallback_format();
$changed = TRUE;
}
}
}
}
return $changed;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableDependencyTrait:: |
protected | property | Cache contexts. | |
CacheableDependencyTrait:: |
protected | property | Cache max-age. | |
CacheableDependencyTrait:: |
protected | property | Cache tags. | |
CacheableDependencyTrait:: |
protected | function | Sets cacheability; useful for value object constructors. | |
ConfigEntityBase:: |
private | property | Whether the config is being deleted by the uninstall process. | |
ConfigEntityBase:: |
protected | property | The language code of the entity's default language. | |
ConfigEntityBase:: |
protected | property | The original ID of the configuration entity. | |
ConfigEntityBase:: |
protected | property | The enabled/disabled status of the configuration entity. | 4 |
ConfigEntityBase:: |
protected | property | Third party entity settings. | |
ConfigEntityBase:: |
protected | property | Trust supplied data and not use configuration schema on save. | |
ConfigEntityBase:: |
protected | property | Information maintained by Drupal core about configuration. | |
ConfigEntityBase:: |
protected | function | Overrides \Drupal\Core\Entity\DependencyTrait:addDependency(). | |
ConfigEntityBase:: |
public | function |
Creates a duplicate of the entity. Overrides EntityBase:: |
1 |
ConfigEntityBase:: |
public | function |
Disables the configuration entity. Overrides ConfigEntityInterface:: |
1 |
ConfigEntityBase:: |
public | function |
Enables the configuration entity. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Returns the value of a property. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Returns the cache tags that should be used to invalidate caches. Overrides EntityBase:: |
1 |
ConfigEntityBase:: |
public | function |
Gets the configuration dependency name. Overrides EntityBase:: |
|
ConfigEntityBase:: |
protected static | function | Gets the configuration manager. | |
ConfigEntityBase:: |
public | function |
Gets the configuration target identifier for the entity. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Gets the configuration dependencies. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Gets the original ID. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
protected | function | Gets the typed config manager. | |
ConfigEntityBase:: |
public | function |
Gets whether on not the data is trusted. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
protected static | function |
Override to never invalidate the individual entities' cache tags; the
config system already invalidates them. Overrides EntityBase:: |
|
ConfigEntityBase:: |
protected | function |
Override to never invalidate the entity's cache tag; the config system
already invalidates it. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Checks whether this entity is installable. Overrides ConfigEntityInterface:: |
2 |
ConfigEntityBase:: |
public | function |
Overrides Entity::isNew(). Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Returns whether this entity is being changed during the uninstall process. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Deprecated way of generating a link to the entity. See toLink(). Overrides EntityBase:: |
|
ConfigEntityBase:: |
public static | function |
Acts on entities before they are deleted and before hooks are invoked. Overrides EntityBase:: |
8 |
ConfigEntityBase:: |
public | function |
Saves an entity permanently. Overrides EntityBase:: |
1 |
ConfigEntityBase:: |
public | function |
Sets the value of a property. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Sets the original ID. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Sets the status of the configuration entity. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
public | function | ||
ConfigEntityBase:: |
public static | function | Helper callback for uasort() to sort configuration entities by weight and label. | 6 |
ConfigEntityBase:: |
public | function |
Returns whether the configuration entity is enabled. Overrides ConfigEntityInterface:: |
4 |
ConfigEntityBase:: |
public | function |
Gets an array of all property values. Overrides EntityBase:: |
2 |
ConfigEntityBase:: |
public | function |
Gets the URL object for the entity. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Sets that the data should be trusted. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
public | function |
Gets the public URL for this entity. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Gets the URL object for the entity. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Constructs an Entity object. Overrides EntityBase:: |
10 |
ConfigEntityBase:: |
public | function |
Overrides EntityBase:: |
4 |
ConfigEntityBundleBase:: |
protected | function | Deletes display if a bundle is deleted. | |
ConfigEntityBundleBase:: |
protected | function | Returns view or form displays for this bundle. | |
ConfigEntityBundleBase:: |
public static | function |
Acts on deleted entities before the delete hook is invoked. Overrides EntityBase:: |
2 |
ConfigEntityBundleBase:: |
public | function |
Acts on a saved entity before the insert or update hook is invoked. Overrides EntityBase:: |
2 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | Aliased as: traitSleep | 1 |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. Aliased as: addDependencyTrait | |
EntityBase:: |
protected | property | Boolean indicating whether the entity should be forced to be new. | |
EntityBase:: |
protected | property | The entity type. | |
EntityBase:: |
protected | property | A typed data object wrapping this entity. | |
EntityBase:: |
public | function |
Checks data value access. Overrides AccessibleInterface:: |
1 |
EntityBase:: |
public | function |
Gets the bundle of the entity. Overrides EntityInterface:: |
1 |
EntityBase:: |
public | function |
Deletes an entity permanently. Overrides EntityInterface:: |
2 |
EntityBase:: |
public | function |
Enforces an entity to be new. Overrides EntityInterface:: |
|
EntityBase:: |
protected | function | Gets the entity manager. | |
EntityBase:: |
protected | function | Gets the entity type bundle info service. | |
EntityBase:: |
protected | function | Gets the entity type manager. | |
EntityBase:: |
public | function |
The cache contexts associated with this object. Overrides CacheableDependencyTrait:: |
|
EntityBase:: |
public | function |
The maximum age for which this object may be cached. Overrides CacheableDependencyTrait:: |
|
EntityBase:: |
public | function |
The cache tags associated with this object. Overrides CacheableDependencyTrait:: |
|
EntityBase:: |
public | function |
Gets the key that is used to store configuration dependencies. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Gets the entity type definition. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Gets the ID of the type of the entity. Overrides EntityInterface:: |
|
EntityBase:: |
protected | function | The list cache tags to invalidate for this entity. | |
EntityBase:: |
public | function |
Gets a typed data object for this entity object. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Indicates if a link template exists for a given key. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Gets the label of the entity. Overrides EntityInterface:: |
6 |
EntityBase:: |
public | function |
Gets the language of the entity. Overrides EntityInterface:: |
1 |
EntityBase:: |
protected | function | Gets the language manager. | |
EntityBase:: |
protected | function | Gets an array link templates. | 1 |
EntityBase:: |
public static | function |
Loads an entity. Overrides EntityInterface:: |
|
EntityBase:: |
public static | function |
Loads one or more entities. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Acts on a created entity before hooks are invoked. Overrides EntityInterface:: |
4 |
EntityBase:: |
public static | function |
Acts on loaded entities. Overrides EntityInterface:: |
2 |
EntityBase:: |
public static | function |
Changes the values of an entity before it is created. Overrides EntityInterface:: |
5 |
EntityBase:: |
public | function |
Gets a list of entities referenced by this entity. Overrides EntityInterface:: |
1 |
EntityBase:: |
public | function |
Generates the HTML for a link to this entity. Overrides EntityInterface:: |
|
EntityBase:: |
public | function |
Gets a list of URI relationships supported by this entity. Overrides EntityInterface:: |
|
EntityBase:: |
protected | function | Gets an array of placeholders for this entity. | 2 |
EntityBase:: |
public | function |
Gets the entity UUID (Universally Unique Identifier). Overrides EntityInterface:: |
1 |
EntityBase:: |
protected | function | Gets the UUID generator. | |
MessageTemplate:: |
public | property | Array with the arguments and their replacement value, or callbacks. | |
MessageTemplate:: |
protected | property | A brief description of this message template. | |
MessageTemplate:: |
protected | property | The human-readable name of the message template. | |
MessageTemplate:: |
protected | property | Serialized array with misc options. | |
MessageTemplate:: |
protected | property | The ID of this message template. | |
MessageTemplate:: |
protected | property | The serialised text of the message template. | |
MessageTemplate:: |
protected | property |
The UUID of the message template. Overrides ConfigEntityBase:: |
|
MessageTemplate:: |
public | function |
Calculates dependencies and stores them in the dependency property. Overrides ConfigEntityBase:: |
|
MessageTemplate:: |
public static | function |
Overrides EntityBase:: |
|
MessageTemplate:: |
public | function |
Get the message template description. Overrides MessageTemplateInterface:: |
|
MessageTemplate:: |
public | function |
Get the message template label. Overrides MessageTemplateInterface:: |
|
MessageTemplate:: |
public | function | ||
MessageTemplate:: |
public | function |
Return a single setting by key. Overrides MessageTemplateInterface:: |
|
MessageTemplate:: |
public | function |
Return the message template settings. Overrides MessageTemplateInterface:: |
|
MessageTemplate:: |
public | function |
Get the message template. Overrides MessageTemplateInterface:: |
|
MessageTemplate:: |
public | function |
Retrieves the configured message text in a certain language. Overrides MessageTemplateInterface:: |
|
MessageTemplate:: |
public | function |
Get the UUID. Overrides MessageTemplateInterface:: |
|
MessageTemplate:: |
public | function |
Gets the identifier. Overrides EntityBase:: |
|
MessageTemplate:: |
public | function |
Check if the message is new. Overrides MessageTemplateInterface:: |
|
MessageTemplate:: |
public | function |
Informs the entity that entities it depends on will be deleted. Overrides ConfigEntityBase:: |
|
MessageTemplate:: |
public | function |
Acts on an entity before the presave hook is invoked. Overrides ConfigEntityBundleBase:: |
|
MessageTemplate:: |
public | function |
Set the message template description. Overrides MessageTemplateInterface:: |
|
MessageTemplate:: |
public | function |
Set the message template label. Overrides MessageTemplateInterface:: |
|
MessageTemplate:: |
public | function |
Set additional settings for the message template. Overrides MessageTemplateInterface:: |
|
MessageTemplate:: |
public | function |
Set the message template. Overrides MessageTemplateInterface:: |
|
MessageTemplate:: |
public | function |
Set the UUID. Overrides MessageTemplateInterface:: |
|
MessageTemplateInterface:: |
public | function | Return text. | |
PluginDependencyTrait:: |
protected | function | Calculates and adds dependencies of a specific plugin instance. | 1 |
PluginDependencyTrait:: |
protected | function | Calculates and returns dependencies of a specific plugin instance. | |
PluginDependencyTrait:: |
protected | function | Wraps the module handler. | 1 |
PluginDependencyTrait:: |
protected | function | Wraps the theme handler. | 1 |
RefinableCacheableDependencyTrait:: |
public | function | 1 | |
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
SynchronizableEntityTrait:: |
protected | property | Whether this entity is being created, updated or deleted through a synchronization process. | |
SynchronizableEntityTrait:: |
public | function | ||
SynchronizableEntityTrait:: |
public | function |