Drupal8: getUrlPath: avoid relying on the deprecated 'q' variable
Context: Symbiotic has an extension for theming that uses this trick to detect whether it's running in a frontend or backend form (to avoid loading our CSS on the frontend):
function adminimore_civicrm_config(&$config) {
$path = CRM_Utils_System::getUrlPath();
$item = CRM_Core_Menu::get($path);
$resources = CRM_Core_Resources::singleton();
// if item is not known, assume it's public (e.g. wordpress shortcode)
if ($item && !CRM_Utils_Array::value('is_public', $item)) {
$resources->addStyleFile(ADMINIMORE_RESOURCE, 'css/civicrm-admin.css', 15, 'html-header');
$resources->addStyleFile(ADMINIMORE_RESOURCE, 'css/civicrm-buttons.css', 15, 'html-header');
}
$resources->addStyleFile(ADMINIMORE_RESOURCE, 'css/civicrm-menu.css', 15, 'html-header');
_adminimore_civix_civicrm_config($config);
}
In Drupal8, we were having weird problems where the CSS would sometimes not load. If we refreshed, it loaded, but after some time, the bug would appear again and we would stumble on a screen using the default CiviCRM CSS.
After some poking around, it seems that Drupal8 deprecated the 'q' variable, which CiviCRM is still using in CRM_Utils_System::getUrlPath()
.
I did a quick patch to test a workaround, which so far seems to be working:
public static function getUrlPath() {
if (CRM_Core_Config::singleton()->userFramework == 'Drupal8') {
if (class_exists('Drupal') && \Drupal::hasContainer()) {
$path = \Drupal::service('path.current')->getPath();
// Remove '/' prefix. Ex: '/civicrm/contribute' becomes 'civicrm/contribute'.
if ($path) {
$path = substr($path, 1);
// Remove the language prefix, if present
// The URL returned by Drupal randomly includes the language prefix, sometimes not.
if (preg_match('/^\w\w\//', $path)) {
$path = substr($path, 3);
}
return $path;
}
}
}
if (isset($_GET[CRM_Core_Config::singleton()->userFrameworkURLVar])) {
return $_GET[CRM_Core_Config::singleton()->userFrameworkURLVar];
}
return NULL;
}
A cleaner solution might be to check if the $config->userSystem->getUrlPath()
function exists, and if it does, call it?