From edf837b4cf8f0227f768246327005c92bebd6630 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 7 Jan 2020 17:35:33 -0500 Subject: [PATCH] Rename custom blocks and optimize api get by file/module/directive name --- .../core/Civi/Api4/Action/Afform/Get.php | 35 +++++++++++-------- ext/afform/gui/afform_gui.php | 4 +-- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/ext/afform/core/Civi/Api4/Action/Afform/Get.php b/ext/afform/core/Civi/Api4/Action/Afform/Get.php index beeca11797..e3bfe1e6bf 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/Get.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/Get.php @@ -19,24 +19,27 @@ class Get extends \Civi\Api4\Generic\BasicGetAction { $getComputed = $this->_isFieldSelected('has_local') || $this->_isFieldSelected('has_base'); $getLayout = $this->_isFieldSelected('layout'); - $toGet = $this->_itemsToGet('name'); - $names = $toGet ?? array_keys($scanner->findFilePaths()); + // This helps optimize lookups by file/module/directive name + $toGet = array_filter([ + 'name' => $this->_itemsToGet('name'), + 'module_name' => $this->_itemsToGet('module_name'), + 'directive_name' => $this->_itemsToGet('directive_name'), + ]); - // Optimize lookups by module/directive name - if (!$toGet) { - $moduleNames = $this->_itemsToGet('module_name'); - $directiveNames = $this->_itemsToGet('directive_name'); - } + $names = $toGet['name'] ?? array_keys($scanner->findFilePaths()); $values = $this->getAutoGenerated($names, $toGet, $getLayout); foreach ($names as $name) { $info = [ + 'name' => $name, 'module_name' => _afform_angular_module_name($name, 'camel'), 'directive_name' => _afform_angular_module_name($name, 'dash'), ]; - if (($moduleNames && !in_array($info['module_name'], $moduleNames)) || ($directiveNames && !in_array($info['directive_name'], $directiveNames))) { - continue; + foreach ($toGet as $key => $get) { + if (!in_array($info[$key], $get)) { + continue; + } } $record = $scanner->getMeta($name); if (!$record && !isset($values[$name])) { @@ -71,12 +74,16 @@ class Get extends \Civi\Api4\Generic\BasicGetAction { */ protected function getAutoGenerated(&$names, $toGet, $getLayout) { $values = $groupNames = []; - foreach ($toGet ?? [] as $name) { - if (strpos($name, 'blockCustom_') === 0 && strlen($name) > 12) { - $groupNames[] = substr($name, 12); + foreach ($toGet['name'] ?? [] as $name) { + if (strpos($name, 'afjoinCustom_') === 0 && strlen($name) > 13) { + $groupNames[] = substr($name, 13); } } - if ($toGet && !$groupNames) { + if ( + (!empty($toGet['name']) && !$groupNames) + || (!empty($toGet['module_name']) && !strstr(implode(' ', $toGet['module_name']), 'afjoinCustom')) + || (!empty($toGet['directive_name']) && !strstr(implode(' ', $toGet['directive_name']), 'afjoin-custom')) + ) { return $values; } $customApi = CustomGroup::get() @@ -98,7 +105,7 @@ class Get extends \Civi\Api4\Generic\BasicGetAction { ); } foreach ($customApi->execute() as $custom) { - $name = 'blockCustom_' . $custom['name']; + $name = 'afjoinCustom_' . $custom['name']; if (!in_array($name, $names)) { $names[] = $name; } diff --git a/ext/afform/gui/afform_gui.php b/ext/afform/gui/afform_gui.php index 8f0effff64..c5fe4e3091 100644 --- a/ext/afform/gui/afform_gui.php +++ b/ext/afform/gui/afform_gui.php @@ -206,7 +206,7 @@ function afform_gui_civicrm_buildAsset($asset, $params, &$mimeType, &$content) { } } - // Load fields from entity joins + // Load fields from afform blocks with joins $blockData = \Civi\Api4\Afform::get() ->setCheckPermissions(FALSE) ->addWhere('join', 'IS NOT NULL') @@ -221,7 +221,7 @@ function afform_gui_civicrm_buildAsset($asset, $params, &$mimeType, &$content) { } } - // Todo: scan for other elements + // Todo: add method for extensions to define other elements $data['elements'] = [ 'container' => [ 'title' => ts('Container'), -- 2.25.1