You are here

class WebformSubmissionLogController in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_submission_log/src/Controller/WebformSubmissionLogController.php \Drupal\webform_submission_log\Controller\WebformSubmissionLogController

Returns responses for webform submission log routes.

Copied from: \Drupal\dblog\Controller\DbLogController.

Hierarchy

Expanded class hierarchy of WebformSubmissionLogController

File

modules/webform_submission_log/src/Controller/WebformSubmissionLogController.php, line 18

Namespace

Drupal\webform_submission_log\Controller
View source
class WebformSubmissionLogController extends ControllerBase {
  use WebformEntityStorageTrait;

  /**
   * The database service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

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

  /**
   * The webform submission log manager.
   *
   * @var \Drupal\webform_submission_log\WebformSubmissionLogManagerInterface
   */
  protected $logManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {

    /** @var \Drupal\webform_submission_log\Controller\WebformSubmissionLogController $instance */
    $instance = parent::create($container);
    $instance->database = $container
      ->get('database');
    $instance->dateFormatter = $container
      ->get('date.formatter');
    $instance->requestHandler = $container
      ->get('webform.request');
    $instance->logManager = $container
      ->get('webform_submission_log.manager');
    $instance->entityTypeManager = $container
      ->get('entity_type.manager');
    return $instance;
  }

  /**
   * Displays a listing of webform submission log messages.
   *
   * @param \Drupal\webform\WebformInterface|null $webform
   *   A webform.
   * @param \Drupal\webform\WebformSubmissionInterface|null $webform_submission
   *   A webform submission.
   * @param \Drupal\Core\Entity\EntityInterface|null $source_entity
   *   A source entity.
   * @param \Drupal\Core\Session\AccountInterface|null $account
   *   A user account.
   *
   * @return array
   *   A render array as expected by drupal_render().
   */
  public function overview(WebformInterface $webform = NULL, WebformSubmissionInterface $webform_submission = NULL, EntityInterface $source_entity = NULL, AccountInterface $account = NULL) {

    // Entities.
    if (empty($webform) && !empty($webform_submission)) {
      $webform = $webform_submission
        ->getWebform();
    }
    if (empty($source_entity) && !empty($webform_submission)) {
      $source_entity = $webform_submission
        ->getSourceEntity();
    }
    $webform_entity = $webform_submission ?: $webform;

    // Header.
    $header = [];
    $header['lid'] = [
      'data' => $this
        ->t('#'),
      'field' => 'log.lid',
      'sort' => 'desc',
    ];
    if (empty($webform)) {
      $header['webform_id'] = [
        'data' => $this
          ->t('Webform'),
        'field' => 'log.webform_id',
        'class' => [
          RESPONSIVE_PRIORITY_MEDIUM,
        ],
      ];
    }
    if (empty($source_entity) && empty($webform_submission)) {
      $header['entity'] = [
        'data' => $this
          ->t('Submitted to'),
        'class' => [
          RESPONSIVE_PRIORITY_LOW,
        ],
      ];
    }
    if (empty($webform_submission)) {
      $header['sid'] = [
        'data' => $this
          ->t('Submission'),
        'field' => 'log.sid',
      ];
    }
    $header['handler_id'] = [
      'data' => $this
        ->t('Handler'),
      'field' => 'log.handler_id',
    ];
    $header['operation'] = [
      'data' => $this
        ->t('Operation'),
      'field' => 'log.operation',
      'class' => [
        RESPONSIVE_PRIORITY_MEDIUM,
      ],
    ];
    $header['message'] = [
      'data' => $this
        ->t('Message'),
      'field' => 'log.message',
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ];
    $header['uid'] = [
      'data' => $this
        ->t('User'),
      'field' => 'user.name',
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ];
    $header['timestamp'] = [
      'data' => $this
        ->t('Date'),
      'field' => 'log.timestamp',
      'sort' => 'desc',
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ];

    // Query.
    $options = [
      'header' => $header,
      'limit' => 50,
    ];
    $logs = $this->logManager
      ->loadByEntities($webform_entity, $source_entity, $account, $options);

    // Rows.
    $rows = [];
    foreach ($logs as $log) {
      $row = [];
      $row['lid'] = $log->lid;
      if (empty($webform)) {
        $log_webform = $this
          ->getWebformStorage()
          ->load($log->webform_id);
        $row['webform_id'] = $log_webform
          ->toLink($log_webform
          ->label(), 'results-log');
      }
      if (empty($source_entity) && empty($webform_submission)) {
        $entity = NULL;
        if ($log->entity_type && $log->entity_id) {
          $entity_type = $log->entity_type;
          $entity_id = $log->entity_id;
          if ($entity = $this
            ->entityTypeManager()
            ->getStorage($entity_type)
            ->load($entity_id)) {
            $row['entity'] = $entity
              ->hasLinkTemplate('canonical') ? $entity
              ->toLink() : "{$entity_type}:{$entity_id}";
          }
          else {
            $row['entity'] = "{$entity_type}:{$entity_id}";
          }
        }
        else {
          $row['entity'] = '';
        }
      }
      if (empty($webform_submission)) {
        if ($log->sid) {
          $log_webform_submission = $this
            ->getSubmissionStorage()
            ->load($log->sid);
          $row['sid'] = [
            'data' => [
              '#type' => 'link',
              '#title' => $log->sid,
              '#url' => $this->requestHandler
                ->getUrl($log_webform_submission, $source_entity, 'webform_submission.log'),
            ],
          ];
        }
        else {
          $row['sid'] = '';
        }
      }
      $row['handler_id'] = $log->handler_id;
      $row['operation'] = $log->operation;
      $row['message'] = [
        'data' => [
          '#markup' => $this
            ->t($log->message, $log->variables),
        ],
      ];
      $row['uid'] = [
        'data' => [
          '#theme' => 'username',
          '#account' => $this
            ->getEntityStorage('user')
            ->load($log->uid),
        ],
      ];
      $row['timestamp'] = $this->dateFormatter
        ->format($log->timestamp, 'short');
      $rows[] = $row;
    }
    $build['table'] = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#sticky' => TRUE,
      '#empty' => $this
        ->t('No log messages available.'),
    ];
    $build['pager'] = [
      '#type' => 'pager',
    ];
    return $build;
  }

  /**
   * Wrapper that allows the $node to be used as $source_entity.
   */
  public function nodeOverview(WebformInterface $webform = NULL, WebformSubmissionInterface $webform_submission = NULL, EntityInterface $node = NULL) {
    return $this
      ->overview($webform, $webform_submission, $node);
  }

}

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::$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.
WebformEntityStorageTrait::$entityStorageToTypeMappings protected property An associate array of entity type storage aliases.
WebformEntityStorageTrait::$entityTypeManager protected property The entity type manager. 5
WebformEntityStorageTrait::getEntityStorage protected function Retrieves the entity storage.
WebformEntityStorageTrait::getSubmissionStorage protected function Retrieves the webform submission storage.
WebformEntityStorageTrait::getWebformStorage protected function Retrieves the webform storage.
WebformEntityStorageTrait::__get public function Implements the magic __get() method.
WebformSubmissionLogController::$database protected property The database service.
WebformSubmissionLogController::$dateFormatter protected property The date formatter service.
WebformSubmissionLogController::$logManager protected property The webform submission log manager.
WebformSubmissionLogController::$requestHandler protected property The webform request handler.
WebformSubmissionLogController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
WebformSubmissionLogController::nodeOverview public function Wrapper that allows the $node to be used as $source_entity.
WebformSubmissionLogController::overview public function Displays a listing of webform submission log messages.