diff --git a/ext/afform/core/CRM/Afform/Page/AfformBase.php b/ext/afform/core/CRM/Afform/Page/AfformBase.php index a2981b63aaa8..c13f1eac5c63 100644 --- a/ext/afform/core/CRM/Afform/Page/AfformBase.php +++ b/ext/afform/core/CRM/Afform/Page/AfformBase.php @@ -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'], @@ -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(); } } diff --git a/ext/afform/core/afform.php b/ext/afform/core/afform.php index 87c4e2433756..fca382ac46b5 100644 --- a/ext/afform/core/afform.php +++ b/ext/afform/core/afform.php @@ -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 = '
'; + } + } + + 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; +}