Skip to content

Commit

Permalink
Add Variables::EVENT_PARSE_VARIABLES to allow you to parse custom r…
Browse files Browse the repository at this point in the history
…egistered variables
  • Loading branch information
engram-design committed Jan 13, 2025
1 parent b302f42 commit eeb1315
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
21 changes: 21 additions & 0 deletions src/events/ParseVariablesEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace verbb\formie\events;

use verbb\formie\elements\Form;
use verbb\formie\elements\Submission;
use verbb\formie\models\Notification;

use yii\base\Event;

class ParseVariablesEvent extends Event
{
// Properties
// =========================================================================

public mixed $value;
public ?Submission $submission = null;
public ?Form $form = null;
public ?Notification $notification = null;
public array $variables = [];

}
25 changes: 19 additions & 6 deletions src/helpers/Variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use verbb\formie\base\SubFieldInterface;
use verbb\formie\elements\Form;
use verbb\formie\elements\Submission;
use verbb\formie\events\ParseVariablesEvent;
use verbb\formie\events\RegisterVariablesEvent;
use verbb\formie\fields\formfields;
use verbb\formie\models\Notification;
Expand Down Expand Up @@ -33,6 +34,7 @@ class Variables
// =========================================================================

public const EVENT_REGISTER_VARIABLES = 'registerVariables';
public const EVENT_PARSE_VARIABLES = 'parseVariables';


// Static Methods
Expand Down Expand Up @@ -230,7 +232,7 @@ public static function getParsedValue(mixed $value, Submission $submission = nul
// Form Info
$formName = $form->title ?? '';

Formie::$plugin->getRenderCache()->setGlobalVariables($cacheKey, [
$variables = [
'formName' => $formName,
'submissionUrl' => $submission->cpEditUrl ?? '',
'submissionId' => $submission->id ?? null,
Expand Down Expand Up @@ -259,14 +261,15 @@ public static function getParsedValue(mixed $value, Submission $submission = nul
'userFullName' => $userFullName,
'userFirstName' => $userFirstName,
'userLastName' => $userLastName,
]);
];

// Add support for all global sets
foreach (Craft::$app->getGlobals()->getAllSets() as $globalSet) {
Formie::$plugin->getRenderCache()->setGlobalVariables($cacheKey, [
$globalSet->handle => $globalSet,
]);
$variables[$globalSet->handle] = $globalSet;
}

// Cache variables in-memory for better performance next parse
Formie::$plugin->getRenderCache()->setGlobalVariables($cacheKey, $variables);
}

$fieldVariables = [];
Expand Down Expand Up @@ -299,9 +302,19 @@ public static function getParsedValue(mixed $value, Submission $submission = nul
}
}

// Allow plugins to modify the variables
$event = new ParseVariablesEvent([
'submission' => $submission,
'form' => $form,
'notification' => $notification,
'value' => $value,
'variables' => $variables,
]);
Event::trigger(self::class, self::EVENT_PARSE_VARIABLES, $event);

// Try to parse submission + extra variables
try {
return Formie::$plugin->getTemplates()->renderObjectTemplate($value, $submission, $variables);
return Formie::$plugin->getTemplates()->renderObjectTemplate($value, $submission, $event->variables);
} catch (Throwable $e) {
Formie::error(Craft::t('formie', 'Failed to render dynamic string “{value}”. Template error: “{message}” {file}:{line}', [
'value' => $originalValue,
Expand Down

0 comments on commit eeb1315

Please sign in to comment.