CRM_Core_Resources_Common - Add skeleton for bundles, 'coreStyles' and 'coreResources'
authorTim Otten <totten@civicrm.org>
Sun, 23 Aug 2020 00:54:42 +0000 (17:54 -0700)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 3 Sep 2020 22:02:17 +0000 (08:02 +1000)
CRM/Core/Resources/Common.php [new file with mode: 0644]
CRM/Utils/Hook.php
Civi/Core/Container.php

diff --git a/CRM/Core/Resources/Common.php b/CRM/Core/Resources/Common.php
new file mode 100644 (file)
index 0000000..008b60a
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Define some common, global lists of resources.
+ */
+class CRM_Core_Resources_Common {
+
+  const REGION = 'html-header';
+
+  /**
+   * The 'bundle.coreStyles' service is a collection of resources used on some
+   * non-Civi pages (wherein Civi may be mixed-in).
+   *
+   * @param string $name
+   *   i.e. 'coreStyles'
+   * @return \CRM_Core_Resources_Bundle
+   */
+  public static function createStyleBundle($name) {
+    $bundle = new CRM_Core_Resources_Bundle($name);
+    // TODO
+    CRM_Utils_Hook::alterBundle($bundle);
+    self::useRegion($bundle, self::REGION);
+    return $bundle;
+  }
+
+  /**
+   * The 'bundle.coreResources' service is a collection of resources
+   * shared by Civi pages (ie pages where Civi controls rendering).
+   *
+   * @param string $name
+   *   i.e. 'coreResources'
+   * @return \CRM_Core_Resources_Bundle
+   */
+  public static function createFullBundle($name) {
+    $bundle = new CRM_Core_Resources_Bundle($name);
+    // TODO
+    CRM_Utils_Hook::alterBundle($bundle);
+    self::useRegion($bundle, self::REGION);
+    return $bundle;
+  }
+
+  /**
+   * 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;
+  }
+
+}
index 381b69f8f68521ce164e56ebf4fe05589a8130a8..17532b2bb8b8cea9aede4434740b55488d233c6a 100644 (file)
@@ -421,6 +421,22 @@ abstract class CRM_Utils_Hook {
     return self::singleton()->invoke(['op', 'objectName', 'objectId', 'links', 'mask', 'values'], $op, $objectName, $objectId, $links, $mask, $values, 'civicrm_links');
   }
 
+  /**
+   * Alter the contents of a resource bundle (ie a collection of JS/CSS/etc).
+   *
+   * TIP: $bundle->add*() and $bundle->filter() should be useful for
+   * adding/removing/updating items.
+   *
+   * @param CRM_Core_Resources_Bundle $bundle
+   * @return null
+   * @see CRM_Core_Resources_CollectionInterface::add()
+   * @see CRM_Core_Resources_CollectionInterface::filter()
+   */
+  public static function alterBundle($bundle) {
+    return self::singleton()
+      ->invoke(['bundle'], $bundle, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, 'civicrm_alterBundle');
+  }
+
   /**
    * This hook is invoked during the CiviCRM form preProcess phase.
    *
index ee8b214e7fd1ce570f09bc988d04474019d41ef0..5a36afacf4d76ed8ed141d969f8b99027ae163ba 100644 (file)
@@ -206,6 +206,12 @@ class Container {
       []
     ))->setPublic(TRUE);
 
+    $container->setDefinition('bundle.coreStyles', new Definition('CRM_Core_Resources_Bundle', ['coreStyles']))
+      ->setFactory('CRM_Core_Resources_Common::createStyleBundle');
+
+    $container->setDefinition('bundle.coreResources', new Definition('CRM_Core_Resources_Bundle', ['coreResources']))
+      ->setFactory('CRM_Core_Resources_Common::createFullBundle');
+
     $container->setDefinition('pear_mail', new Definition('Mail'))
       ->setFactory('CRM_Utils_Mail::createMailer');