Skip to content

Commit

Permalink
Afform - Support embedding forms via WP shortcodes.
Browse files Browse the repository at this point in the history
This gives very basic and flawed support for Afform shortcodes. It works, but

- The afform must have a server_route defined for no other reason than because civi shortcodes expect a page callback
- The widget for selecting an afform is terrible & search only works if you type the exact title
  • Loading branch information
colemanw committed Feb 27, 2021
1 parent 97032c8 commit 2791073
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
48 changes: 48 additions & 0 deletions ext/afform/core/afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,51 @@ 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' => [],
],
];
}
}

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

/**
* Filter the CiviCRM shortcode arguments to process afform shortcodes.
*
* @param array $args
* Existing shortcode arguments.
* @param array $shortcode_atts
* Shortcode attributes.
* @return array
* Modified shortcode arguments.
*/
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'];
}

return $args;
}
70 changes: 70 additions & 0 deletions ext/afform/core/api/v3/Afform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* Get a list of afforms.
*
* @param array $params
*
* @return array
* API result
*/
function civicrm_api3_afform_get($params) {
/** @var \CRM_Afform_AfformScanner $scanner */
$scanner = \Civi::service('afform_scanner');

$names = array_keys($scanner->findFilePaths());
$result = [];

foreach ($names as $name) {
$info = [
'name' => $name,
'module_name' => _afform_angular_module_name($name, 'camel'),
'directive_name' => _afform_angular_module_name($name, 'dash'),
];
$record = $scanner->getMeta($name);
$result[$name] = array_merge($record, $info);
}

$filterableFields = ['name', 'module_name', 'directive_name', 'server_route', 'type', 'description', 'title'];
return _civicrm_api3_basic_array_get('Afform', $params, $result, 'name', $filterableFields);
}

function _civicrm_api3_afform_get_spec(&$fields) {
$fields['name'] = [
'title' => 'Name',
'api.aliases' => ['id'],
'type' => CRM_Utils_Type::T_STRING,
];
$fields['title'] = [
'title' => 'Title',
'type' => CRM_Utils_Type::T_STRING,
];
$fields['module_name'] = [
'title' => 'Module Name',
'type' => CRM_Utils_Type::T_STRING,
];
$fields['directive_name'] = [
'title' => 'Directive Name',
'type' => CRM_Utils_Type::T_STRING,
];
$fields['description'] = [
'title' => 'Description',
'type' => CRM_Utils_Type::T_STRING,
];
$fields['server_route'] = [
'title' => 'Server Route',
'type' => CRM_Utils_Type::T_STRING,
];
$fields['type'] = [
'title' => 'Type',
'type' => CRM_Utils_Type::T_STRING,
];
$fields['is_dashlet'] = [
'title' => 'Dashlet',
'type' => CRM_Utils_Type::T_BOOLEAN,
];
$fields['is_public'] = [
'title' => 'Public',
'type' => CRM_Utils_Type::T_BOOLEAN,
];
}

0 comments on commit 2791073

Please sign in to comment.