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 outputs the AngularJS directive.
  • Loading branch information
colemanw committed Apr 7, 2021
1 parent 7021898 commit f228ad7
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions ext/afform/core/afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,62 @@ function afform_civicrm_alterApiRoutePermissions(&$permissions, $entity, $action
}
}
}

/**
* Implements hook_civicrm_preProcess().
*
* Wordpress only: Adds Afforms to the shortcode dialog (when editing pages/posts).
*/
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 only: Register callback for rendering shortcodes
if (function_exists('add_filter')) {
add_filter('civicrm_shortcode_get_markup', 'afform_shortcode_content', 10, 4);
}

/**
* Wordpress only: Render Afform content for shortcodes.
*
* @param string $content
* HTML Markup
* @param array $atts
* Shortcode attributes.
* @param array $args
* Existing shortcode arguments.
* @param string $context
* How many shortcodes are present on the page: 'single' or 'multiple'.
* @return string
* Modified markup.
*/
function afform_shortcode_content($content, $atts, $args, $context) {
if ($atts['component'] === 'afform') {
$afform = civicrm_api4('Afform', 'get', [
'select' => ['directive_name', 'module_name'],
'where' => [['name', '=', $atts['name']]],
])->first();
if ($afform) {
Civi::service('angularjs.loader')->addModules($afform['module_name']);
$content = "
<div class='crm-container' id='bootstrap-theme'>
<crm-angular-js modules='{$afform['module_name']}'>
<{$afform['directive_name']}></{$afform['directive_name']}>
</crm-angular-js>
</div>";
}
}
return $content;
}

0 comments on commit f228ad7

Please sign in to comment.