You are here

public function Message::save in Message 8

Saves an entity permanently.

When saving existing entities, the entity is assumed to be complete, partial updates of entities are not supported.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Throws

\Drupal\Core\Entity\EntityStorageException In case of failures an exception is thrown.

Overrides EntityBase::save

File

src/Entity/Message.php, line 300

Class

Message
Defines the Message entity class.

Namespace

Drupal\message\Entity

Code

public function save() {
  $token_options = !empty($this->data['token options']) ? $this->data['token options'] : [];
  $tokens = [];

  // Require a valid template when saving.
  if (!$this
    ->getTemplate()) {
    throw new MessageException('No valid template found.');
  }

  // Handle hard coded arguments.
  foreach ($this
    ->getTemplate()
    ->getText() as $text) {
    preg_match_all('/[@|%|\\!]\\{([a-z0-9:_\\-]+?)\\}/i', $text, $matches);
    foreach ($matches[1] as $delta => $token) {
      $output = \Drupal::token()
        ->replace('[' . $token . ']', [
        'message' => $this,
      ], $token_options);
      if ($output != '[' . $token . ']') {

        // Token was replaced and token sanitizes.
        $argument = $matches[0][$delta];
        $tokens[$argument] = Markup::create($output);
      }
    }
  }
  $arguments = $this
    ->getArguments();
  $this
    ->setArguments(array_merge($tokens, $arguments));
  return parent::save();
}