Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-18792, dev/core#378 - Create CSS theming subsystem #12929

Merged
merged 1 commit into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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