You are here

class WebformRevisionsRequest in Config Entity Revisions 8

Same name and namespace in other branches
  1. 8.2 modules/webform_revisions/src/WebformRevisionsRequest.php \Drupal\webform_revisions\WebformRevisionsRequest
  2. 1.x modules/webform_revisions/src/WebformRevisionsRequest.php \Drupal\webform_revisions\WebformRevisionsRequest

Handles webform requests.

Hierarchy

Expanded class hierarchy of WebformRevisionsRequest

1 string reference to 'WebformRevisionsRequest'
webform_revisions.services.yml in modules/webform_revisions/webform_revisions.services.yml
modules/webform_revisions/webform_revisions.services.yml
1 service uses WebformRevisionsRequest
webform.request in modules/webform_revisions/webform_revisions.services.yml
Drupal\webform_revisions\WebformRevisionsRequest

File

modules/webform_revisions/src/WebformRevisionsRequest.php, line 27

Namespace

Drupal\webform_revisions
View source
class WebformRevisionsRequest extends WebformRequest implements WebformRequestInterface {

  /**
   * The route provider.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $routeProvider;

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The route admin context to determine whether a route is an admin one.
   *
   * @var \Drupal\Core\Routing\AdminContext
   */
  protected $adminContext;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity type repository.
   *
   * @var \Drupal\Core\Entity\EntityTypeRepositoryInterface
   */
  protected $entityTypeRepository;

  /**
   * The webform entity reference manager.
   *
   * @var \Drupal\webform\WebformEntityReferenceManagerInterface
   */
  protected $webformEntityReferenceManager;

  /**
   * Webform source entity plugin manager.
   *
   * @var \Drupal\webform\Plugin\WebformSourceEntityManagerInterface
   */
  protected $webformSourceEntityManager;

  /**
   * Track if the current page is a webform admin route.
   *
   * @var bool
   */
  protected $isAdminRoute;

