Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary fix for frontend AfformTokens being generated incorrectly #31801

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CRM/Utils/System/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,14 @@ public function url(

// Overwrite base URL if we already have a front-end URL.
if (!$forceBackend && $frontend_url != '') {
$base = $frontend_url;
if (!empty(\Civi::$statics['afformtokenscontext'])) {
\Civi::log()->debug('url(): In afformTokens context. Clearing $frontend_url');
$frontend_url = '';
}
else {
// Only if not in afform tokens context
$base = $frontend_url;
}
}

$queryParts = [];
Expand Down Expand Up @@ -408,6 +415,7 @@ public function url(

}

unset(Civi::$statics['afformtokenscontext']);
return $final;
}

Expand Down
1 change: 1 addition & 0 deletions ext/afform/core/Civi/Afform/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public static function evaluateTokens(\Civi\Token\Event\TokenValueEvent $e) {
if (empty($row->context['contactId'])) {
continue;
}
\Civi::$statics['afformtokenscontext'] = TRUE;
Copy link
Member

@christianwach christianwach Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattwire Rather than using a global to modify what you're doing, you could replicate what CiviCRM-WordPress does to ensure that a Base Page URL is generated. Something like:

if (function_exists('add_filter')) {
    // For WordPress: add modifying callbacks prior to multi-lingual compat.
    add_filter('civicrm/basepage/match', [civi_wp()->basepage, 'ensure_match'], 9);
    add_filter('civicrm/core/url/base', [civi_wp()->basepage, 'ensure_url'], 9, 2);
}
$url = self::createUrl($afform, $row->context['contactId']);
if (function_exists('add_filter')) {
    // For WordPress: remove callbacks.
    remove_filter('civicrm/basepage/match', [civi_wp()->basepage, 'ensure_match'], 9);
    remove_filter('civicrm/core/url/base', [civi_wp()->basepage, 'ensure_url'], 9);
}

This should make modifying CRM_Utils_System_WordPress::url() unnecessary.

$url = self::createUrl($afform, $row->context['contactId']);
$row->format('text/plain')->tokens(static::$prefix, $afform['name'] . 'Url', $url);
$row->format('text/html')->tokens(static::$prefix, $afform['name'] . 'Link', sprintf('<a href="%s">%s</a>', htmlentities($url), htmlentities($afform['title'] ?? $afform['name'])));
Expand Down