You are here

protected function WebformSubmissionListBuilder::initialize in Webform 6.x

Initialize WebformSubmissionListBuilder object.

File

src/WebformSubmissionListBuilder.php, line 299

Class

WebformSubmissionListBuilder
Provides a list controller for webform submission entity.

Namespace

Drupal\webform

Code

protected function initialize() {
  $this->keys = $this->request->query
    ->get('search');
  $this->state = $this->request->query
    ->get('state');
  $this->sourceEntityTypeId = $this->request->query
    ->get('entity');
  list($this->webform, $this->sourceEntity) = $this->requestHandler
    ->getWebformEntities();
  $this->messageManager
    ->setWebform($this->webform);
  $this->messageManager
    ->setSourceEntity($this->sourceEntity);

  /** @var WebformSubmissionStorageInterface $webform_submission_storage */
  $webform_submission_storage = $this
    ->getStorage();
  $route_name = $this->routeMatch
    ->getRouteName();
  $base_route_name = $this->webform ? $this->requestHandler
    ->getBaseRouteName($this->webform, $this->sourceEntity) : '';

  // Set account and state based on the current route.
  switch ($route_name) {
    case 'entity.webform_submission.user':
      $this->account = $this->routeMatch
        ->getParameter('user');
      break;
    case "{$base_route_name}.webform.user.submissions":
      $this->account = $this->currentUser;
      $this->draft = FALSE;
      break;
    case "{$base_route_name}.webform.user.drafts":
      $this->account = $this->currentUser;
      $this->draft = TRUE;
      break;
    default:
      $this->account = NULL;
      break;
  }

  // Initialize submission views and view.
  $this->submissionView = $this->routeMatch
    ->getParameter('submission_view') ?: '';
  $this->submissionViews = $this
    ->getSubmissionViews();
  if ($this->submissionViews && empty($this->submissionView) && $this
    ->isSubmissionViewResultsReplaced()) {
    $this->submissionView = WebformArrayHelper::getFirstKey($this->submissionViews);
  }

  // Check submission view access.
  if ($this->submissionView && !isset($this->submissionViews[$this->submissionView])) {
    $submission_views = $this
      ->getSubmissionViewsConfig();
    if (isset($submission_views[$this->submissionView])) {
      throw new AccessDeniedHttpException();
    }
    else {
      throw new NotFoundHttpException();
    }
  }

  // If there is a submission view, we do not need to initialize
  // the entity list.
  if ($this->submissionView) {
    return;
  }

  // Set default display settings.
  $this->direction = 'desc';
  $this->limit = 20;
  $this->customize = FALSE;
  $this->sort = 'created';
  $this->total = $this
    ->getTotal($this->keys, $this->state, $this->sourceEntityTypeId);
  switch ($route_name) {

    // Display webform submissions which includes properties and elements.
    // @see /admin/structure/webform/manage/{webform}/results/submissions
    // @see /node/{node}/webform/results/submissions
    case "{$base_route_name}.webform.results_submissions":
      $this->columns = $webform_submission_storage
        ->getCustomColumns($this->webform, $this->sourceEntity, $this->account, TRUE);
      $this->sort = $this
        ->getCustomSetting('sort', 'created');
      $this->direction = $this
        ->getCustomSetting('direction', 'desc');
      $this->limit = $this
        ->getCustomSetting('limit', 20);
      $this->format = $this
        ->getCustomSetting('format', $this->format);
      $this->linkType = $this
        ->getCustomSetting('link_type', $this->linkType);
      $this->customize = $this->webform
        ->access('update') || $this->webform
        ->getSetting('results_customize', TRUE);
      if ($this->format['element_format'] === 'raw') {
        foreach ($this->columns as &$column) {
          $column['format'] = 'raw';
          if (isset($column['element'])) {
            $column['element']['#format'] = 'raw';
          }
        }
      }
      break;

    // Display all submissions.
    // @see /admin/structure/webform/submissions/manage
    case 'entity.webform_submission.collection':

      // Display only submission properties.
      // @see /admin/structure/webform/submissions/manage
      $this->columns = $webform_submission_storage
        ->getSubmissionsColumns();
      break;

    // Display user's submissions.
    // @see /user/{user}/submissions
    case 'entity.webform_submission.user':
      $this->columns = $webform_submission_storage
        ->getUsersSubmissionsColumns();
      break;

    // Display user's submissions.
    // @see /webform/{webform}/submissions
    // @see /webform/{webform}/drafts
    // @see /node/{node}/webform/submissions
    // @see /node/{node}/webform/drafts
    case "{$base_route_name}.webform.user.drafts":
    case "{$base_route_name}.webform.user.submissions":
    default:
      $this->columns = $webform_submission_storage
        ->getUserColumns($this->webform, $this->sourceEntity, $this->account, TRUE);
      break;
  }
}