Skip to content

Commit

Permalink
CRM-18792 - CRM_Core_Theme - Add helper for loading CSS files from th…
Browse files Browse the repository at this point in the history
…emes

CRM-18792 - CRM_Core_Resources - Load civicrm.css through theme system

CRM-18792 - Rename `CRM_Core_Theme` to `\Civi\Core\Theme`

CRM-18792 - Civi\Core\Theme - Remove statics

WIP

CRM_Core_Resources::addCoreStyles - Revert change

CRM-18792 - addStyleFile - Always pass through to theme. Support fallback.

Rename `Civi\Core\Theme` to `Civi\Core\Themes`

The class manages a list of themes -- not just a single theme.

CRM-18792 - Add org.civicrm.demotheme

CRM-18792 - Add uncommitted test files (`Civi\Core\Themes`)

CRM-18792 - Fix regression in CRM_Core_ResourceTest

CRM-18792 - Theme naming - Use prefix '_' for hidden themes

This cleans up a few things:

 * Previously, there was a special case for using FALLBACK in `search_order`.
 * If you're creating a multitheme extension, you may want to define a base theme
   (which is extended by the others). Previously, you were required to show this
   base theme as a user-selectable option. Now, it can be hidden.
 * There was a bug where `resolveUrl()` would sometimes call the wrong callback.
   (It used resolver for `$active` instead of `$themeKey`.)

CRM-18792 - Themes - File overrides and excludes should use same naming

Previously, when using `addStyleFile($cssExt,$css$file)`, the file overrides
and exlcudes would combine them differently e.g.

 * For `addStyleFile('civicrm','css/bootstrap.css')`
   * Override `css/bootstrap.css`
   * Exclude `civicrm:css/bootstrap.css`
 * For `('org.foo.bar','css/bang.css')`
   * Override `org.foo.bar-css/bang.css`
   * Exclude `org.foo.bar:css/bang.css`

Now, they use the same notation:

 * For `addStyleFile('civicrm','css/bootstrap.css')`
   * Override `css/bootstrap.css`
   * Exclude `css/bootstrap.css`
 * For `('org.foo.bar','css/bang.css')`
   * Override `org.foo.bar-css/bang.css`
   * Exclude `org.foo.bar-css/bang.css`

"Display Preferences" - Add the `theme_backend` and `theme_frontend` settings

hook_civicrm_activeTheme - Allow extensions and CMS modules to choose active theme

CRM_Utils_Hook::themes() - Tweak docblock

Civi\Core\Themes - Move cache from `short` to `long`

Remove tools/extensions/org.civicrm.demotheme

Fix merge ahem errors
  • Loading branch information
totten authored and eileenmcnaughton committed Jun 15, 2019
1 parent 583a991 commit ebb1e2c
Show file tree
Hide file tree
Showing 20 changed files with 796 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CRM/Admin/Form/Preferences/Display.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class CRM_Admin_Form_Preferences_Display extends CRM_Admin_Form_Preferences {
'sort_name_format' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
'menubar_position' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
'menubar_color' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
'theme_backend' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
'theme_frontend' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
];

/**
Expand Down
34 changes: 21 additions & 13 deletions CRM/Core/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ public function addScriptFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $regi
$domain = ($translate === TRUE) ? $ext : $translate;
$this->addString($this->strings->get($domain, $this->getPath($ext, $file), 'text/javascript'), $domain);
}
$this->resolveFileName($file, $ext);
return $this->addScriptUrl($this->getUrl($ext, $file, TRUE), $weight, $region);
$url = $this->getUrl($ext, $this->filterMinify($ext, $file), TRUE);
return $this->addScriptUrl($url, $weight, $region);
}

/**
Expand Down Expand Up @@ -427,8 +427,12 @@ public function addString($text, $domain = 'civicrm') {
* @return CRM_Core_Resources
*/
public function addStyleFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
$this->resolveFileName($file, $ext);
return $this->addStyleUrl($this->getUrl($ext, $file, TRUE), $weight, $region);
/** @var Civi\Core\Themes $theme */
$theme = Civi::service('themes');
foreach ($theme->resolveUrls($theme->getActiveThemeKey(), $ext, $file) as $url) {
$this->addStyleUrl($url, $weight, $region);
}
return $this;
}