  /**
   * Constructs a WebformRevisionsRequest object.
   *
   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
   *   The route provider.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Routing\AdminContext $admin_context
   *   The route admin context service.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityTypeRepositoryInterface $entity_type_repository
   *   The entity type repository.
   * @param \Drupal\webform\WebformEntityReferenceManagerInterface $webform_entity_reference_manager
   *   The webform entity reference manager.
   * @param \Drupal\webform\Plugin\WebformSourceEntityManagerInterface $webform_source_entity_manager
   *   The webform source entity plugin manager.
   */
  public function __construct(RouteProviderInterface $route_provider, RequestStack $request_stack, AdminContext $admin_context, RouteMatchInterface $route_match, EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, WebformEntityReferenceManagerInterface $webform_entity_reference_manager, WebformSourceEntityManagerInterface $webform_source_entity_manager) {
    $this->routeProvider = $route_provider;
    $this->request = $request_stack
      ->getCurrentRequest();
    $this->adminContext = $admin_context;
    $this->routeMatch = $route_match;
    $this->entityTypeManager = $entity_type_manager;
    $this->entityTypeRepository = $entity_type_repository;
    $this->webformEntityReferenceManager = $webform_entity_reference_manager;
    $this->webformSourceEntityManager = $webform_source_entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function isWebformAdminRoute() {
    if (isset($this->isAdminRoute)) {
      return $this->isAdminRoute;
    }

    // Make sure the current route is an admin route.
    if (!$this->adminContext
      ->isAdminRoute()) {
      $this->isAdminRoute = FALSE;
      return $this->isAdminRoute;
    }
    $route_name = $this->routeMatch
      ->getRouteName();
    if (in_array($route_name, [
      'entity.webform.canonical',
      'entity.webform_submission.edit_form',
    ])) {
      $this->isAdminRoute = FALSE;
    }
    else {
      $this->isAdminRoute = preg_match('/^(webform\\.|^entity\\.([^.]+\\.)?webform)/', $route_name) ? TRUE : FALSE;
    }
    return $this->isAdminRoute;
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrentSourceEntity($ignored_types = NULL) {

    // TODO: Can we refactor this method away altogether and let all its callers
    // work directly with webform source entity manager?
    return $this->webformSourceEntityManager
      ->getSourceEntity(is_null($ignored_types) ? [] : $ignored_types);
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrentWebform() {
    $source_entity = static::getCurrentSourceEntity('webform');
    if ($source_entity && ($webform = $this->webformEntityReferenceManager
      ->getWebform($source_entity))) {
      return $webform;
    }
    $webform = $this->routeMatch
      ->getParameter('webform');
    if (is_string($webform)) {
      $controller = WebformRevisionsController::create(\Drupal::getContainer());
      $revisionId = $source_entity
        ->get('webform_revision')
        ->getValue();
      if ($revisionId) {
        $revisionId = $revisionId[0]['target_id'];
      }
      else {

        // Get the id by getting the default webform's third party settings.
        $webform = $this->entityTypeManager
          ->getStorage('webform')
          ->load($webform);
        $content_entity_id = $webform
          ->getContentEntityID();

        // Get the first revision id.
        $revisionId = \Drupal::database()
          ->query('SELECT MIN(revision) FROM {config_entity_revisions_revision} c WHERE c.id = :id', [
          ':id' => $content_entity_id,
        ])
          ->fetchField();
      }
      $webform = $controller
        ->loadConfigEntityRevision($revisionId);
    }
    return $webform;
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrentWebformSubmission() {
    $webform_submission = $this->routeMatch
      ->getParameter('webform_submission');
    if (is_string($webform_submission)) {
      $webform_submission = $this->entityTypeManager
        ->getStorage('webform_submission')
        ->load($webform_submission);
    }
    return $webform_submission;
  }

  /**
   * {@inheritdoc}
   */
  public function getWebformEntities() {
    $webform = $this
      ->getCurrentWebform();
    $source_entity = $this
      ->getCurrentSourceEntity('webform');
    return [
      $webform,
      $source_entity,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getWebformSubmissionEntities() {
    $webform_submission = $this->routeMatch
      ->getParameter('webform_submission');
    if (is_string($webform_submission)) {
      $webform_submission = $this->entityTypeManager
        ->getStorage('webform_submission')
        ->load($webform_submission);
    }
    $source_entity = $this
      ->getCurrentSourceEntity('webform_submission');
    return [
      $webform_submission,
      $source_entity,
    ];
  }

  /****************************************************************************/

  // Routing helpers

  /****************************************************************************/

  /**
   * {@inheritdoc}
   */
  public function isAjax() {
    return $this->request
      ->get(AjaxResponseSubscriber::AJAX_REQUEST_PARAMETER) ? TRUE : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getUrl(EntityInterface $webform_entity, EntityInterface $source_entity = NULL, $route_name, array $route_options = []) {
    $route_name = $this
      ->getRouteName($webform_entity, $source_entity, $route_name);
    $route_parameters = $this
      ->getRouteParameters($webform_entity, $source_entity);
    return Url::fromRoute($route_name, $route_parameters, $route_options);
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteName(EntityInterface $webform_entity, EntityInterface $source_entity = NULL, $route_name) {
    if (!$this
      ->hasSourceEntityWebformRoutes($source_entity)) {
      $source_entity = NULL;
    }
    return $this
      ->getBaseRouteName($webform_entity, $source_entity) . '.' . $route_name;
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteParameters(EntityInterface $webform_entity, EntityInterface $source_entity = NULL) {
    if (!$this
      ->hasSourceEntityWebformRoutes($source_entity)) {
      $source_entity = NULL;
    }
    if (static::isValidSourceEntity($webform_entity, $source_entity)) {
      if ($webform_entity instanceof WebformSubmissionInterface) {
        return [
          'webform_submission' => $webform_entity
            ->id(),
          $source_entity
            ->getEntityTypeId() => $source_entity
            ->id(),
        ];
      }
      else {
        return [
          $source_entity
            ->getEntityTypeId() => $source_entity
            ->id(),
        ];
      }
    }
    elseif ($webform_entity instanceof WebformSubmissionInterface) {
      return [
        'webform_submission' => $webform_entity
          ->id(),
        'webform' => $webform_entity
          ->getWebform()
          ->id(),
      ];
    }
    else {
      return [
        $webform_entity
          ->getEntityTypeId() => $webform_entity
          ->id(),
      ];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getBaseRouteName(EntityInterface $webform_entity, EntityInterface $source_entity = NULL) {
    if ($webform_entity instanceof WebformSubmissionInterface) {
      $webform = $webform_entity
        ->getWebform();
    }
    elseif ($webform_entity instanceof WebformInterface) {
      $webform = $webform_entity;
    }
    else {
      throw new \InvalidArgumentException('Webform entity');
    }
    if (static::isValidSourceEntity($webform, $source_entity)) {
      return 'entity.' . $source_entity
        ->getEntityTypeId();
    }
    else {
      return 'entity';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function hasSourceEntityWebformRoutes(EntityInterface $source_entity = NULL) {
    if ($source_entity && $this
      ->routeExists('entity.' . $source_entity
      ->getEntityTypeId() . '.webform_submission.canonical')) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isValidSourceEntity(EntityInterface $webform_entity, EntityInterface $source_entity = NULL) {

    // Validate that source entity exists and can be linked to.
    if (!$source_entity || !$source_entity
      ->hasLinkTemplate('canonical')) {
      return FALSE;
    }

    // Get the webform.
    if ($webform_entity instanceof WebformSubmissionInterface) {
      $webform = $webform_entity
        ->getWebform();
    }
    elseif ($webform_entity instanceof WebformInterface) {
      $webform = $webform_entity;
    }
    else {
      throw new \InvalidArgumentException('Webform entity');
    }

    // Validate that source entity's field target id is the correct webform.
    $webform_target = $this->webformEntityReferenceManager
      ->getWebform($source_entity);
    if ($webform_target && $webform_target
      ->id() == $webform
      ->id()) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }

  /**
   * Check if route exists.
   *
   * @param string $name
   *   Route name.
   *
   * @return bool
   *   TRUE if the route exists.
   *
   * @see http://drupal.stackexchange.com/questions/222591/how-do-i-verify-a-route-exists
   */
  protected function routeExists($name) {
    try {
      $this->routeProvider
        ->getRouteByName($name);
      return TRUE;
    } catch (\Exception $exception) {
      return FALSE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WebformRequest::getCurrentWebformSubmissionUrl public function Get the URL for the current webform submission and source entity. Overrides WebformRequestInterface::getCurrentWebformSubmissionUrl
WebformRequest::getCurrentWebformUrl public function Get the URL for the current webform and source entity. Overrides WebformRequestInterface::getCurrentWebformUrl
WebformRevisionsRequest::$adminContext protected property The route admin context to determine whether a route is an admin one. Overrides WebformRequest::$adminContext
WebformRevisionsRequest::$entityTypeManager protected property The entity type manager. Overrides WebformRequest::$entityTypeManager
WebformRevisionsRequest::$entityTypeRepository protected property The entity type repository. Overrides WebformRequest::$entityTypeRepository
WebformRevisionsRequest::$isAdminRoute protected property Track if the current page is a webform admin route. Overrides WebformRequest::$isAdminRoute
WebformRevisionsRequest::$request protected property The current request. Overrides WebformRequest::$request
WebformRevisionsRequest::$routeMatch protected property The current route match. Overrides WebformRequest::$routeMatch
WebformRevisionsRequest::$routeProvider protected property The route provider. Overrides WebformRequest::$routeProvider
WebformRevisionsRequest::$webformEntityReferenceManager protected property The webform entity reference manager. Overrides WebformRequest::$webformEntityReferenceManager
WebformRevisionsRequest::$webformSourceEntityManager protected property Webform source entity plugin manager. Overrides WebformRequest::$webformSourceEntityManager
WebformRevisionsRequest::getBaseRouteName public function Get the base route name for a form/submission and source entity. Overrides WebformRequest::getBaseRouteName
WebformRevisionsRequest::getCurrentSourceEntity public function Get the current request's source entity. Overrides WebformRequest::getCurrentSourceEntity
WebformRevisionsRequest::getCurrentWebform public function Get webform associated with the current request. Overrides WebformRequest::getCurrentWebform
WebformRevisionsRequest::getCurrentWebformSubmission public function Get webform submission associated with the current request. Overrides WebformRequest::getCurrentWebformSubmission
WebformRevisionsRequest::getRouteName public function Get the route name for a form/submission and source entity. Overrides WebformRequest::getRouteName
WebformRevisionsRequest::getRouteParameters public function Get the route parameters for a form/submission and source entity. Overrides WebformRequest::getRouteParameters
WebformRevisionsRequest::getUrl public function Get the URL for a form/submission and source entity. Overrides WebformRequest::getUrl
WebformRevisionsRequest::getWebformEntities public function Get the webform and source entity for the current request. Overrides WebformRequest::getWebformEntities
WebformRevisionsRequest::getWebformSubmissionEntities public function Get the webform submission and source entity for the current request. Overrides WebformRequest::getWebformSubmissionEntities
WebformRevisionsRequest::hasSourceEntityWebformRoutes public function Check if a source entity has dedicate webform routes. Overrides WebformRequest::hasSourceEntityWebformRoutes
WebformRevisionsRequest::isAjax public function Determine if the current request is an Ajax request. Overrides WebformRequest::isAjax
WebformRevisionsRequest::isValidSourceEntity public function Check if a source entity is attached to a webform. Overrides WebformRequest::isValidSourceEntity
WebformRevisionsRequest::isWebformAdminRoute public function Determine if the current request is a webform admin route. Overrides WebformRequest::isWebformAdminRoute
WebformRevisionsRequest::routeExists protected function Check if route exists. Overrides WebformRequest::routeExists
WebformRevisionsRequest::__construct public function Constructs a WebformRevisionsRequest object. Overrides WebformRequest::__construct