FormsStepsController.php in Forms Steps 8
File
src/Controller/FormsStepsController.php
View source
<?php
namespace Drupal\forms_steps\Controller;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Controller\ControllerBase;
use Drupal\forms_steps\Entity\Workflow;
use Drupal\forms_steps\Exception\AccessDeniedException;
use Drupal\forms_steps\Exception\FormsStepsNotFoundException;
use Drupal\user\Entity\User;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class FormsStepsController extends ControllerBase {
public function step($forms_steps, $step, $instance_id = NULL) {
return self::getForm($forms_steps, $step, $instance_id);
}
public static function getForm($forms_steps, $step, $instance_id = NULL) {
$formsSteps = \Drupal::entityTypeManager()
->getStorage('forms_steps')
->load($forms_steps);
if (!$formsSteps
->hasStep($step)) {
throw new \InvalidArgumentException("The Step '{$step}' does not exist in forms steps '{$forms_steps}'");
}
$step = $formsSteps
->getStep($step);
$entity_key_type = \Drupal::entityTypeManager()
->getDefinition($step
->entityType())
->getKey('bundle');
$entity = NULL;
$entities = [];
if (!is_null($instance_id)) {
try {
$entities = \Drupal::entityTypeManager()
->getStorage(Workflow::ENTITY_TYPE)
->loadByProperties([
'instance_id' => $instance_id,
]);
} catch (\Exception $ex) {
}
if ($entities) {
foreach ($entities as $_entity) {
if (strcmp($_entity->entity_type->value, $step
->entityType()) == 0 && strcmp($_entity->bundle->value, $step
->entityBundle()) == 0) {
$storage = \Drupal::entityTypeManager()
->getStorage($_entity->entity_type->value);
$idKey = $storage
->getEntityType()
->getKey('id');
if ($storage
->getEntityType()
->isRevisionable() == FALSE) {
$revision = NULL;
}
else {
$revision = $storage
->getQuery()
->condition($idKey, $_entity->entity_id->value)
->latestRevision()
->execute();
}
if ($revision) {
$rid = key($revision);
$entity = $storage
->loadRevision($rid);
}
else {
$entity = $storage
->load($_entity->entity_id->value);
}
break;
}
}
}
}
$userRegistrationAccess = FALSE;
if ($step
->entityType() == 'user') {
$account = User::load(\Drupal::currentUser()
->id());
$registrationAccess = \Drupal::service('access_check.user.register')
->access($account);
$userRegistrationAccess = $registrationAccess
->isAllowed();
}
if (is_null($entity)) {
$entity = \Drupal::entityTypeManager()
->getStorage($step
->entityType())
->create([
$entity_key_type => $step
->entityBundle(),
]);
if ($entity) {
if (!empty($instance_id)) {
if (count($entities) == 0) {
throw new FormsStepsNotFoundException(t('No multi-step instance found.'));
}
}
else {
if ($step
->entityType() !== 'user' && !$entity
->access('create') || $step
->entityType() === 'user' && !($userRegistrationAccess || $entity
->access('create'))) {
throw new AccessDeniedHttpException();
}
else {
if ($formsSteps
->getFirstStep()
->id() != $step
->id()) {
throw new AccessDeniedException(t('First step of the multi-step forms is required.'));
}
}
}
}
}
else {
if ($step
->entityType() !== 'user' && !$entity
->access('update') || $step
->entityType() === 'user' && !$entity
->access('update')) {
throw new AccessDeniedException(t('First step of the multi-step forms is required.'));
}
}
$formMode = preg_replace("/^{$step->entityType()}\\./", '', $step
->formMode());
try {
$form = \Drupal::service('entity.form_builder')
->getForm($entity, $formMode, [
'form_steps' => TRUE,
]);
} catch (InvalidPluginDefinitionException $e) {
$entityTypeId = $entity
->getEntityTypeId();
$formModeOptions = \Drupal::service('entity_display.repository')
->getFormModeOptions($entityTypeId);
if (isset($formModeOptions[$formMode])) {
\Drupal::messenger()
->addError("Site's cache must be cleared after adding new form mode:" . $formMode . " on " . $entityTypeId);
}
else {
\Drupal::messenger()
->addWarning($e
->getMessage() . 'The form class could not be loaded.');
}
throw new NotFoundHttpException();
}
if ($step
->hideDelete()) {
unset($form['actions']['delete']);
}
elseif ($step
->deleteLabel()) {
$form['actions']['delete']['#title'] = t($step
->deleteLabel());
}
return $form;
}
}