/**
Expand Down Expand Up @@ -937,18 +941,22 @@ public static function getEntityRefMetadata() {
}

/**
* In debug mode, look for a non-minified version of this file
* Determine the minified file name.
*
* @param string $fileName
* @param string $extName
*/
private function resolveFileName(&$fileName, $extName) {
if (CRM_Core_Config::singleton()->debug && strpos($fileName, '.min.') !== FALSE) {
$nonMiniFile = str_replace('.min.', '.', $fileName);
if ($this->getPath($extName, $nonMiniFile)) {
$fileName = $nonMiniFile;
* @param string $ext
* @param string $file
* @return string
* An updated $fileName. If a minified version exists and is supported by
* system policy, the minified version will be returned. Otherwise, the original.
*/
public function filterMinify($ext, $file) {
if (CRM_Core_Config::singleton()->debug && strpos($file, '.min.') !== FALSE) {
$nonMiniFile = str_replace('.min.', '.', $file);
if ($this->getPath($ext, $nonMiniFile)) {
$file = $nonMiniFile;
}
}
return $file;
}

/**
Expand Down
57 changes: 57 additions & 0 deletions CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,63 @@ public static function alterMenu(&$items) {
);
}

/**
* A theme is a set of CSS files which are loaded on CiviCRM pages. To register a new
* theme, add it to the $themes array. Use these properties:
*
* - ext: string (required)
* The full name of the extension which defines the theme.
* Ex: "org.civicrm.themes.greenwich".
* - title: string (required)
* Visible title.
* - help: string (optional)
* Description of the theme's appearance.
* - url_callback: mixed (optional)
* A function ($themes, $themeKey, $cssExt, $cssFile) which returns the URL(s) for a CSS resource.
* Returns either an array of URLs or PASSTHRU.
* Ex: \Civi\Core\Themes\Resolvers::simple (default)
* Ex: \Civi\Core\Themes\Resolvers::none
* - prefix: string (optional)
* A prefix within the extension folder to prepend to the file name.
* - search_order: array (optional)
* A list of themes to search.
* Generally, the last theme should be "*fallback*" (Civi\Core\Themes::FALLBACK).
* - excludes: array (optional)
* A list of files (eg "civicrm:css/bootstrap.css" or "$ext:$file") which should never
* be returned (they are excluded from display).
*
* @param array $themes
* List of themes, keyed by name.
* @return null
* the return value is ignored
*/
public static function themes(&$themes) {
return self::singleton()->invoke(1, $themes,
self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject,
'civicrm_themes'
);
}

/**
* The activeTheme hook determines which theme is active.
*
* @param string $theme
* The identifier for the theme. Alterable.
* Ex: 'greenwich'.
* @param array $context
* Information about the current page-request. Includes some mix of:
* - page: the relative path of the current Civi page (Ex: 'civicrm/dashboard').
* - themes: an instance of the Civi\Core\Themes service.
* @return null
* the return value is ignored
*/
public static function activeTheme(&$theme, $context) {
return self::singleton()->invoke(array('theme', 'context'), $theme, $context,
self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject,
'civicrm_activeTheme'
);
}

/**
* This hook is called for declaring managed entities via API.
*
Expand Down
5 changes: 5 additions & 0 deletions Civi/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ public function createContainer() {
[]
));

$container->setDefinition('themes', new Definition(
'Civi\Core\Themes',
[]
));

$container->setDefinition('pear_mail', new Definition('Mail'))
->setFactory('CRM_Utils_Mail::createMailer');

Expand Down
Loading

0 comments on commit ebb1e2c

Please sign in to comment.