You are here

public function MessageForm::submitForm in Message UI 8

Updates the message object by processing the submitted values.

Overrides ContentEntityForm::submitForm

File

src/Form/MessageForm.php, line 202

Class

MessageForm
Form controller for the message_ui entity edit forms.

Namespace

Drupal\message_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Build the node object from the submitted values.
  parent::submitForm($form, $form_state);

  /* @var $message Message */
  $message = $this->entity;

  // Set message owner.
  $uid = $form_state
    ->getValue('uid');
  if (is_array($uid) && !empty($uid[0]['target_id'])) {
    $message
      ->setOwnerId($uid[0]['target_id']);
  }

  // Get the tokens to be replaced and prepare for replacing.
  $replace_tokens = $form_state
    ->getValue('replace_tokens');
  $token_actions = empty($replace_tokens) ? [] : $replace_tokens;

  // Get the message args and replace tokens.
  if ($args = $message
    ->getArguments()) {
    if (!empty($token_actions) && $token_actions != 'no_update') {

      // Loop through the arguments of the message.
      foreach (array_keys($args) as $token) {
        if ($token_actions == 'update') {

          // Get the hard coded value of the message and him in the message.
          $token_name = str_replace([
            '@{',
            '}',
          ], [
            '[',
            ']',
          ], $token);
          $token_service = \Drupal::token();
          $value = $token_service
            ->replace($token_name, [
            'message' => $message,
          ]);
        }
        else {

          // Hard coded value given from the user.
          $value = $form_state
            ->getValue($token);
        }
        $args[$token] = $value;
      }
    }
  }
  $this->entity
    ->setArguments($args);
}