Skip to content

Commit

Permalink
HKI-766-71354 webform automatic redirect form node
Browse files Browse the repository at this point in the history
  • Loading branch information
stankut committed Apr 3, 2020
1 parent 1b2a224 commit 1af523b
Showing 1 changed file with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\os2web_nemlogin\Service\AuthProviderService;
use Drupal\webform\Entity\Webform;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
Expand Down Expand Up @@ -56,12 +57,45 @@ public function redirectToNemlogin(GetResponseEvent $event) {
// This is necessary because this also gets called on
// webform sub-tabs such as "edit", "revisions", etc. This
// prevents those pages from redirected.
if ($request->attributes->get('_route') !== 'entity.webform.canonical') {
$route = $request->attributes->get('_route');
if ($route !== 'entity.webform.canonical' && $route !== 'entity.node.canonical') {
return;
}

/** @var \Drupal\webform\WebformInterface $webform */
$webform = $request->attributes->get('webform');
$webform = NULL;

if ($route === 'entity.webform.canonical') {
$webform = $request->attributes->get('webform');
}
else {
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager */
$entityFieldManager = \Drupal::service('entity_field.manager');

$node = $request->attributes->get('node');
$nodeType = $node->getType();

// Search if this node type is related with field of type 'webform'.
$webformFieldMap = $entityFieldManager->getFieldMapByFieldType('webform');
if (isset($webformFieldMap['node'])) {
foreach ($webformFieldMap['node'] as $field_name => $field_meta) {
// We found field of type 'webform' in this node, let's try fetching
// the webform.
if (in_array($nodeType, $field_meta['bundles'])) {
if ($webformId = $node->get($field_name)->target_id) {
$webform = Webform::load($webformId);
break;
}
}
}
}
}

// If we don't have any webform.
if (!$webform) {
return;
}

$webformNemidSettings = $webform->getThirdPartySetting('os2forms', 'os2forms_nemid');

// Getting nemlogin_auto_redirect setting.
Expand Down

0 comments on commit 1af523b

Please sign in to comment.