You are here

class YamlFormMessageManager in YAML Form 8

Defines the form message (and login) manager.

Hierarchy

Expanded class hierarchy of YamlFormMessageManager

1 string reference to 'YamlFormMessageManager'
yamlform.services.yml in ./yamlform.services.yml
yamlform.services.yml
1 service uses YamlFormMessageManager
yamlform.message_manager in ./yamlform.services.yml
Drupal\yamlform\YamlFormMessageManager

File

src/YamlFormMessageManager.php, line 17

Namespace

Drupal\yamlform
View source
class YamlFormMessageManager implements YamlFormMessageManagerInterface {
  use StringTranslationTrait;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The configuration object factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Form submission storage.
   *
   * @var \Drupal\yamlform\YamlFormSubmissionStorageInterface
   */
  protected $entityStorage;

  /**
   * The token service.
   *
   * @var \Drupal\Core\Utility\Token
   */
  protected $token;

  /**
   * Logger service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * Form request handler.
   *
   * @var \Drupal\yamlform\YamlFormRequestInterface
   */
  protected $requestHandler;

  /**
   * The token manager.
   *
   * @var \Drupal\yamlform\YamlFormTranslationManagerInterface
   */
  protected $tokenManager;

  /**
   * A form.
   *
   * @var \Drupal\yamlform\YamlFormInterface
   */
  protected $yamlform;

  /**
   * The source entity.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $sourceEntity;

  /**
   * A form submission.
   *
   * @var \Drupal\yamlform\YamlFormSubmissionInterface
   */
  protected $yamlformSubmission;

