Skip to content

Commit

Permalink
Afform - Load afforms directly from shortcodes instead of going throu…
Browse files Browse the repository at this point in the history
…gh page route
  • Loading branch information
colemanw committed Feb 28, 2021
1 parent 0b0f318 commit b6fc3dc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
32 changes: 20 additions & 12 deletions ext/afform/core/CRM/Afform/Page/AfformBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,35 @@ class CRM_Afform_Page_AfformBase extends CRM_Core_Page {

public function run() {
// To avoid php complaints about the number of args passed to this function vs the base function
[$pagePath, $pageArgs] = func_get_args();
[$pagePath] = func_get_args();

$afform = civicrm_api4('Afform', 'get', [
'checkPermissions' => FALSE,
'where' => [['name', '=', $pageArgs['afform']]],
'select' => ['title', 'module_name', 'directive_name'],
'where' => [['server_route', '=', implode('/', $pagePath)]],
'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();
}

}
39 changes: 24 additions & 15 deletions ext/afform/core/afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ function afform_civicrm_alterMenu(&$items) {
if (!empty($meta['server_route'])) {
$items[$meta['server_route']] = [
'page_callback' => 'CRM_Afform_Page_AfformBase',
'page_arguments' => 'afform=' . urlencode($name),
'title' => $meta['title'] ?? '',
'access_arguments' => [["@afform:$name"], 'and'],
'is_public' => $meta['is_public'],
Expand Down Expand Up @@ -425,28 +424,38 @@ function afform_civicrm_preProcess($formName, &$form) {

// Wordpress shortcode handling
if (function_exists('add_filter')) {
add_filter('civicrm_shortcode_preprocess_atts', 'afform_amend_args', 10, 2);
add_filter('civicrm_shortcode_get_data', 'afform_shortcode_data', 10, 4);
}

/**
* Filter the CiviCRM shortcode arguments to process afform shortcodes.
* Render Afform content for WP shortcodes.
*
* @param array $data
* Existing data
* @param array $atts
* Shortcode attributes.
* @param array $args
* Existing shortcode arguments.
* @param array $shortcode_atts
* Shortcode attributes.
* @param string $mode
* 'single'|'multiple'
* @return array
* Modified shortcode arguments.
* Modified data.
*/
function afform_amend_args($args, $shortcode_atts) {

// Lookup afform path
if ($shortcode_atts['component'] === 'afform') {
$args['q'] = civicrm_api4('Afform', 'get', [
'select' => ['server_route'],
'where' => [['name', '=', 'afformMyAfform']],
], 0)['server_route'];
function afform_shortcode_data($data, $atts, $args, $mode) {
if ($atts['component'] === 'afform') {
$afform = civicrm_api4('Afform', 'get', [
'select' => ['title', 'directive_name', 'module_name'],
'where' => [['name', '=', $atts['name']]],
])->first();
if ($afform) {
$data['title'] = $afform['title'];
// In full-page mode, bootstrap Angular and load afform
if ($mode === 'single') {
CRM_Afform_Page_AfformBase::bootstrapStandalone($afform);
$data['text'] = '<div ng-app="afformStandalone"><form ng-view></form></div>';
}
}
}

return $args;
return $data;
}

0 comments on commit b6fc3dc

Please sign in to comment.