Managed - Noisily deprecate unnamed managed entities
authorcolemanw <coleman@civicrm.org>
Fri, 1 Sep 2023 20:18:39 +0000 (16:18 -0400)
committercolemanw <coleman@civicrm.org>
Sat, 2 Sep 2023 15:50:54 +0000 (11:50 -0400)
CRM/Core/ManagedEntities.php

index dc67cfdb2107c90c2c9b3b9214dffaca831ae148..0c16cb264469cb95aa10885ec33a668552dd2679 100644 (file)
@@ -525,8 +525,16 @@ class CRM_Core_ManagedEntities {
     $declarations = [];
     CRM_Utils_Hook::managed($declarations, $modules);
     $this->validate($declarations);
-    foreach (array_keys($declarations) as $name) {
-      $declarations[$name] += ['name' => $name];
+    // FIXME: Some well-meaning developer added this a long time ago to support associative arrays
+    // that use the array index as the declaration name. But it probably never worked, because by the time it gets to this point,
+    // lots of implementations of `hook_civicrm_managed()` would have run `$declarations = array_merge($declarations, [...])`
+    // which would have reset the indexes.
+    // Adding a noisy deprecation notice for now, then we should remove this block:
+    foreach ($declarations as $index => $declaration) {
+      if (empty($declaration['name'])) {
+        CRM_Core_Error::deprecatedWarning(sprintf('Managed entity "%s" declared by extension "%s" without a name.', $index, $declaration['module']));
+        $declarations[$index] += ['name' => $index];
+      }
     }
     return $declarations;
   }