From e0d7fb80dd4905234cea20023671ac3b5049a2ae Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 10 Feb 2021 11:20:15 -0500 Subject: [PATCH] AfformMetadataInjector - Cleanup variable names & comments --- .../Civi/Afform/AfformMetadataInjector.php | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/ext/afform/core/Civi/Afform/AfformMetadataInjector.php b/ext/afform/core/Civi/Afform/AfformMetadataInjector.php index 5d13b28ac7..a25a3a4c37 100644 --- a/ext/afform/core/Civi/Afform/AfformMetadataInjector.php +++ b/ext/afform/core/Civi/Afform/AfformMetadataInjector.php @@ -74,15 +74,16 @@ class AfformMetadataInjector { /** * Merge field definition metadata into an afform field's definition * - * @param string $entityType + * @param string $entityName * @param string $action * @param \DOMElement $afField * @throws \API_Exception */ - private static function fillFieldMetadata($entityType, $action, \DOMElement $afField) { + private static function fillFieldMetadata($entityName, $action, \DOMElement $afField) { $fieldName = $afField->getAttribute('name'); - if (strpos($entityType, ' AS ')) { - [$entityType, $alias] = explode(' AS ', $entityType); + // For explicit joins, strip the alias off the field name + if (strpos($entityName, ' AS ')) { + [$entityName, $alias] = explode(' AS ', $entityName); $fieldName = preg_replace('/^' . preg_quote($alias . '.', '/') . '/', '', $fieldName); } $params = [ @@ -90,20 +91,21 @@ class AfformMetadataInjector { 'where' => [['name', '=', $fieldName]], 'select' => ['label', 'input_type', 'input_attrs', 'options'], 'loadOptions' => ['id', 'label'], + // If the admin included this field on the form, then it's OK to get metadata about the field regardless of user permissions. + 'checkPermissions' => FALSE, ]; - if (in_array($entityType, \CRM_Contact_BAO_ContactType::basicTypes(TRUE))) { - $params['values'] = ['contact_type' => $entityType]; - $entityType = 'Contact'; + if (in_array($entityName, \CRM_Contact_BAO_ContactType::basicTypes(TRUE))) { + $params['values'] = ['contact_type' => $entityName]; + $entityName = 'Contact'; } + $fieldInfo = civicrm_api4($entityName, 'getFields', $params)->first(); // Merge field definition data with whatever's already in the markup. - // If the admin has chosen to include this field on the form, then it's OK for us to get metadata about the field - regardless of user's other permissions. - $getFields = civicrm_api4($entityType, 'getFields', $params + ['checkPermissions' => FALSE]); $deep = ['input_attrs']; - foreach ($getFields as $fieldInfo) { + if ($fieldInfo) { $existingFieldDefn = trim(pq($afField)->attr('defn') ?: ''); if ($existingFieldDefn && $existingFieldDefn[0] != '{') { // If it's not an object, don't mess with it. - continue; + return; } // Default placeholder for select inputs if ($fieldInfo['input_type'] === 'Select') { @@ -125,6 +127,8 @@ class AfformMetadataInjector { } /** + * Determines name of the api entity based on the field name prefix + * * @param string $fieldName * @param string[] $entityList * @return string -- 2.25.1