Alter webform messages found in WebformMessageManager List item


Is there a way to change the hardcoded webform messages from /webform/src/WebformMessageManager.php?

for example, the one on line 284

  case WebformMessageManagerInterface::DRAFTS_PREVIOUS:
    $t_args = [':href' =&gt; $this-&gt;requestHandler-&gt;getUrl($webform, $source_entity, 'webform.user.drafts')-&gt;toString()];
    return $this-&gt;t('You have pending drafts for this webform.') . ' ' . $this-&gt;t('&lt;a href=":href"&gt;View your pending drafts&lt;/a&gt;.', $t_args);
The beauty about Services is that they can be overridden to your liking.

So all we have to do is extend the WebformMessageManager so we inherit all of it's properties and methods and simply alter the method you need to modify.

my_module/src/CustomWebformMessageManager.php

&lt;?php

namespace Drupal\my_module;

use Drupal\webform\WebformMessageManager;
use Drupal\webform\WebformMessageManagerInterface;

class CustomWebformMessageManager extends WebformMessageManager implements WebformMessageManagerInterface {

  /**
   * {@inheritdoc}
   */
  public function get($key) {
    // Get custom message from settings.
    if ($custom_message = $this-&gt;getCustomMessage($key)) {
      return $custom_message;
    }

    $webform = $this-&gt;webform;
    $source_entity = $this-&gt;sourceEntity;

    // Get custom message from settings with arguments.
    switch ($key) {
      case WebformMessageManagerInterface::PREVIOUS_SUBMISSION:
        $webform_submission = $this-&gt;entityStorage-&gt;getLastSubmission($webform, $source_entity, $this-&gt;currentUser);
        $args = [':href' =&gt; $this-&gt;requestHandler-&gt;getUrl($webform_submission, $source_entity, 'webform.user.submission')-&gt;toString()];
        return $this-&gt;getCustomMessage('previous_submission_message', $args);

      case WebformMessageManagerInterface::PREVIOUS_SUBMISSIONS:
        $args = [':href' =&gt; $this-&gt;requestHandler-&gt;getUrl($webform, $source_entity, 'webform.user.submissions')-&gt;toString()];
        return $this-&gt;getCustomMessage('previous_submissions_message', $args);
    }

    // Get hard-coded messages.
    switch ($key) {
      case WebformMessageManagerInterface::ADMIN_PAGE:
        return $this-&gt;t('Only webform administrators are allowed to access this page and create new submissions.');

      case WebformMessageManagerInterface::ADMIN_CLOSED:
        $t_args = [':href' =&gt; $webform-&gt;toUrl('settings-form')-&gt;toString()];
        return $this-&gt;t('This webform is &lt;a href=":href"&gt;closed&lt;/a&gt;. Only submission administrators are allowed to access this webform and create new submissions.', $t_args);

      case WebformMessageManagerInterface::ADMIN_ARCHIVED:
        $t_args = [':href' =&gt; $webform-&gt;toUrl('settings')-&gt;toString()];
        return $this-&gt;t('This webform is &lt;a href=":href"&gt;archived&lt;/a&gt;. Only submission administrators are allowed to access this webform and create new submissions.', $t_args);

      case WebformMessageManagerInterface::SUBMISSION_DEFAULT_CONFIRMATION:
        $t_args = ['%form' =&gt; ($source_entity) ? $source_entity-&gt;label() : $webform-&gt;label()];
        return $this-&gt;t('New submission added to %form.', $t_args);

      case WebformMessageManagerInterface::FORM_SAVE_EXCEPTION:
        $t_args = [
          ':handlers_href' =&gt; $webform-&gt;toUrl('handlers')-&gt;toString(),
          ':settings_href' =&gt; $webform-&gt;toUrl('settings')-&gt;toString(),
        ];
        return $this-&gt;t('This webform is currently not saving any submitted data. Please enable the &lt;a href=":settings_href"&gt;saving of results&lt;/a&gt; or add a &lt;a href=":handlers_href"&gt;submission handler&lt;/a&gt; to the webform.', $t_args);

      case WebformMessageManagerInterface::HANDLER_SUBMISSION_REQUIRED:
        $t_args = [':href' =&gt; $webform-&gt;toUrl('handlers')-&gt;toString()];
        return $this-&gt;t('This webform\'s &lt;a href=":href"&gt;submission handlers&lt;/a&gt; requires submissions to be saved to the database.', $t_args);

      case WebformMessageManagerInterface::DRAFT_PREVIOUS:
        $webform_draft = $this-&gt;entityStorage-&gt;loadDraft($webform, $source_entity, $this-&gt;currentUser);
        $t_args = [':href' =&gt; $webform_draft-&gt;getTokenUrl()-&gt;toString()];
        return $this-&gt;t('You have a pending draft for this webform.') . ' ' . $this-&gt;t('&lt;a href=":href"&gt;Load your pending draft&lt;/a&gt;.', $t_args);
      case WebformMessageManagerInterface::DRAFTS_PREVIOUS:
        $t_args = [':href' =&gt; $this-&gt;requestHandler-&gt;getUrl($webform, $source_entity, 'webform.user.drafts')-&gt;toString()];
// ALTER THE LINE YOU WANT
        return $this-&gt;t('MY CUSTOM VIEW PENDING DRAFT MESSAGE.') . ' ' . $this-&gt;t('&lt;a href=":href"&gt;MY CUSTOM VIEW PENDING DRAFT MESSAGE.&lt;/a&gt;.', $t_args);

      case WebformMessageManagerInterface::SUBMISSION_UPDATED:
        $t_args = ['%form' =&gt; ($source_entity) ? $source_entity-&gt;label() : $webform-&gt;label()];
        return $this-&gt;t('Submission updated in %form.', $t_args);

      case WebformMessageManagerInterface::SUBMISSION_TEST:
        return $this-&gt;t("The below webform has been prepopulated with custom/random test data. When submitted, this information &lt;strong&gt;will still be saved&lt;/strong&gt; and/or &lt;strong&gt;sent to designated recipients&lt;/strong&gt;.");

      case WebformMessageManagerInterface::TEMPLATE_PREVIEW:
        $t_args = [':href' =&gt; $webform-&gt;toUrl('duplicate-form')-&gt;toString()];
        return $this-&gt;t('You are previewing the below template, which can be used to &lt;a href=":href"&gt;create a new webform&lt;/a&gt;. &lt;strong&gt;Submitted data will be ignored&lt;/strong&gt;.', $t_args);

      case WebformMessageManagerInterface::PREPOPULATE_SOURCE_ENTITY_TYPE:
      case WebformMessageManagerInterface::PREPOPULATE_SOURCE_ENTITY_REQUIRED:
        return $this-&gt;t('This webform is not available. Please contact the site administrator.');

      case WebformMessageManagerInterface::PREVIOUS_SUBMISSION:
        $webform_submission = $this-&gt;entityStorage-&gt;getLastSubmission($webform, $source_entity, $this-&gt;currentUser);
        $args = [':href' =&gt; $this-&gt;requestHandler-&gt;getUrl($webform_submission, $source_entity, 'webform.user.submission')-&gt;toString()];
        return $this-&gt;getCustomMessage('previous_submission_message', $args);

      case WebformMessageManagerInterface::PREVIOUS_SUBMISSIONS:
        $args = [':href' =&gt; $this-&gt;requestHandler-&gt;getUrl($webform, $source_entity, 'webform.user.submissions')-&gt;toString()];
        return $this-&gt;getCustomMessage('previous_submissions_message', $args);
    }

    return FALSE;
  }

}


my_module/my_module.services.yml

services:
  webform.message_manager: # The service ID you want to alter
    class: Drupal\my_module\CustomWebformMessageManager // Your Custom Class
    arguments: ['@current_user', '@config.factory', '@entity_type.manager', '@logger.channel.webform', '@renderer', '@messenger', '@webform.request', '@webform.token_manager']



  Important: Make sure you give your module a higher weight
  than webform so your service alter runs.


You could use the Modules weight module. 

OR

In Devel's execute php, execute module_set_weight('my_module', 10);