}
$this->addedBundles[$bundle->name] = TRUE;
- // If an item is already assigned to a region, we'll respect that.
- // Otherwise, we'll use defaults.
- $pickRegion = function ($snippet) {
- if (isset($snippet['settings'])) {
- return $this->getSettingRegion($snippet['region'] ?? NULL)->_name;
+ // Ensure that every asset has a region.
+ $bundle->filter(function($snippet) {
+ if (empty($snippet['region'])) {
+ $snippet['region'] = isset($snippet['settings'])
+ ? $this->getSettingRegion()->_name
+ : self::DEFAULT_REGION;
}
- else {
- return $snippet['region'] ?? self::DEFAULT_REGION;
- }
- };
-
- $byRegion = [];
- foreach ($bundle->getAll() as $snippet) {
- $snippet['region'] = $pickRegion($snippet);
- $byRegion[$snippet['region']][$snippet['name']] = $snippet;
- }
+ return $snippet;
+ });
+ $byRegion = CRM_Utils_Array::index(['region', 'name'], $bundle->getAll());
foreach ($byRegion as $regionName => $snippets) {
CRM_Core_Region::instance($regionName)->merge($snippets);
}
public $name;
/**
+ * @param string|NULL $name
+ * @param string[]|NULL $types
+ * List of resource-types to permit in this bundle. NULL for a default list.
*/
- public function __construct($name = NULL) {
+ public function __construct($name = NULL, $types = NULL) {
$this->name = $name;
- $this->types = ['script', 'scriptFile', 'scriptUrl', 'settings', 'style', 'styleFile', 'styleUrl'];
+ $this->types = $types ?: ['script', 'scriptFile', 'scriptUrl', 'settings', 'style', 'styleFile', 'styleUrl'];
}
}
$_GET = $this->originalGet;
}
+ /**
+ * Make two bundles (multi-regional). Add them to CRM_Core_Resources.
+ * Ensure that the resources land in the right regions.
+ */
+ public function testAddBundle() {
+ $foo = new CRM_Core_Resources_Bundle('foo', ['scriptUrl', 'styleUrl', 'markup']);
+ $bar = new CRM_Core_Resources_Bundle('bar', ['scriptUrl', 'styleUrl', 'markup']);
+
+ $foo->addScriptUrl('http://example.com/foo.js', 100, 'testAddBundle_foo');
+ $foo->add(['markup' => 'Hello, foo', 'region' => 'page-header']);
+ $bar->addScriptUrl('http://example.com/bar.js', 100, 'testAddBundle_bar');
+ $bar->add(['markup' => 'Hello, bar', 'region' => 'page-header']);
+ $foo->addStyleUrl('http://example.com/shoes.css');
+
+ $this->res->addBundle($foo);
+ $this->res->addBundle([$bar]);
+
+ $getPropsByRegion = function($region, $key) {
+ $props = [];
+ foreach (CRM_Core_Region::instance($region)->getAll() as $snippet) {
+ if (isset($snippet[$key])) {
+ $props[] = $snippet[$key];
+ }
+ }
+ return $props;
+ };
+
+ $this->assertEquals(
+ ['http://example.com/foo.js'],
+ $getPropsByRegion('testAddBundle_foo', 'scriptUrl')
+ );
+ $this->assertEquals(
+ ['http://example.com/bar.js'],
+ $getPropsByRegion('testAddBundle_bar', 'scriptUrl')
+ );
+ $this->assertEquals(
+ ['', 'Hello, foo', 'Hello, bar'],
+ $getPropsByRegion('page-header', 'markup')
+ );
+ $this->assertEquals(
+ ['http://example.com/shoes.css'],
+ $getPropsByRegion('page-footer', 'styleUrl')
+ );
+ }
+
public function testAddScriptFile() {
$this->res
->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')