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

Give the custom css file a 'name' to make it easier to manipulate in hook_civicrm_alterBundle() #20278

Merged
merged 1 commit into from
May 19, 2021
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: 1 addition & 1 deletion CRM/Core/Resources/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static function createStyleBundle($name) {
$config = CRM_Core_Config::singleton();
if (!empty($config->customCSSURL)) {
$customCSSURL = Civi::resources()->addCacheCode($config->customCSSURL);
$bundle->addStyleUrl($customCSSURL, 99);
$bundle->addStyleUrl($customCSSURL, ['weight' => 99, 'name' => 'civicrm:css/custom.css']);
Copy link
Contributor

@deb1990 deb1990 May 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a very good idea. A small suggestion though.
Do we need the :css thingy? How about just making it civicrm:custom.css.
Its not a huge deal, but civicrm:css/custom.css kind of makes it looks like a URL. When civicrm:custom.css looks more of an identifier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes ... what's in a name??

Resources added via addStyleFile() have a default name combining the extension name and the relative path so the other resources added by createStyleBundle() are civicrm:css/civicrm.css and civicrm:css/crm-i.css. So following that pattern of names I went with civicrm:css/custom.css. However,

  1. the file isn't provided by core so arguably isn't civicrm - but it is added by core.
  2. the path is not necessarily css - but is quite likely to be [civicrm.files]/css
  3. the file name is not necessarily custom.css- but is a common option

Providing the name is unique I don't think it matters much. The purpose is to let people target that custom css file, whatever it's actual filename or path. I did consider civicrm:custom.css but thought the omission of the css/ part might cause more question since the name then does not follow the pattern of the other names in the bundle.

Happy to change the name if there's consensus on a better one.

And while you're all thinking about this, perhaps you could also comment on https://lab.civicrm.org/dev/core/-/issues/2460 which led to this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. It makes sense, no need to change in that case.

}
if (!Civi::settings()->get('disable_core_css')) {
$bundle->addStyleFile('civicrm', 'css/civicrm.css', -99);
Expand Down
12 changes: 12 additions & 0 deletions tests/phpunit/CRM/Core/Resources/BundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,16 @@ public function testFillDefaults() {
$this->assertEquals('page-header', $bundle->get('cheese')['region']);
}

/**
* Test creation of coreStyles bundle
*/
public function testCoreStylesBundle() {
$config = CRM_Core_Config::singleton();
$config->customCSSURL = "http://example.com/css/custom.css";
$bundle = CRM_Core_Resources_Common::createStyleBundle('coreStyles');
$this->assertEquals('civicrm:css/civicrm.css', $bundle->get('civicrm:css/civicrm.css')['name']);
$this->assertEquals('civicrm:css/crm-i.css', $bundle->get('civicrm:css/crm-i.css')['name']);
$this->assertEquals('civicrm:css/custom.css', $bundle->get('civicrm:css/custom.css')['name']);
}

}