When debugging a Shoreditch issue with overridden fonts, we began to wonder why CiviCRM's core and custom styles are being added to every page (at least whenever civicrm_initialize() gets called), instead of only on CiviCRM pages. A generic switch for that turned out to be non-trivial, as sometimes we do want CiviCRM styles on non-CiviCRM pages (e.g. when using profiles), sometimes we want them on public CiviCRM pages, and sometimes we don't.
This led to a POC for a configuration setting for when to load CiviCRM's custom CSS (in that special case Shoreditch's CSS), allowing administrators to choose between
"always" (current and default behavior)
"all CiviCRM pages"
"all CiviCRM pages except public CiviCRM pages".
Simply adding all styles on every page may be useless on sites with more functionality than just CiviCRM. With shoreditch, that behaviour is just prone to destroying your CMS theme styles.
I think this would be a pretty great idea in general :)
Is there any reason for not wanting the custom css to be included in the public civicrm pages though? After all civicrm.css and civicrmNavigation.css are always included on those pages, so why would you not want that additional stylesheet there too?
Like @AkA84 I'm curious as to when you wouldn't want that file included when all other CiviCRM resources are being loaded. Forgive my naivety, but isn't the solution to the "overridden fonts" problem, ahem, not to apply the sans-serif font stack to the body element.
@jensschuppe It seems to me that your patch tries valiantly to paper over a deeper problem, which is that the Drupal module at minimumloads core styles in the civicrm_initialize() function. This means even an API call will cause core styles to be added to the document <head>. Trying to discover when to override this behaviour is going to be a major headache to do consistently for all CMSes and all situations.
In fact, in WordPress I'm not sure calling CRM_Core_Menu::get($path) will provide you with the correct information anyway. See my comment on GitHub for some more details on how core resources are loaded in WordPress.
My recommendation would be to look more closely at the Drupal module and see if you can separate civicrm_initialize() and civicrm_html_head() such that civicrm_html_head() is only called when needed. I'd prefer to see less code, not more!
Reflecting on what I wrote above, perhaps I could have been clearer.
I don't consider the loading of the custom stylesheet to be a problem for Core to resolve - I consider it a bug in the Drupal module. All Civi-related CSS and/or Javascript should only be loaded when a CiviCRM "entity" is being rendered in the browser - not every time civicrm_initialize() is called.
Unusually, Drupal needs parity with WordPress in this instance :-)
I still think the Shoreditch font problem is, as I said elsewhere, "a divergence from the aim to "contain" Shoreditch's CSS declarations to CiviCRM markup" and has a simple solution.
@haystack It seems very reasonable to review CiviCRM's way of loading its styles and scripts, as that is the very reason for subsequent problems with overridden style rules. Maybe consider my patch as a quick solution for treating symptoms in a backwards-compatible way.
@AkA84 A scenario for not wanting custom styles (e.g. Shoreditch's) is when using a custom theme for public CiviCRM pages. As we can not assume a theme handling all of CiviCRM's markup correctly, the admin should be able to choose between whether or not to include custom CiviCRM styles. We had a theme developed to include styles for CiviCRM's markup already, and everything was broken after starting to use Shoreditch.
@jensschuppe With respect, I believe your patch will introduce more problems than it solves. As I said, I am not persuaded that checking CRM_Core_Menu::get($path) will produce information of value in WordPress. See @totten's comments on the internal CiviCRM routes that are exceptions with respect to the menu XML data.
It seems to me that there are two stages to solving this:
Fix the Drupal-CiviCRM module which incorrectly triggers the loading of styles whenever civicrm_initialize() is called - which is responsible for custom styles being included even when a CiviCRM entity is not present on the page
Coerce Shoreditch to target only the markup it is responsible for and remove the style declarations that are outside its scope - especially those on the <body> tag
A third stage, which you allude to, is that it should be possible for modules/plugins/themes to prevent CiviCRM styles from loading, whether individually or collectively, or replace the assets with their own. I address this (for Shoreditch's internals) in this PR and have included sample code. If the same is required for other CiviCRM assets, then I suggest that a new hook is introduced which would put the responsibility for checking the conditions on to modules/plugins/themes instead of complicating Core. Doing so would allow your code to decide whether or not to include custom CiviCRM styles given the context.
Coerce Shoreditch to target only the markup it is responsible for and remove the style declarations that are outside its scope - especially those on the <body> tag
Just want to confirm that we're aware of the issues and we're working on it!
By way of an update on this, I've just pushed code to the CiviCRM Admin Utilities plugin master branch that gives you the option to disable the default CiviCRM stylesheet, the CiviCRM menu stylesheet, the Shoreditch custom stylesheet and the Shoreditch Bootstrap stylesheet on plugin's "Settings" screen. WordPress only, obviously.
What Drupal folks might be interested in is that this works without requiring any changes to CiviCRM Core - including this proposed change. This is partly because the CiviCRM-WordPress plugin separates civicrm_initialize() from the equivalent of civicrm_html_head() but also because it is easy to hook injust before the equivalent callback to civicrm_page_build() runs and disable the relevant resources before the region is rendered. I'm not sure how you'd make sure one callback runs before another in Drupal, but I'd assume there's a way. If so, then this resource-disabling code might be of interest.
Just to tag on here, I am coming to this thread after trying to track down why the Google Page Speed Insights show a very low mobile score (2%) with Shoreditch enabled, and why it jumps to a more normal level (87%) as soon as I disable the Shoreditch CSS.
@AkA84@Upperholme I think this may be a combination of issues actually manifesting itself.
I believe that CiviCRM does not differentiate between public and private pages in its CSS. In fact all CiviCRM CSS is loaded on public CiviCRM pages in general anyway. They just normally don't have a lot of styling so its less obvious. See here for discussion on this issue: #378 (closed)
This is then made worse as Shoreditch more aggressively styles the page and sometimes outside of the main CiviCRM divs. The buttons and and page margin/padding at the top is then affected as well on any page where CiviCRM is bootstrapped.
I think the long term solution to this is to solve 1 above but we have been short of time/budget to do so. I'm not sure if @Upperholme this is something you could help look at perhaps and help us progress?
Also note that @totten has indicated to me that next month he is likely to be looking at some theme related issues under some contract work he has and he may be better placed to weigh in then
Hello, finally managed to go back to this, so I was wondering if there is any update?
On Shoreditch's side we have removed the font family leak and we're currently working on decoupling any remaining drupal-related style from the theme, but unfortunately I think this won't be enough to fully address issues like this one until we can prevent in a reliable way the custom css, as good as namespaced it can be, to be injected into public pages (btw I have to image that this issue have been presenting itself with any custom css, not just with Shoreditch's?).
Over the course of the months different options had been suggested, so I wanted to ask is there now a proper way to fix it, or is one being worked on, or is this something that atm would better be tacked somehow by Shoreditch itself, etc
@AkA84 I think it's merged into the rc (5.16) - we merged it in NY code sprint - but I would probably focus on fixing Shoreditch against the rc if I were you & tagging the shoreditch release as requiring that Civi version.
The way I manage things is that I release new tags of my extensions somewhat frequently but I also increment the Civi version they work against fairly often. That means I'm not writing something to cope with many variants of core - I'm working on the latest - but given that people on older versions of Civi already have a working extension version they have the choice of 'staying stable' or upgrading to get the new feature or improvement (in which case they have to update both).
In your case given the driver is to get the most out of your time before you leave I would definitely focus on doing the best fix you can rather than making it work on older versions - i.e possibly cut a release now (with the KAM fixes) & then work on this against the rc or master & when you tag the next release increment the civi version for it (older sites will still be able to use the version you release first).
I'm trying the new theming system on a local v5.16, and I have a couple questions (of course please let me know if I should ask them somewhere else):
It looks like a theme's civicrm.css file completely overrides the original civicrm.css that ships with core. Unfortunately this doesn't work well with Shoreditch, as our theme does not replace the original stylesheet but rather builds on top of it.
I can't seem to find a way to set up the theme to extend civicrm.css, is this scenario even contemplated by the theming system?
I could of course do the following in the theme's own civicrm.css
@importurl("path/to/original/civicrm.css");/* theme's own custom css here */
but I think that would pose its own set of challenges given that the @import path would change based on what folder the theme would be placed in
In the docs there are a couple of mentions of the ability to differentiate between "frontend" and "backend" pages, and to decide where the theme gets applied
cv api setting.create theme_frontend=newyork theme_backend=newyork
They apply to all CiviCRM screens equally. If you want to provide a different look-and-feel on different screens (e.g. frontend vs backend), there's no way to selectively adjust these options.
@AkA84 so I spoke with @totten & he is going to update this with details on how to build on top of the css - there is a helper I believe for you. Hopefully by the time you come online he'll have gotten to updating this with that info ....
Last thing that is left to resolve then is how to make the theme extend the original civicrm.css file, because for now just for testing purposes the shoreditch css file manually @imports civicrm.css (cc @totten )
@AkA84 have you tried pinging @totten on chat - your tz must overlap with his somewhere - he does have a lot of people clamouring for his attention although he is aware of this.
In the meantime this might be a pointer - it's part of the addition to civix for creating themes & refers to a 'search order'