You are here

function _html_title_form_node_form_submit_replace_node_confirmation_message in HTML Title 8

Replace the node confirmation message.

This function loops over all status messages and replaces the default node confirmation message with our own message that renders the HTML in the node title.

Parameters

array $form: The form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

1 string reference to '_html_title_form_node_form_submit_replace_node_confirmation_message'
html_title_form_node_form_alter in ./html_title.module
Implements hook_form_alter().

File

./html_title.module, line 137
HTML Title module to enable limited HTML tags in title.

Code

function _html_title_form_node_form_submit_replace_node_confirmation_message(array &$form, FormStateInterface $form_state) {
  $messenger = \Drupal::messenger();

  /** @var \Drupal\html_title\HtmlTitleFilter $html_title_filter_service */
  $html_title_filter_service = \Drupal::service('html_title.filter');

  /** @var \Drupal\node\NodeInterface $node */
  $node = $form_state
    ->getFormObject()
    ->getEntity();

  // Retrieve all status messages.
  $status_messages = $messenger
    ->messagesByType('status');
  $t_args = [
    '@type' => node_get_type_label($node),
    '%title' => $node
      ->toLink()
      ->toString(),
  ];
  $overriden_t_args = [
    '@type' => node_get_type_label($node),
    '%title' => Link::fromTextAndUrl($html_title_filter_service
      ->decodeToMarkup($node
      ->label()), $node
      ->toUrl())
      ->toString(),
  ];

  // Loop over all status messages, check if the status messages contains the
  // node created/updated confirmation message and replace it with our custom
  // message. The new message shows the rendered HTML.
  foreach ($status_messages as $key => $status_message) {
    if ((string) $status_message === t('@type %title has been created.', $t_args)
      ->render()) {
      $status_messages[$key] = t('@type %title has been created.', $overriden_t_args);
    }
    elseif ((string) $status_message === t('@type %title has been updated.', $t_args)
      ->render()) {
      $status_messages[$key] = t('@type %title has been updated.', $overriden_t_args);
    }
  }

  // Delete all status messages and re-add them so the overridden message will
  // be shown.
  $messenger
    ->deleteByType('status');
  foreach ($status_messages as $status_message) {
    $messenger
      ->addStatus($status_message);
  }
}