Merge pull request #18704 from mlutfy/partListingOutput
[civicrm-core.git] / CRM / Core / Resources / Common.php
index c85abeaec0a1f8cd0df96490cc7629377eac688b..13b86a233dfe6c9688d419279c3f1daf2ced912b 100644 (file)
@@ -16,6 +16,36 @@ class CRM_Core_Resources_Common {
 
   const REGION = 'html-header';
 
+  /**
+   * Create a "basic" (generic) bundle.
+   *
+   * The bundle goes through some lifecycle events (like `hook_alterBundle`).
+   *
+   * To define default content for a basic bundle, you may either give an
+   * `$init` function or subscribe to `hook_alterBundle`.
+   *
+   * @param string $name
+   *   Symbolic name of the bundle.
+   * @param callable|NULL $init
+   *   Optional initialization function. Populate default resources.
+   *   Signature: `function($bundle): void`
+   *   Example: `function myinit($b) { $b->addScriptFile(...)->addStyleFile(...); }`
+   * @param string|string[] $types
+   *   List of resource-types to permit in this bundle. NULL for a default list.
+   *   Example: ['styleFile', 'styleUrl']
+   *   The following aliases are allowed: '*all*', '*default*', '*script*', '*style*'
+   * @return CRM_Core_Resources_Bundle
+   */
+  public static function createBasicBundle($name, $init = NULL, $types = NULL) {
+    $bundle = new CRM_Core_Resources_Bundle($name, $types);
+    if ($init !== NULL) {
+      $init($bundle);
+    }
+    CRM_Utils_Hook::alterBundle($bundle);
+    $bundle->fillDefaults();
+    return $bundle;
+  }
+
   /**
    * The 'bundle.bootstrap3' service is a collection of resources which are
    * loaded when a page needs to support Boostrap CSS v3.
@@ -47,7 +77,7 @@ class CRM_Core_Resources_Common {
     );
 
     CRM_Utils_Hook::alterBundle($bundle);
-    self::useRegion($bundle, self::REGION);
+    $bundle->fillDefaults();
     return $bundle;
   }
 
@@ -76,7 +106,7 @@ class CRM_Core_Resources_Common {
     $bundle->addStyleFile('civicrm', 'css/crm-i.css', -101);
 
     CRM_Utils_Hook::alterBundle($bundle);
-    self::useRegion($bundle, self::REGION);
+    $bundle->fillDefaults();
     return $bundle;
   }
 
@@ -133,7 +163,7 @@ class CRM_Core_Resources_Common {
     ]);
 
     CRM_Utils_Hook::alterBundle($bundle);
-    self::useRegion($bundle, self::REGION);
+    $bundle->fillDefaults();
     return $bundle;
   }
 
@@ -273,21 +303,4 @@ class CRM_Core_Resources_Common {
     return $items;
   }
 
-  /**
-   * Ensure that all elements of the bundle are in the same region.
-   *
-   * @param CRM_Core_Resources_Bundle $bundle
-   * @param string $region
-   * @return CRM_Core_Resources_Bundle
-   */
-  protected static function useRegion($bundle, $region) {
-    $bundle->filter(function ($s) use ($region) {
-      if ($s['type'] !== 'settings' && !isset($s['region'])) {
-        $s['region'] = $region;
-      }
-      return $s;
-    });
-    return $bundle;
-  }
-
 }