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 {
$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;
}
......
......@@ -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'),
......
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