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']) ->addWhere('input_type', 'IS NOT NULL') ->execute() ->indexBy('name'); } // 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/widgets/*.html') as $file) { $matches = []; preg_match('/([-a-z_A-Z0-9]*).html/', $file, $matches); $data['widgets'][$matches[1]] = $matches[1]; } $mimeType = 'text/javascript'; $content = "CRM.afformAdminData=" . json_encode($data, JSON_UNESCAPED_SLASHES) . ';'; }