Skip to content

Commit

Permalink
DP-36788 Add support for gf_token in Mass Forms iframe URLs
Browse files Browse the repository at this point in the history
Implemented logic to append the `gf_token` query parameter to iframe URLs on form page nodes in full view mode. Added Mass Forms ddev domain to the list of allowed domains.
  • Loading branch information
dstorozhuk committed Jan 29, 2025
1 parent 0709119 commit 8960b93
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
41 changes: 41 additions & 0 deletions changelogs/DP-36788.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Write your changelog entry here. Every pull request must have a changelog yml file.
#
# Change types:
# #############################################################################
# You can use one of the following types:
# - Added: For new features.
# - Changed: For changes to existing functionality.
# - Deprecated: For soon-to-be removed features.
# - Removed: For removed features.
# - Fixed: For any bug fixes.
# - Security: In case of vulnerabilities.
#
# Format
# #############################################################################
# The format is crucial. Please follow the examples below. For reference, the requirements are:
# - All 3 parts are required and you must include "Type", "description" and "issue".
# - "Type" must be left aligned and followed by a colon.
# - "description" must be indented with 2 spaces followed by a colon
# - "issue" must be indented with 4 spaces followed by a colon.
# - "issue" is for the Jira ticket number only e.g. DP-1234
# - No extra spaces, indents, or blank lines are allowed.
#
# Example:
# #############################################################################
# Fixed:
# - description: Fixes scrolling on edit pages in Safari.
# issue: DP-13314
#
# You may add more than 1 description & issue for each type using the following format:
# Changed:
# - description: Automating the release branch.
# issue: DP-10166
# - description: Second change item that needs a description.
# issue: DP-19875
# - description: Third change item that needs a description along with an issue.
# issue: DP-19843
#
Added:
- description: Posibility to route the `gf_token` request paramater to Mass Forms iframe URL if it exists in the request.
issue: DP-36788
8 changes: 8 additions & 0 deletions docroot/modules/custom/mass_utility/mass_utility.module
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use Drupal\editor\Entity\Editor;
use Drupal\file\Entity\File;
use Drupal\link\LinkItemInterface;
use Drupal\mass_utility\EntityAlter\ImageStyle;
use Drupal\mass_utility\EntityViewBuildAlterGravityFormToken;
use Drupal\node\Entity\Node;
use Drupal\path_alias\PathAliasInterface;
use Drupal\redirect\Entity\Redirect;
Expand Down Expand Up @@ -1067,6 +1068,13 @@ function mass_utility_entity_view(array &$build, EntityInterface $entity, Entity
}
}

/**
* Implements hook_entity_view_alter().
*/
function mass_utility_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
\Drupal::classResolver(EntityViewBuildAlterGravityFormToken::class)->alter($build, $entity, $display);
}

/**
* Implements hook_form_FORM_ID_alter().
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Drupal\mass_utility;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Class EntityViewBuildAlterGravityFormToken.
*
* This class provides functionality to alter the render array of an entity.
* It injects a `gf_token` query parameter into an iframe URL for nodes of a
* specific type and view mode.
*/
class EntityViewBuildAlterGravityFormToken implements ContainerInjectionInterface {

/**
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
private RequestStack $requestStack;

/**
* Alters the render array of a specified entity.
*
* @param array $build
* The render array of the entity to be altered, passed by reference.
* @param EntityInterface $entity
* The entity object being rendered.
* @param EntityViewDisplayInterface $display
* The display configuration for the entity's view mode.
*
* @return void
* This method does not return a value, it alters the render array by reference.
*/
public function alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if ($entity instanceof NodeInterface
&& $entity->bundle() == 'form_page'
&& $build['#view_mode'] == 'full') {
if (empty($build["field_form_url"][0]["#url"])) {
return;
}

$iframe_url = $build["field_form_url"][0]["#url"];
if (!($iframe_url instanceof Url)) {
return;
}

$build["field_form_url"]["iframe_url"] = $iframe_url->toString();

$gf_token = $this->requestStack->getCurrentRequest()->get('gf_token');
if (empty($gf_token)) {
return;
}

$iframe_url->setOption('query', ['gf_token' => $gf_token]);

$build["field_form_url"]["iframe_url"] = $iframe_url->toString();
}
}

/**
* Constructs a EntityViewBuildAlterGravityFormToken object.
*
* @param RequestStack $request_stack
* The RequestStack service.
*/
public function __construct(RequestStack $request_stack) {
$this->requestStack = $request_stack;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('request_stack'));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function mass_validation_entity_bundle_field_info_alter(&$fields, EntityTypeInte
$field_form_url = &$fields['field_form_url'];
$field_form_url->addConstraint('GravityForm', [
'allowedValues' => [
'mass-forms.ddev.site',
'dev.forms.mass.gov',
'live.forms.mass.gov',
'test.forms.mass.gov',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
{% include "@atoms/09-media/iframe.twig" with {
"iframe": {
"customClass": 'js-iframe-resizer',
"src": node.field_form_url.uri,
"src": content.field_form_url.iframe_url,
"height": 'auto',
}
} %}
Expand Down

0 comments on commit 8960b93

Please sign in to comment.