From 4f664e9c749c49796fd99101b5c09d5734b3883d Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 12 Apr 2021 18:15:49 -0400 Subject: [PATCH] Afform - Improve integration with ContactLayout editor - Adds a separate category of ContactLayout block per afform-type - Adds a helper function to getFields so non-dao entities can load options from an optionGroup - Renames search display afforms to "Search Form" to distinguish them from actual search displays --- CRM/Core/OptionValue.php | 7 ++- Civi/Api4/Generic/BasicGetFieldsAction.php | 18 ++++++++ .../Civi/AfformAdmin/AfformAdminMeta.php | 2 +- ext/afform/core/Civi/Api4/Afform.php | 1 + ext/afform/core/afform.php | 43 ++++++++++--------- ext/afform/core/managed/AfformType.mgd.php | 2 +- 6 files changed, 50 insertions(+), 23 deletions(-) diff --git a/CRM/Core/OptionValue.php b/CRM/Core/OptionValue.php index 27581b35cb..765cedd17f 100644 --- a/CRM/Core/OptionValue.php +++ b/CRM/Core/OptionValue.php @@ -390,7 +390,7 @@ class CRM_Core_OptionValue { * Array of option-values * */ - public static function getValues($groupParams, &$values, $orderBy = 'weight', $isActive = FALSE) { + public static function getValues($groupParams, &$values = [], $orderBy = 'weight', $isActive = FALSE) { if (empty($groupParams)) { return NULL; } @@ -403,6 +403,8 @@ SELECT option_value.description as description, option_value.weight as weight, option_value.is_active as is_active, + option_value.icon as icon, + option_value.color as color, option_value.is_default as is_default"; $from = " @@ -454,8 +456,11 @@ FROM 'weight' => $dao->weight, 'is_active' => $dao->is_active, 'is_default' => $dao->is_default, + 'icon' => $dao->icon, + 'color' => $dao->icon, ]; } + return $values; } } diff --git a/Civi/Api4/Generic/BasicGetFieldsAction.php b/Civi/Api4/Generic/BasicGetFieldsAction.php index 0c0d9eae66..124ba7f821 100644 --- a/Civi/Api4/Generic/BasicGetFieldsAction.php +++ b/Civi/Api4/Generic/BasicGetFieldsAction.php @@ -214,6 +214,24 @@ class BasicGetFieldsAction extends BasicGetAction { return $this; } + /** + * Helper function to retrieve options from an option group (for non-DAO entities). + * + * @param string $optionGroupName + */ + public function pseudoconstantOptions(string $optionGroupName) { + if ($this->getLoadOptions()) { + $options = \CRM_Core_OptionValue::getValues(['name' => $optionGroupName]); + foreach ($options as &$option) { + $option['id'] = $option['value']; + } + } + else { + $options = TRUE; + } + return $options; + } + public function fields() { return [ [ diff --git a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php index ba860905b4..56467f1b22 100644 --- a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php +++ b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php @@ -19,7 +19,7 @@ class AfformAdminMeta { // Pluralize tabs (too bad option groups only store a single label) $plurals = [ 'form' => E::ts('Custom Forms'), - 'search' => E::ts('Search Displays'), + 'search' => E::ts('Search Forms'), 'block' => E::ts('Field Blocks'), 'system' => E::ts('System Forms'), ]; diff --git a/ext/afform/core/Civi/Api4/Afform.php b/ext/afform/core/Civi/Api4/Afform.php index dfcd80ef4b..54031ab13d 100644 --- a/ext/afform/core/Civi/Api4/Afform.php +++ b/ext/afform/core/Civi/Api4/Afform.php @@ -127,6 +127,7 @@ class Afform extends Generic\AbstractEntity { ], [ 'name' => 'type', + 'options' => $self->pseudoconstantOptions('afform_type'), ], [ 'name' => 'requires', diff --git a/ext/afform/core/afform.php b/ext/afform/core/afform.php index 08844f65b7..af2051c313 100644 --- a/ext/afform/core/afform.php +++ b/ext/afform/core/afform.php @@ -241,26 +241,29 @@ function afform_civicrm_pageRun(&$page) { * @link https://github.com/civicrm/org.civicrm.contactlayout */ function afform_civicrm_contactSummaryBlocks(&$blocks) { - $scanner = \Civi::service('afform_scanner'); - foreach ($scanner->getMetas() as $afform) { - if (!empty($afform['contact_summary']) && $afform['contact_summary'] === 'block') { - // Provide our own group for this block to visually distinguish it on the contact summary editor palette. - $blocks += [ - 'afform' => [ - 'title' => ts('Form Builder'), - 'icon' => 'fa-list-alt', - 'blocks' => [], - ], - ]; - $blocks['afform']['blocks'][$afform['name']] = [ - 'title' => $afform['title'], - 'tpl_file' => 'afform/contactSummary/AfformBlock.tpl', - 'module' => _afform_angular_module_name($afform['name']), - 'directive' => _afform_angular_module_name($afform['name'], 'dash'), - 'sample' => [], - 'edit' => 'civicrm/admin/afform#/edit/' . $afform['name'], - ]; - } + $afforms = \Civi\Api4\Afform::get(FALSE) + ->setSelect(['name', 'title', 'directive_name', 'module_name', 'type', 'type:icon', 'type:label']) + ->addWhere('contact_summary', '=', 'block') + ->execute(); + foreach ($afforms as $afform) { + // Create a group per afform type + $blocks += [ + "afform_{$afform['type']}" => [ + 'title' => $afform['type:label'], + 'icon' => $afform['type:icon'], + 'blocks' => [], + ], + ]; + $blocks["afform_{$afform['type']}"]['blocks'][$afform['name']] = [ + 'title' => $afform['title'], + 'tpl_file' => 'afform/contactSummary/AfformBlock.tpl', + 'module' => $afform['module_name'], + 'directive' => $afform['directive_name'], + 'sample' => [ + $afform['type:label'], + ], + 'edit' => 'civicrm/admin/afform#/edit/' . $afform['name'], + ]; } } diff --git a/ext/afform/core/managed/AfformType.mgd.php b/ext/afform/core/managed/AfformType.mgd.php index e0865d731c..17c72c6aac 100644 --- a/ext/afform/core/managed/AfformType.mgd.php +++ b/ext/afform/core/managed/AfformType.mgd.php @@ -60,7 +60,7 @@ try { 'option_group_id' => 'afform_type', 'name' => 'search', 'value' => 'search', - 'label' => 'Search Display', + 'label' => 'Search Form', 'weight' => 10, 'icon' => 'fa-search', ], -- 2.25.1