You are here

class YamlFormRequest in YAML Form 8

Handles form requests.

Hierarchy

Expanded class hierarchy of YamlFormRequest

1 string reference to 'YamlFormRequest'
yamlform.services.yml in ./yamlform.services.yml
yamlform.services.yml
1 service uses YamlFormRequest
yamlform.request in ./yamlform.services.yml
Drupal\yamlform\YamlFormRequest

File

src/YamlFormRequest.php, line 15

Namespace

Drupal\yamlform
View source
class YamlFormRequest implements YamlFormRequestInterface {

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

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

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

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

  /**
   * Constructs a YamlFormSubmissionExporter object.
   *
   * @param \Drupal\Core\Entity\EntityTypeRepositoryInterface $entity_type_repository
   *   The entity type repository.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, RequestStack $request_stack, RouteMatchInterface $route_match) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityTypeRepository = $entity_type_repository;
    $this->request = $request_stack
      ->getCurrentRequest();
    $this->routeMatch = $route_match;
  }

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

    // See if source entity is being set via query string parameters.
    if ($source_entity = $this
      ->getCurrentSourceEntityFromQuery()) {
      return $source_entity;
    }
    $entity_types = $this->entityTypeRepository
      ->getEntityTypeLabels();
    if ($ignored_types) {
      if (is_array($ignored_types)) {
        $entity_types = array_diff_key($entity_types, array_flip($ignored_types));
      }
      else {
        unset($entity_types[$ignored_types]);
      }
    }
    foreach ($entity_types as $entity_type => $entity_label) {
      $entity = $this->routeMatch
        ->getParameter($entity_type);
      if ($entity instanceof EntityInterface) {
        return $entity;
      }
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrentYamlForm() {
    $source_entity = self::getCurrentSourceEntity('yamlform');
    if ($source_entity && method_exists($source_entity, 'hasField') && $source_entity
      ->hasField('yamlform')) {
      return $source_entity->yamlform->entity;
    }
    else {
      return $this->routeMatch
        ->getParameter('yamlform');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getYamlFormEntities() {
    $yamlform = $this
      ->getCurrentYamlForm();
    $source_entity = $this
      ->getCurrentSourceEntity('yamlform');
    return [
      $yamlform,
      $source_entity,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getYamlFormSubmissionEntities() {
    $yamlform_submission = $this->routeMatch
      ->getParameter('yamlform_submission');
    $source_entity = $this
      ->getCurrentSourceEntity('yamlform_submission');
    return [
      $yamlform_submission,
      $source_entity,
    ];
  }

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

  // Routing helpers

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

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

  /**
   * {@inheritdoc}
   */
  public function getRouteParameters(EntityInterface $yamlform_entity, EntityInterface $source_entity = NULL) {
    if (self::isValidSourceEntity($yamlform_entity, $source_entity)) {
      if ($yamlform_entity instanceof YamlFormSubmissionInterface) {
        return [
          'yamlform_submission' => $yamlform_entity
            ->id(),
          $source_entity
            ->getEntityTypeId() => $source_entity
            ->id(),
        ];
      }
      else {
        return [
          $source_entity
            ->getEntityTypeId() => $source_entity
            ->id(),
        ];
      }
    }
    elseif ($yamlform_entity instanceof YamlFormSubmissionInterface) {
      return [
        'yamlform_submission' => $yamlform_entity
          ->id(),
        'yamlform' => $yamlform_entity
          ->getYamlForm()
          ->id(),
      ];
    }
    else {
      return [
        $yamlform_entity
          ->getEntityTypeId() => $yamlform_entity
          ->id(),
      ];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getBaseRouteName(EntityInterface $yamlform_entity, EntityInterface $source_entity = NULL) {
    if ($yamlform_entity instanceof YamlFormSubmissionInterface) {
      $yamlform = $yamlform_entity
        ->getYamlForm();
    }
    elseif ($yamlform_entity instanceof YamlFormInterface) {
      $yamlform = $yamlform_entity;
    }
    else {
      throw new \InvalidArgumentException('Form entity');
    }
    if (self::isValidSourceEntity($yamlform, $source_entity)) {
      return 'entity.' . $source_entity
        ->getEntityTypeId();
    }
    else {
      return 'entity';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isValidSourceEntity(EntityInterface $yamlform_entity, EntityInterface $source_entity = NULL) {
    if ($yamlform_entity instanceof YamlFormSubmissionInterface) {
      $yamlform = $yamlform_entity
        ->getYamlForm();
    }
    elseif ($yamlform_entity instanceof YamlFormInterface) {
      $yamlform = $yamlform_entity;
    }
    else {
      throw new \InvalidArgumentException('Form entity');
    }
    if ($source_entity && method_exists($source_entity, 'hasField') && $source_entity
      ->hasField('yamlform') && $source_entity->yamlform->target_id == $yamlform
      ->id()) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }

  /**
   * Get form submission source entity from query string.
   *
   * @return \Drupal\Core\Entity\EntityInterface|null
   *   A source entity.
   */
  protected function getCurrentSourceEntityFromQuery() {

    // Get and check for form.
    $yamlform = $this->routeMatch
      ->getParameter('yamlform');
    if (!$yamlform) {
      return NULL;
    }

    // Get and check source entity type.
    $source_entity_type = $this->request->query
      ->get('source_entity_type');
    if (!$source_entity_type || !$this->entityTypeManager
      ->hasDefinition($source_entity_type)) {
      return NULL;
    }

    // Get and check source entity id.
    $source_entity_id = $this->request->query
      ->get('source_entity_id');
    if (!$source_entity_id) {
      return NULL;
    }

    // Get and check source entity.
    $source_entity = $this->entityTypeManager
      ->getStorage($source_entity_type)
      ->load($source_entity_id);
    if (!$source_entity) {
      return NULL;
    }

    // Check source entity access.
    if (!$source_entity
      ->access('view')) {
      return NULL;
    }

    // Check that the form is referenced by the source entity.
    if (!$yamlform
      ->getSetting('form_prepopulate_source_entity')) {

      // Get source entity's yamlform field.
      $yamlform_field_name = $this
        ->getSourceEntityYamlFormFieldName($source_entity);
      if (!$yamlform_field_name) {
        return NULL;
      }

      // Check that source entity's reference form is the current YAML
      // form.
      if ($source_entity->{$yamlform_field_name}->target_id != $yamlform
        ->id()) {
        return NULL;
      }
    }
    return $source_entity;
  }

  /**
   * Get the source entity's yamlform field name.
   *
   * @param EntityInterface $source_entity
   *   A form submission's source entity.
   *
   * @return string
   *   The name of the yamlform field, or an empty string.
   */
  protected function getSourceEntityYamlFormFieldName(EntityInterface $source_entity) {
    if ($source_entity instanceof ContentEntityInterface) {
      $fields = $source_entity
        ->getFieldDefinitions();
      foreach ($fields as $field_name => $field_definition) {
        if ($field_definition
          ->getType() == 'yamlform') {
          return $field_name;
        }
      }
    }
    return '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
YamlFormRequest::$entityTypeManager protected property The entity type manager.
YamlFormRequest::$entityTypeRepository protected property The entity type repository.
YamlFormRequest::$request protected property The current request.
YamlFormRequest::$routeMatch protected property The current route match.
YamlFormRequest::getBaseRouteName public function Get the base route name for a form/submission and source entity. Overrides YamlFormRequestInterface::getBaseRouteName
YamlFormRequest::getCurrentSourceEntity public function Get the current request's source entity. Overrides YamlFormRequestInterface::getCurrentSourceEntity
YamlFormRequest::getCurrentSourceEntityFromQuery protected function Get form submission source entity from query string.
YamlFormRequest::getCurrentYamlForm public function Get form associated with the current request. Overrides YamlFormRequestInterface::getCurrentYamlForm
YamlFormRequest::getRouteName public function Get the route name for a form/submission and source entity. Overrides YamlFormRequestInterface::getRouteName
YamlFormRequest::getRouteParameters public function Get the route parameters for a form/submission and source entity. Overrides YamlFormRequestInterface::getRouteParameters
YamlFormRequest::getSourceEntityYamlFormFieldName protected function Get the source entity's yamlform field name.
YamlFormRequest::getYamlFormEntities public function Get the form and source entity for the current request. Overrides YamlFormRequestInterface::getYamlFormEntities
YamlFormRequest::getYamlFormSubmissionEntities public function Get the form submission and source entity for the current request. Overrides YamlFormRequestInterface::getYamlFormSubmissionEntities
YamlFormRequest::isValidSourceEntity public function Check if a source entity is attached to a form. Overrides YamlFormRequestInterface::isValidSourceEntity
YamlFormRequest::__construct public function Constructs a YamlFormSubmissionExporter object.