Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Function at_commerce_css_alter does not check the existence of other commerce module, when any/many of these modules are not enabled it causes drupal to recursively check for the files many times leading to a tremendous performance impact. In my case I have none of the commerce modules and over 900ms was being spent in searching these non existent commerce modules on every page load.
Below is the patched code which simple adds a check for each depended module before fetching their paths, saving performance degradation.
/**
* Implements hook_css_alter().
*/
function at_commerce_css_alter(&$css) {
// Replace all Commerce module CSS files with our own for total control over all styles.
// Commerce module uses the BAT CSS file naming convention (base, admin, theme).
$path = drupal_get_path('theme', 'at_commerce');
// cart
if (module_exists('commerce_cart')){
$cart_theme = drupal_get_path('module', 'commerce_cart') . '/theme/commerce_cart. theme.css';
if (isset($css[$cart_theme])) {
$css[$cart_theme]['data'] = $path . '/css/commerce/commerce_cart.theme.css';
$css[$cart_theme]['group'] = 1;
}
}
// checkout
if (module_exists('commerce_checkout')){
$checkout_base = drupal_get_path('module', 'commerce_checkout') . '/theme/ commerce_checkout.base.css';
$checkout_admin = drupal_get_path('module', 'commerce_checkout') . '/theme/ commerce_checkout.admin.css';
$checkout_theme = drupal_get_path('module', 'commerce_checkout') . '/theme/ commerce_checkout.theme.css';
if (isset($css[$checkout_base])) {
$css[$checkout_base]['data'] = $path . '/css/commerce/commerce_checkout.base. css';
$css[$checkout_base]['group'] = 1;
}
if (isset($css[$checkout_admin])) {
$css[$checkout_admin]['data'] = $path . '/css/commerce/commerce_checkout.admin. css';
$css[$checkout_admin]['group'] = 1;
}
if (isset($css[$checkout_theme])) {
$css[$checkout_theme]['data'] = $path . '/css/commerce/commerce_checkout.theme. css';
$css[$checkout_theme]['group'] = 1;
}
}
// customer
if (module_exists('commerce_customer')){
$customer_admin = drupal_get_path('module', 'commerce_customer') . '/theme/ commerce_customer.admin.css';
if (isset($css[$customer_admin])) {
$css[$customer_admin]['data'] = $path . '/css/commerce/commerce_customer.admin. css';
$css[$customer_admin]['group'] = 1;
}
}
// file (contrib)
if (module_exists('commerce_file')){
$file_forms = drupal_get_path('module', 'commerce_file') . '/theme/commerce_file. forms.css';
if (isset($css[$file_forms])) {
$css[$file_forms]['data'] = $path . '/css/commerce/commerce_file.forms.css';
$css[$file_forms]['group'] = 1;
}
}
// line items
if (module_exists('commerce_line_item')){
$line_item_admin = drupal_get_path('module', 'commerce_line_item') . '/theme/ commerce_line_item.admin.css';
$line_item_theme = drupal_get_path('module', 'commerce_line_item') . '/theme/ commerce_line_item.theme.css';
if (isset($css[$line_item_admin])) {
$css[$line_item_admin]['data'] = $path . '/css/commerce/commerce_line_item.admin. css';
$css[$line_item_admin]['group'] = 1;
}
if (isset($css[$line_item_theme])) {
$css[$line_item_theme]['data'] = $path . '/css/commerce/commerce_line_item.theme. css';
$css[$line_item_theme]['group'] = 1;
}
}
// order
if (module_exists('commerce_order')){
$order_admin = drupal_get_path('module', 'commerce_order') . '/theme/ commerce_order.admin.css';
$order_theme = drupal_get_path('module', 'commerce_order') . '/theme/ commerce_order.theme.css';
if (isset($css[$order_admin])) {
$css[$order_admin]['data'] = $path . '/css/commerce/commerce_order.admin.css';
$css[$order_admin]['group'] = 1;
}
if (isset($css[$order_theme])) {
$css[$order_theme]['data'] = $path . '/css/commerce/commerce_order.theme.css';
$css[$order_theme]['group'] = 1;
}
}
// payment
if (module_exists('commerce_payment')){
$payment_admin = drupal_get_path('module', 'commerce_payment') . '/theme/ commerce_payment.admin.css';
$payment_theme = drupal_get_path('module', 'commerce_payment') . '/theme/ commerce_payment.theme.css';
if (isset($css[$payment_admin])) {
$css[$payment_admin]['data'] = $path . '/css/commerce/commerce_payment.admin. css';
$css[$payment_admin]['group'] = 1;
}
if (isset($css[$payment_theme])) {
$css[$payment_theme]['data'] = $path . '/css/commerce/commerce_payment.theme. css';
$css[$payment_theme]['group'] = 1;
}
}
// price
if (module_exists('commerce_price')){
$price_theme = drupal_get_path('module', 'commerce_price') . '/theme/ commerce_price.theme.css';
if (isset($css[$price_theme])) {
$css[$price_theme]['data'] = $path . '/css/commerce/commerce_price.theme.css';
$css[$price_theme]['group'] = 1;
}
}
// product
if (module_exists('commerce_product')){
$product_admin = drupal_get_path('module', 'commerce_product') . '/theme/ commerce_product.admin.css';
$product_theme = drupal_get_path('module', 'commerce_product') . '/theme/ commerce_product.theme.css';
if (isset($css[$product_admin])) {
$css[$product_admin]['data'] = $path . '/css/commerce/commerce_product.admin. css';
$css[$product_admin]['group'] = 1;
}
if (isset($css[$product_theme])) {
$css[$product_theme]['data'] = $path . '/css/commerce/commerce_product.theme. css';
$css[$product_theme]['group'] = 1;
}
}
// tax
if (module_exists('commerce_tax')){
$tax_admin = drupal_get_path('module', 'commerce_tax') . '/theme/commerce_tax. admin.css';
$tax_theme = drupal_get_path('module', 'commerce_tax') . '/theme/commerce_tax. theme.css';
if (isset($css[$tax_admin])) {
$css[$tax_admin]['data'] = $path . '/css/commerce/commerce_tax.admin.css';
$css[$tax_admin]['group'] = 1;
}
if (isset($css[$tax_theme])) {
$css[$tax_theme]['data'] = $path . '/css/commerce/commerce_tax.theme.css';
$css[$tax_theme]['group'] = 1;
}
}
}
Comments
Comment #1
toamit CreditAttribution: toamit commentedComment #2
Jeff Burnz CreditAttribution: Jeff Burnz commentedIt's a good idea to do the checks, its not really critical but it is important. To be frank I always meant to do this, not sure why it never got done.
Comment #3
toamit CreditAttribution: toamit commented> its not really critical.
Sorry, I disagree, my site was incurring 900ms latency cost for no benefit. It was also quite troublesome to find where the problem was.
>To be frank I always meant to do this, not sure why it never got done.
Great. It is a simple thing but with good benefit to most.
Comment #5
Jeff Burnz CreditAttribution: Jeff Burnz commentedI have pushed the fix so this is done. I'll cut a new minor version release shortly.
Comment #6
toamit CreditAttribution: toamit commented@Jeff Thanks for quick work.