  /**
   * Constructs a YamlFormMessageManager object.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   Current user.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration object factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity manager.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\yamlform\YamlFormRequestInterface $request_handler
   *   The form request handler.
   * @param \Drupal\yamlform\YamlFormTokenManagerInterface $token_manager
   *   The token manager.
   */
  public function __construct(AccountInterface $current_user, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, LoggerInterface $logger, YamlFormRequestInterface $request_handler, YamlFormTokenManagerInterface $token_manager) {
    $this->currentUser = $current_user;
    $this->configFactory = $config_factory;
    $this->entityStorage = $entity_type_manager
      ->getStorage('yamlform_submission');
    $this->logger = $logger;
    $this->requestHandler = $request_handler;
    $this->tokenManager = $token_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function setYamlForm(YamlFormInterface $yamlform = NULL) {
    $this->yamlform = $yamlform;
  }

  /**
   * {@inheritdoc}
   */
  public function setSourceEntity(EntityInterface $entity = NULL) {
    $this->sourceEntity = $entity;
  }

  /**
   * {@inheritdoc}
   */
  public function setYamlFormSubmission(YamlFormSubmissionInterface $yamlform_submission = NULL) {
    $this->yamlformSubmission = $yamlform_submission;
    if ($yamlform_submission && empty($this->yamlform)) {
      $this->yamlform = $yamlform_submission
        ->getYamlForm();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function display($key, $type = 'status') {
    if ($build = $this
      ->build($key)) {
      drupal_set_message(\Drupal::service('renderer')
        ->renderPlain($build), $type);
      return TRUE;
    }
    else {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function build($key) {
    if ($message = $this
      ->get($key)) {
      return [
        '#markup' => $message,
        '#allowed_tags' => Xss::getAdminTagList(),
      ];
    }
    else {
      return [];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function get($key) {
    $yamlform_settings = $this->yamlform ? $this->yamlform
      ->getSettings() : [];
    $entity = $this->yamlformSubmission ?: $this->yamlform;
    if (!empty($yamlform_settings[$key])) {
      return $this->tokenManager
        ->replace($yamlform_settings[$key], $entity);
    }
    $default_settings = $this->configFactory
      ->get('yamlform.settings')
      ->get('settings');
    if (!empty($default_settings['default_' . $key])) {
      return $this->tokenManager
        ->replace($default_settings['default_' . $key], $entity);
    }
    $yamlform = $this->yamlform;
    $source_entity = $this->sourceEntity;
    $t_args = [
      '%form' => $source_entity ? $source_entity
        ->label() : $yamlform
        ->label(),
      ':handlers_href' => $yamlform
        ->toUrl('handlers-form')
        ->toString(),
      ':settings_href' => $yamlform
        ->toUrl('settings-form')
        ->toString(),
      ':duplicate_href' => $yamlform
        ->toUrl('duplicate-form')
        ->toString(),
    ];
    switch ($key) {
      case YamlFormMessageManagerInterface::ADMIN_ACCESS:
        return $this
          ->t('This form is <a href=":settings_href">closed</a>. Only submission administrators are allowed to access this form and create new submissions.', $t_args);
      case YamlFormMessageManagerInterface::SUBMISSION_DEFAULT_CONFIRMATION:
        return $this
          ->t('New submission added to %form.', $t_args);
      case YamlFormMessageManagerInterface::FORM_SAVE_EXCEPTION:
        return $this
          ->t('This form 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 form.', $t_args);
      case YamlFormMessageManagerInterface::SUBMISSION_PREVIOUS:
        $yamlform_submission = $this->entityStorage
          ->getLastSubmission($yamlform, $source_entity, $this->currentUser);
        $submission_route_name = $this->requestHandler
          ->getRouteName($yamlform_submission, $source_entity, 'yamlform.user.submission');
        $submission_route_parameters = $this->requestHandler
          ->getRouteParameters($yamlform_submission, $source_entity);
        $t_args[':submission_href'] = Url::fromRoute($submission_route_name, $submission_route_parameters)
          ->toString();
        return $this
          ->t('You have already submitted this form.') . ' ' . $this
          ->t('<a href=":submission_href">View your previous submission</a>.', $t_args);
      case YamlFormMessageManagerInterface::SUBMISSIONS_PREVIOUS:
        $submissions_route_name = $this->requestHandler
          ->getRouteName($yamlform, $source_entity, 'yamlform.user.submissions');
        $submissions_route_parameters = $this->requestHandler
          ->getRouteParameters($yamlform, $source_entity);
        $t_args[':submissions_href'] = Url::fromRoute($submissions_route_name, $submissions_route_parameters)
          ->toString();
        return $this
          ->t('You have already submitted this form.') . ' ' . $this
          ->t('<a href=":submissions_href">View your previous submissions</a>.', $t_args);
      case YamlFormMessageManagerInterface::SUBMISSION_UPDATED:
        return $this
          ->t('Submission updated in %form.', $t_args);
      case YamlFormMessageManagerInterface::SUBMISSION_TEST:
        return $this
          ->t("The below form 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>.", $t_args);
      case YamlFormMessageManagerInterface::TEMPLATE_PREVIEW:
        return $this
          ->t('You are previewing the below template, which can be used to <a href=":duplicate_href">create a new form</a>. <strong>Submitted data will be ignored</strong>.', $t_args);
      default:
        return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function log($key, $type = 'warning') {
    $yamlform = $this->yamlform;
    $context = [
      'link' => $yamlform
        ->toLink($this
        ->t('Edit'), 'edit-form')
        ->toString(),
    ];
    switch ($key) {
      case YamlFormMessageManagerInterface::FORM_FILE_UPLOAD_EXCEPTION:
        $message = 'To support file uploads the saving of submission must be enabled. <strong>All uploaded load files would be lost</strong> Please either uncheck \'Disable saving of submissions\' or remove all the file upload elements.';
        break;
      case YamlFormMessageManagerInterface::FORM_SAVE_EXCEPTION:
        $context['%form'] = $yamlform
          ->label();
        $message = '%form is not saving any submitted data and has been disabled.';
        break;
    }
    $this->logger
      ->{$type}($message, $context);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
YamlFormMessageManager::$configFactory protected property The configuration object factory.
YamlFormMessageManager::$currentUser protected property The current user.
YamlFormMessageManager::$entityStorage protected property Form submission storage.
YamlFormMessageManager::$logger protected property Logger service.
YamlFormMessageManager::$requestHandler protected property Form request handler.
YamlFormMessageManager::$sourceEntity protected property The source entity.
YamlFormMessageManager::$token protected property The token service.
YamlFormMessageManager::$tokenManager protected property The token manager.
YamlFormMessageManager::$yamlform protected property A form.
YamlFormMessageManager::$yamlformSubmission protected property A form submission.
YamlFormMessageManager::build public function Build message. Overrides YamlFormMessageManagerInterface::build
YamlFormMessageManager::display public function Display message. Overrides YamlFormMessageManagerInterface::display
YamlFormMessageManager::get public function Get message. Overrides YamlFormMessageManagerInterface::get
YamlFormMessageManager::log public function Log message. Overrides YamlFormMessageManagerInterface::log
YamlFormMessageManager::setSourceEntity public function Set the form source entity whose submissions are being exported. Overrides YamlFormMessageManagerInterface::setSourceEntity
YamlFormMessageManager::setYamlForm public function Set the form used for custom messages and token replacement. Overrides YamlFormMessageManagerInterface::setYamlForm
YamlFormMessageManager::setYamlFormSubmission public function Set the form submission used for token replacement. Overrides YamlFormMessageManagerInterface::setYamlFormSubmission
YamlFormMessageManager::__construct public function Constructs a YamlFormMessageManager object.
YamlFormMessageManagerInterface::ADMIN_ACCESS constant Admin only access.
YamlFormMessageManagerInterface::FORM_CLOSED_MESSAGE constant Form closed.
YamlFormMessageManagerInterface::FORM_CONFIDENTIAL_MESSAGE constant Form confidential.
YamlFormMessageManagerInterface::FORM_EXCEPTION constant Form exception.
YamlFormMessageManagerInterface::FORM_FILE_UPLOAD_EXCEPTION constant Form not able to handle file uploads.
YamlFormMessageManagerInterface::FORM_PREVIEW_MESSAGE constant Form preview.
YamlFormMessageManagerInterface::FORM_SAVE_EXCEPTION constant Form not saving or sending any data.
YamlFormMessageManagerInterface::LIMIT_TOTAL_MESSAGE constant Limit total submission.
YamlFormMessageManagerInterface::LIMIT_USER_MESSAGE constant Limit user submission.
YamlFormMessageManagerInterface::SUBMISSIONS_PREVIOUS constant Submissions previous.
YamlFormMessageManagerInterface::SUBMISSION_CONFIRMATION constant Submission confirmation.
YamlFormMessageManagerInterface::SUBMISSION_DEFAULT_CONFIRMATION constant Default submission confirmation.
YamlFormMessageManagerInterface::SUBMISSION_DRAFT_LOADED constant Submission draft loaded.
YamlFormMessageManagerInterface::SUBMISSION_DRAFT_SAVED constant Submission draft saved.
YamlFormMessageManagerInterface::SUBMISSION_PREVIOUS constant Submission previous.
YamlFormMessageManagerInterface::SUBMISSION_TEST constant Submission test.
YamlFormMessageManagerInterface::SUBMISSION_UPDATED constant Submission updates.
YamlFormMessageManagerInterface::TEMPLATE_PREVIEW constant Submission confirmation.