From 7ed57b0404c162b4e6eac529c1d1f2f41815d024 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 13 May 2021 00:07:01 -0400 Subject: [PATCH] Afform - hide disabled contact types & entities from disabled components/extensions --- .../Civi/AfformAdmin/AfformAdminMeta.php | 38 +++++++++---------- ext/afform/admin/afformEntities/Activity.php | 1 - ext/afform/admin/afformEntities/Household.php | 2 - .../admin/afformEntities/Individual.php | 2 - .../admin/afformEntities/Organization.php | 2 - 5 files changed, 19 insertions(+), 26 deletions(-) diff --git a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php index eace44476d..9146d03667 100644 --- a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php +++ b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php @@ -51,14 +51,16 @@ class AfformAdminMeta { } /** - * @param $entityName - * @return array + * Get info about an api entity, with special handling for contact types + * @param string $entityName + * @return array|null */ - public static function getApiEntity($entityName) { - if (in_array($entityName, ['Individual', 'Household', 'Organization'])) { - $contactTypes = \CRM_Contact_BAO_ContactType::basicTypeInfo(); + public static function getApiEntity(string $entityName) { + $contactTypes = \CRM_Contact_BAO_ContactType::basicTypeInfo(); + if (isset($contactTypes[$entityName])) { return [ 'entity' => 'Contact', + 'contact_type' => $entityName, 'label' => $contactTypes[$entityName]['label'], ]; } @@ -66,6 +68,10 @@ class AfformAdminMeta { ->addWhere('name', '=', $entityName) ->addSelect('title', 'icon') ->execute()->first(); + if (!$info) { + // Disabled contact type or nonexistent api entity + return NULL; + } return [ 'entity' => $entityName, 'label' => $info['title'], @@ -127,21 +133,15 @@ class AfformAdminMeta { if (is_dir($dir)) { // Scan for entities foreach (glob($dir . 'afformEntities/*.php') as $file) { - $entity = include $file; - $afformEntity = basename($file, '.php'); - // Contact pseudo-entities (Individual, Organization, Household) get special treatment, - // notably their fields are pre-loaded since they are both commonly-used and nonstandard - if (!empty($entity['contact_type'])) { - // Skip disabled contact types - if (!isset($contactTypes[$entity['contact_type']])) { - continue; - } - $entity['label'] = $contactTypes[$entity['contact_type']]['label']; - } - elseif (empty($entity['label']) || empty($entity['icon'])) { - $entity += self::getApiEntity($entity['entity']); + $entityInfo = include $file; + $entityName = basename($file, '.php'); + $apiInfo = self::getApiEntity($entityInfo['entity'] ?? $entityName); + // Skip disabled contact types & entities from disabled components/extensions + if (!$apiInfo) { + continue; } - $data['entities'][$afformEntity] = $entity; + $entityInfo += $apiInfo; + $data['entities'][$entityName] = $entityInfo; } // Scan for input types foreach (glob($dir . 'ang/afGuiEditor/inputType/*.html') as $file) { diff --git a/ext/afform/admin/afformEntities/Activity.php b/ext/afform/admin/afformEntities/Activity.php index ae8a651eba..a18f17f3b7 100644 --- a/ext/afform/admin/afformEntities/Activity.php +++ b/ext/afform/admin/afformEntities/Activity.php @@ -1,6 +1,5 @@ 'Activity', 'defaults' => "{'url-autofill': '1'}", 'boilerplate' => [ ['#tag' => 'af-field', 'name' => 'subject'], diff --git a/ext/afform/admin/afformEntities/Household.php b/ext/afform/admin/afformEntities/Household.php index cf569b3432..159388b072 100644 --- a/ext/afform/admin/afformEntities/Household.php +++ b/ext/afform/admin/afformEntities/Household.php @@ -1,7 +1,5 @@ 'Contact', - 'contact_type' => 'Household', 'defaults' => "{ data: { contact_type: 'Household', diff --git a/ext/afform/admin/afformEntities/Individual.php b/ext/afform/admin/afformEntities/Individual.php index cc580425e4..ef2d43b350 100644 --- a/ext/afform/admin/afformEntities/Individual.php +++ b/ext/afform/admin/afformEntities/Individual.php @@ -1,7 +1,5 @@ 'Contact', - 'contact_type' => 'Individual', 'defaults' => "{ data: { contact_type: 'Individual', diff --git a/ext/afform/admin/afformEntities/Organization.php b/ext/afform/admin/afformEntities/Organization.php index 5335b6ddc2..bd0f602b7e 100644 --- a/ext/afform/admin/afformEntities/Organization.php +++ b/ext/afform/admin/afformEntities/Organization.php @@ -1,7 +1,5 @@ 'Contact', - 'contact_type' => 'Organization', 'defaults' => "{ data: { contact_type: 'Organization', -- 2.25.1