You are here

class WebformSubmissionController in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Controller/WebformSubmissionController.php \Drupal\webform\Controller\WebformSubmissionController

Provides route responses for Webform submissions.

Hierarchy

Expanded class hierarchy of WebformSubmissionController

1 file declares its use of WebformSubmissionController
WebformSubmissionListBuilder.php in src/WebformSubmissionListBuilder.php

File

src/Controller/WebformSubmissionController.php, line 18

Namespace

Drupal\webform\Controller
View source
class WebformSubmissionController extends ControllerBase {

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The webform request handler.
   *
   * @var \Drupal\webform\WebformRequestInterface
   */
  protected $requestHandler;

  /**
   * The webform token manager.
   *
   * @var \Drupal\webform\WebformTokenManagerInterface
   */
  protected $tokenManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->renderer = $container
      ->get('renderer');
    $instance->requestHandler = $container
      ->get('webform.request');
    $instance->tokenManager = $container
      ->get('webform.token_manager');
    return $instance;
  }

  /**
   * Toggle webform submission sticky.
   *
   * @param \Drupal\webform\WebformSubmissionInterface $webform_submission
   *   A webform submission.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   An Ajax response that toggle the sticky icon.
   */
  public function sticky(WebformSubmissionInterface $webform_submission) {

    // Toggle sticky.
    $webform_submission
      ->setSticky(!$webform_submission
      ->isSticky())
      ->save();

    // Get selector.
    $selector = '#webform-submission-' . $webform_submission
      ->id() . '-sticky';
    $response = new AjaxResponse();

    // Update sticky.
    $response
      ->addCommand(new HtmlCommand($selector, static::buildSticky($webform_submission)));

    // Announce sticky status.
    $t_args = [
      '@label' => $webform_submission
        ->label(),
    ];
    $text = $webform_submission
      ->isSticky() ? $this
      ->t('@label flagged/starred.', $t_args) : $this
      ->t('@label unflagged/unstarred.', $t_args);
    $response
      ->addCommand(new AnnounceCommand($text));
    return $response;
  }

  /**
   * Toggle webform submission locked.
   *
   * @param \Drupal\webform\WebformSubmissionInterface $webform_submission
   *   A webform submission.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   An Ajax response that toggle the lock icon.
   */
  public function locked(WebformSubmissionInterface $webform_submission) {

    // Toggle locked.
    $webform_submission
      ->setLocked(!$webform_submission
      ->isLocked())
      ->save();

    // Get selector.
    $selector = '#webform-submission-' . $webform_submission
      ->id() . '-locked';
    $response = new AjaxResponse();

    // Update lock.
    $response
      ->addCommand(new HtmlCommand($selector, static::buildLocked($webform_submission)));

    // Announce lock status.
    $t_args = [
      '@label' => $webform_submission
        ->label(),
    ];
    $text = $webform_submission
      ->isLocked() ? $this
      ->t('@label locked.', $t_args) : $this
      ->t('@label unlocked.', $t_args);
    $response
      ->addCommand(new AnnounceCommand($text));
    return $response;
  }

  /**
   * Build sticky icon.
   *
   * @param \Drupal\webform\WebformSubmissionInterface $webform_submission
   *   A webform submission.
   *
   * @return \Drupal\Component\Render\FormattableMarkup
   *   Sticky icon.
   */
  public static function buildSticky(WebformSubmissionInterface $webform_submission) {
    $t_args = [
      '@label' => $webform_submission
        ->label(),
    ];
    $args = [
      '@state' => $webform_submission
        ->isSticky() ? 'on' : 'off',
      '@label' => $webform_submission
        ->isSticky() ? t('Unstar/Unflag @label', $t_args) : t('Star/flag @label', $t_args),
    ];
    return new FormattableMarkup('<span class="webform-icon webform-icon-sticky webform-icon-sticky--@state"></span><span class="visually-hidden">@label</span>', $args);
  }

  /**
   * Build locked icon.
   *
   * @param \Drupal\webform\WebformSubmissionInterface $webform_submission
   *   A webform submission.
   *
   * @return \Drupal\Component\Render\FormattableMarkup
   *   Locked icon.
   */
  public static function buildLocked(WebformSubmissionInterface $webform_submission) {
    $t_args = [
      '@label' => $webform_submission
        ->label(),
    ];
    $args = [
      '@state' => $webform_submission
        ->isLocked() ? 'on' : 'off',
      '@label' => $webform_submission
        ->isLocked() ? t('Unlock @label', $t_args) : t('Lock @label', $t_args),
    ];
    return new FormattableMarkup('<span class="webform-icon webform-icon-lock webform-icon-locked--@state"></span><span class="visually-hidden">@label</span>', $args);
  }

  /**
   * Returns a webform submissions's access denied page.
   *
   * @param \Drupal\webform\WebformInterface $webform
   *   The webform.
   * @param \Drupal\webform\WebformSubmissionInterface $webform_submission
   *   A webform submission.
   *
   * @return array
   *   A renderable array containing an access denied page.
   */
  public function accessDenied(WebformInterface $webform, WebformSubmissionInterface $webform_submission) {

    // Message.
    $config = $this
      ->config('webform.settings');
    $message = $webform
      ->getSetting('submission_access_denied_message') ?: $config
      ->get('settings.default_submission_access_denied_message');
    $message = $this->tokenManager
      ->replace($message, $webform_submission);

    // Attributes.
    $attributes = $webform
      ->getSetting('submission_access_denied_attributes');
    $attributes['class'][] = 'webform-submission-access-denied';

    // Build message.
    $build = [
      '#type' => 'container',
      '#attributes' => $attributes,
      'message' => WebformHtmlEditor::checkMarkup($message),
    ];

    // Add config and webform to cache contexts.
    $this->renderer
      ->addCacheableDependency($build, $config);
    $this->renderer
      ->addCacheableDependency($build, $webform);
    return $build;
  }

  /**
   * Returns a webform 's access denied title.
   *
   * @param \Drupal\webform\WebformInterface $webform
   *   The webform.
   *
   * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
   *   The webform's access denied title.
   */
  public function accessDeniedTitle(WebformInterface $webform) {
    return $webform
      ->getSetting('submission_access_denied_title') ?: $this
      ->t('Access denied');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.
WebformSubmissionController::$renderer protected property The renderer service.
WebformSubmissionController::$requestHandler protected property The webform request handler.
WebformSubmissionController::$tokenManager protected property The webform token manager.
WebformSubmissionController::accessDenied public function Returns a webform submissions's access denied page.
WebformSubmissionController::accessDeniedTitle public function Returns a webform 's access denied title.
WebformSubmissionController::buildLocked public static function Build locked icon.
WebformSubmissionController::buildSticky public static function Build sticky icon.
WebformSubmissionController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
WebformSubmissionController::locked public function Toggle webform submission locked.
WebformSubmissionController::sticky public function Toggle webform submission sticky.