From 9384980c250bd6301fc3b8cab2759844fb11e58b Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 13 Jun 2019 12:50:22 -0400 Subject: [PATCH] afform/core - Inject APIv4 metadata on the `af-field` tags --- ext/afform/core/afform.php | 38 ++++++++++++++++++++++++++++++++++++++ ext/afform/docs/roadmap.md | 4 ++++ 2 files changed, 42 insertions(+) diff --git a/ext/afform/core/afform.php b/ext/afform/core/afform.php index 6c7586e7fb..5ab9c7547e 100644 --- a/ext/afform/core/afform.php +++ b/ext/afform/core/afform.php @@ -170,6 +170,44 @@ function afform_civicrm_angularModules(&$angularModules) { } } +/** + * @param \Civi\Angular\Manager $angular + * @see CRM_Utils_Hook::alterAngular() + */ +function afform_civicrm_alterAngular($angular) { + $fieldMetadata = \Civi\Angular\ChangeSet::create('fieldMetadata') + ->alterHtml(';^~afform/;', function($doc, $path){ + $entities = _afform_getMetadata($doc); + + foreach (pq('af-field', $doc) as $afField) { + /** @var DOMElement $afField */ + $fieldName = $afField->getAttribute('field-name'); + $entityName = pq($afField)->parent('[af-name]')->attr('af-name'); + if (!preg_match(';^[a-zA-Z0-9\_\-\. ]+$;', $entityName)) { + throw new \CRM_Core_Exception("Cannot process $path: malformed entity name ($entityName)"); + } + $entityType = $entities[$entityName]['type']; + $getFields = civicrm_api4($entityType, 'getFields', [ + 'where' => [['name', '=', $fieldName]], + ]); + foreach ($getFields as $field) { + pq($afField)->attr('field-defn', json_encode($field, JSON_UNESCAPED_SLASHES)); + } + } + }); + $angular->add($fieldMetadata); +} + +function _afform_getMetadata(phpQueryObject $doc) { + $entities = []; + foreach ($doc->find('af-model-prop') as $afmModelProp) { + $entities[$afmModelProp->getAttribute('af-name')] = [ + 'type' => $afmModelProp->getAttribute('af-type'), + ]; + } + return $entities; +} + /** * Implements hook_civicrm_alterSettingsFolders(). * diff --git a/ext/afform/docs/roadmap.md b/ext/afform/docs/roadmap.md index f715401676..eb55f35545 100644 --- a/ext/afform/docs/roadmap.md +++ b/ext/afform/docs/roadmap.md @@ -50,3 +50,7 @@ Within this extension, there are things which need updating/addressing: support batching (i.e. all API calls issued within a 5ms window are sent as a batch). * Default CSS: There's no mechanism for defining adhoc CSS. This is arguably a feature, though, because the CSS classes should be validated (to ensure theme interoperability). +* `Civi/Angular/ChangeSet.php` previously had an integrity check that activated in developer mode + (`\CRM_Core_Config::singleton()->debug && $coder->checkConsistentHtml($html)`). This has been removed because it was a bit brittle + about self-closing HTML tags. However, the general concept of HTML validation should be reinstated as part of the `afform_auditor`. +* `hook_alterAngular` is used to inject APIv4 metadata for certain tags. This behavior needs a unit-test. -- 2.25.1