get('afModule') == 'afGuiAdmin') { Civi::resources()->addScriptUrl(Civi::service('asset_builder')->getUrl('af-gui-vars.js')); } } /** * Implements hook_civicrm_buildAsset(). * * Loads metadata to send to the gui editor. */ function afform_gui_civicrm_buildAsset($asset, $params, &$mimeType, &$content) { if ($asset !== 'af-gui-vars.js') { return; } $entityWhitelist = $data = []; // First scan the entityDefaults directory for our list of supported entities // FIXME: Need a way to load this from other extensions too foreach (glob(__DIR__ . '/ang/afGuiEditor/entityDefaults/*.json') as $file) { $matches = []; preg_match('/([-a-z_A-Z0-9]*).json/', $file, $matches); $entityWhitelist[] = $entity = $matches[1]; // No json_decode, the files are not strict json and will go through angular.$parse clientside $data['defaults'][$entity] = trim(CRM_Utils_JS::stripComments(file_get_contents($file))); } $data['entities'] = (array) Civi\Api4\Entity::get() ->setCheckPermissions(FALSE) ->setSelect(['name', 'description']) ->addWhere('name', 'IN', $entityWhitelist) ->execute(); foreach ($entityWhitelist as $entityName) { $api = 'Civi\\Api4\\' . $entityName; $data['fields'][$entityName] = (array) $api::getFields() ->setCheckPermissions(FALSE) ->setIncludeCustom(TRUE) ->setLoadOptions(TRUE) ->setAction('create') ->setSelect(['name', 'title', 'input_type', 'input_attrs', 'required', 'options', 'help_pre', 'help_post', 'serialize', 'data_type']) ->addWhere('input_type', 'IS NOT NULL') ->execute() ->indexBy('name'); // TODO: Teach the api to return options in this format foreach ($data['fields'][$entityName] as $name => $field) { if (!empty($field['options'])) { $data['fields'][$entityName][$name]['options'] = CRM_Utils_Array::makeNonAssociative($field['options'], 'key', 'label'); } else { unset($data['fields'][$entityName][$name]['options']); } } } // Now adjust the field metadata // FIXME: This should probably be a callback event or something to allow extensions to tweak the metadata for their entities $data['fields']['Contact']['contact_type']['required_data'] = TRUE; // Scan for input types // FIXME: Need a way to load this from other extensions too foreach (glob(__DIR__ . '/ang/afGuiEditor/inputType/*.html') as $file) { $matches = []; preg_match('/([-a-z_A-Z0-9]*).html/', $file, $matches); $data['inputType'][$matches[1]] = $matches[1]; } $data['styles'] = [ 'default' => ts('Default'), 'primary' => ts('Primary'), 'success' => ts('Success'), 'info' => ts('Info'), 'warning' => ts('Warning'), 'danger' => ts('Danger'), ]; $mimeType = 'text/javascript'; $content = "CRM.afformAdminData=" . json_encode($data, JSON_UNESCAPED_SLASHES) . ';'; }