Skip to content

Commit

Permalink
Afform - Support embedding afforms in WP shortcodes
Browse files Browse the repository at this point in the history
This implements the necessary hooks & callbacks for picking an afform from the Civi shortcode popup,
and displaying the afform when viewing a single WP post/page or list of posts.

Note that unlike traditional Civi shortcodes, afform does not rely on invoking a civi page request,
but simply initializes Angular and outputs the directive.
  • Loading branch information
colemanw committed Mar 1, 2021
1 parent c55a116 commit 372e0dd
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 10 deletions.
28 changes: 18 additions & 10 deletions ext/afform/core/CRM/Afform/Page/AfformBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,30 @@ public function run() {
$afform = civicrm_api4('Afform', 'get', [
'checkPermissions' => FALSE,
'where' => [['name', '=', $pageArgs['afform']]],
'select' => ['title', 'module_name', 'directive_name'],
'select' => ['title', 'module_name', 'directive_name', 'server_route'],
], 0);

$this->set('afModule', $afform['module_name']);
self::bootstrapStandalone($afform);

if (!empty($afform['title'])) {
$title = strip_tags($afform['title']);
CRM_Utils_System::setTitle($title);
CRM_Utils_System::appendBreadCrumb([['title' => $title, 'url' => CRM_Utils_System::url(implode('/', $pagePath), NULL, FALSE, '!/')]]);
}

parent::run();
}

/**
* @param $afform
*/
public static function bootstrapStandalone($afform) {
$loader = new \Civi\Angular\AngularLoader();
$loader->setModules([$afform['module_name'], 'afformStandalone']);
$loader->setPageName(implode('/', $pagePath));
if (!empty($afform['server_route'])) {
$loader->setPageName($afform['server_route']);
}
$loader->getRes()->addSetting([
'afform' => [
'open' => $afform['directive_name'],
Expand All @@ -26,14 +42,6 @@ public function run() {
// TODO: Allow afforms to declare their own theming requirements
->addBundle('bootstrap3');
$loader->load();

if (!empty($afform['title'])) {
$title = strip_tags($afform['title']);
CRM_Utils_System::setTitle($title);
CRM_Utils_System::appendBreadCrumb([['title' => $title, 'url' => CRM_Utils_System::url(implode('/', $pagePath), NULL, FALSE, '!/')]]);
}

parent::run();
}

}
81 changes: 81 additions & 0 deletions ext/afform/core/afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,84 @@ function afform_civicrm_alterApiRoutePermissions(&$permissions, $entity, $action
}
}
}

/**
* Implements hook_civicrm_preProcess().
*
* Adds Afform shortcode selection for WordPress.
*/
function afform_civicrm_preProcess($formName, &$form) {
if ($formName === 'CRM_Core_Form_ShortCode') {

$form->components['afform'] = [
'label' => E::ts('Form Builder'),
'select' => [
'key' => 'name',
'entity' => 'Afform',
'select' => ['minimumInputLength' => 0],
'api' => [
'params' => ['type' => ['IN' => ['form', 'search']]],
],
],
];
}
}

// Wordpress shortcode handling
if (function_exists('add_filter')) {
add_filter('civicrm_shortcode_alter_content', 'afform_shortcode_content', 10, 3);
add_filter('civicrm_shortcode_get_data', 'afform_shortcode_data', 10, 3);
}

/**
* Render Afform content for WP shortcodes.
*
* @param string $content
* HTML Markup
* @param array $atts
* Shortcode attributes.
* @param array $args
* Existing shortcode arguments.
* @return string
* Modified markup.
*/
function afform_shortcode_content($content, $atts, $args) {
if ($atts['component'] === 'afform') {
$afform = civicrm_api4('Afform', 'get', [
'select' => ['title', 'directive_name', 'module_name', 'server_route'],
'where' => [['name', '=', $atts['name']]],
])->first();
if ($afform) {
CRM_Afform_Page_AfformBase::bootstrapStandalone($afform);
$content = '<div ng-app="afformStandalone"><form ng-view></form></div>';
}
}

return $content;
}

/**
* Add Afform title when displaying multiple WP shortcodes.
*
* @param array $data
* Existing data
* @param array $atts
* Shortcode attributes.
* @param array $args
* Existing shortcode arguments.
* @return array
* Modified data.
*/
function afform_shortcode_data($data, $atts, $args) {
if ($atts['component'] === 'afform') {
$afform = civicrm_api4('Afform', 'get', [
'select' => ['title'],
'where' => [['name', '=', $atts['name']]],
])->first();
if ($afform) {
$data['title'] = $afform['title'];
}
}

return $data;
}

0 comments on commit 372e0dd

Please sign in to comment.