Commit e63eabff authored by colemanw's avatar colemanw
Browse files

Rename custom blocks and optimize api get by file/module/directive name

parent eec46d06
...@@ -19,24 +19,27 @@ class Get extends \Civi\Api4\Generic\BasicGetAction { ...@@ -19,24 +19,27 @@ class Get extends \Civi\Api4\Generic\BasicGetAction {
$getComputed = $this->_isFieldSelected('has_local') || $this->_isFieldSelected('has_base'); $getComputed = $this->_isFieldSelected('has_local') || $this->_isFieldSelected('has_base');
$getLayout = $this->_isFieldSelected('layout'); $getLayout = $this->_isFieldSelected('layout');
$toGet = $this->_itemsToGet('name'); // This helps optimize lookups by file/module/directive name
$names = $toGet ?? array_keys($scanner->findFilePaths()); $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 $names = $toGet['name'] ?? array_keys($scanner->findFilePaths());
if (!$toGet) {
$moduleNames = $this->_itemsToGet('module_name');
$directiveNames = $this->_itemsToGet('directive_name');
}
$values = $this->getAutoGenerated($names, $toGet, $getLayout); $values = $this->getAutoGenerated($names, $toGet, $getLayout);
foreach ($names as $name) { foreach ($names as $name) {
$info = [ $info = [
'name' => $name,
'module_name' => _afform_angular_module_name($name, 'camel'), 'module_name' => _afform_angular_module_name($name, 'camel'),
'directive_name' => _afform_angular_module_name($name, 'dash'), 'directive_name' => _afform_angular_module_name($name, 'dash'),
]; ];
if (($moduleNames && !in_array($info['module_name'], $moduleNames)) || ($directiveNames && !in_array($info['directive_name'], $directiveNames))) { foreach ($toGet as $key => $get) {
continue; if (!in_array($info[$key], $get)) {
continue;
}
} }
$record = $scanner->getMeta($name); $record = $scanner->getMeta($name);
if (!$record && !isset($values[$name])) { if (!$record && !isset($values[$name])) {
...@@ -71,12 +74,16 @@ class Get extends \Civi\Api4\Generic\BasicGetAction { ...@@ -71,12 +74,16 @@ class Get extends \Civi\Api4\Generic\BasicGetAction {
*/ */
protected function getAutoGenerated(&$names, $toGet, $getLayout) { protected function getAutoGenerated(&$names, $toGet, $getLayout) {
$values = $groupNames = []; $values = $groupNames = [];
foreach ($toGet ?? [] as $name) { foreach ($toGet['name'] ?? [] as $name) {
if (strpos($name, 'blockCustom_') === 0 && strlen($name) > 12) { if (strpos($name, 'afjoinCustom_') === 0 && strlen($name) > 13) {
$groupNames[] = substr($name, 12); $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; return $values;
} }
$customApi = CustomGroup::get() $customApi = CustomGroup::get()
...@@ -98,7 +105,7 @@ class Get extends \Civi\Api4\Generic\BasicGetAction { ...@@ -98,7 +105,7 @@ class Get extends \Civi\Api4\Generic\BasicGetAction {
); );
} }
foreach ($customApi->execute() as $custom) { foreach ($customApi->execute() as $custom) {
$name = 'blockCustom_' . $custom['name']; $name = 'afjoinCustom_' . $custom['name'];
if (!in_array($name, $names)) { if (!in_array($name, $names)) {
$names[] = $name; $names[] = $name;
} }
......
...@@ -206,7 +206,7 @@ function afform_gui_civicrm_buildAsset($asset, $params, &$mimeType, &$content) { ...@@ -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() $blockData = \Civi\Api4\Afform::get()
->setCheckPermissions(FALSE) ->setCheckPermissions(FALSE)
->addWhere('join', 'IS NOT NULL') ->addWhere('join', 'IS NOT NULL')
...@@ -221,7 +221,7 @@ function afform_gui_civicrm_buildAsset($asset, $params, &$mimeType, &$content) { ...@@ -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'] = [ $data['elements'] = [
'container' => [ 'container' => [
'title' => ts('Container'), 'title' => ts('Container'),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment