Skip to content

Commit

Permalink
Merge pull request #19687 from colemanw/afformShortcodes
Browse files Browse the repository at this point in the history
Afform - Support embedding forms via WP shortcodes.
  • Loading branch information
eileenmcnaughton authored Apr 7, 2021
2 parents fb1057e + f228ad7 commit 73a5a9a
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 2 deletions.
4 changes: 2 additions & 2 deletions api/v3/Generic/Getlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function civicrm_api3_generic_getList($apiRequest) {
$meta = civicrm_api3_generic_getfields(['action' => 'get'] + $apiRequest, FALSE);

// If the user types an integer into the search
$forceIdSearch = empty($request['id']) && !empty($request['input']) && CRM_Utils_Rule::positiveInteger($request['input']);
$forceIdSearch = empty($request['id']) && !empty($request['input']) && !empty($meta['id']) && CRM_Utils_Rule::positiveInteger($request['input']);
// Add an extra page of results for the record with an exact id match
if ($forceIdSearch) {
$request['page_num'] = ($request['page_num'] ?? 1) - 1;
Expand Down Expand Up @@ -230,7 +230,7 @@ function _civicrm_api3_generic_getlist_output($result, $request, $entity, $field
}
}
}
};
}
if (!empty($request['image_field'])) {
$data['image'] = $row[$request['image_field']] ?? '';
}
Expand Down
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;
}
119 changes: 119 additions & 0 deletions ext/afform/core/api/v3/Afform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* Get a list of afforms.
*
* This API exists solely for the purpose of entityRef widgets.
* All other Afform api functionality is v4.
*
* @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);
}

$allFields = [];
_civicrm_api3_afform_get_spec($allFields);
return _civicrm_api3_basic_array_get('Afform', $params, $result, 'name', array_keys($allFields));
}

/**
* @param array $fields
*/
function _civicrm_api3_afform_get_spec(&$fields) {
$fields['name'] = [
'title' => 'Name',
'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,
];
}

/**
* Augment parameters for Afform entityRef list.
*
* @see _civicrm_api3_generic_getlist_params
*
* @param array $request
* API request.
*/
function _civicrm_api3_afform_getlist_params(&$request) {
$fieldsToReturn = ['name', 'title', 'type', 'description'];
$request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
}

/**
* Format output for Afform entityRef list.
*
* @see _civicrm_api3_generic_getlist_output
*
* @param array $result
* @param array $request
*
* @return array
*/
function _civicrm_api3_afform_getlist_output($result, $request) {
$output = [];
if (!empty($result['values'])) {
$icons = CRM_Core_OptionGroup::values('afform_type', FALSE, FALSE, FALSE, NULL, 'icon', FALSE);
foreach ($result['values'] as $row) {
$data = [
'id' => $row[$request['id_field']],
'label' => $row[$request['label_field']],
'description' => [],
'icon' => $icons[$row['type']],
];
if (!empty($row['description'])) {
$data['description'][] = $row['description'];
}
$output[] = $data;
}
}
return $output;
}
1 change: 1 addition & 0 deletions js/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ if (!CRM.vars) CRM.vars = {};
}
markup += '<div><div class="crm-select2-row-label '+(row.label_class || '')+'">' +
(row.color ? '<span class="crm-select-item-color" style="background-color: ' + row.color + '"></span> ' : '') +
(row.icon ? '<i class="crm-i ' + row.icon + '" aria-hidden="true"></i> ' : '') +
_.escape((row.prefix !== undefined ? row.prefix + ' ' : '') + row.label + (row.suffix !== undefined ? ' ' + row.suffix : '')) +
'</div>' +
'<div class="crm-select2-row-description">';
Expand Down

0 comments on commit 73a5a9a

Please sign in to comment.