3
  1. 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' => $this->requestHandler->getUrl($webform, $source_entity, 'webform.user.drafts')->toString()];
    return $this->t('You have pending drafts for this webform.') . ' ' . $this->t('<a href=":href">View your pending drafts</a>.', $t_args);
  • This is a legitimate question, why downvote it? – Kevin Apr 23 at 11:56
  • Because the poster himself already stated that it is hardcoded. Obviously you have to change the code / write a patch to alter those messages. Can't see how this question in this broad scope could be helpful for other users. – Hudri Apr 23 at 13:18
  • @Hudri no need for patch, there is a way to alter it. I don't have time to answer this at the moment. – No Sssweat Apr 23 at 13:21
  • Things in t() cant be changed with something like String Overrides anymore or equivalent? – Kevin Apr 23 at 13:21
  • Here is a hint: Altering Existing Services – No Sssweat Apr 23 at 13:22
1

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

<?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->getCustomMessage($key)) {
      return $custom_message;
    }

    $webform = $this->webform;
    $source_entity = $this->sourceEntity;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      case WebformMessageManagerInterface::PREVIOUS_SUBMISSIONS:
        $args = [':href' => $this->requestHandler->getUrl($webform, $source_entity, 'webform.user.submissions')->toString()];
        return $this->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);

  • Thank you for taking the time to answer my question. It works as expected. – Florin Simion Apr 24 at 7:58
1

Extending the WebformMessageManager is the right approach to address the immediate issue/limitation of hardcoded messages.

To address the specific issue of changing the hardcoded WebformMessageManagerInterface::DRAFTS_PREVIOUS message in /webform/src/WebformMessageManager.php, I created Issue #3050884: Allow WebformMessageManagerInterface::DRAFTS_PREVIOUS to be customized

  • Thank you for jrockowitz for creating the ticket. I've been quite busy recently and didn't have time to create it myself. – Florin Simion Apr 26 at 11:53
  • There is now a patch with test coverage. – jrockowitz Apr 26 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.