"Class not found" error on `\Civi\*` because hook_civicrm_config runs after hook_civicrm_container
hook_civicrm_config
is responsible for telling the autoloader which classes exist in extensions, so must run before any other hooks.
However:
-
hook_civicrm_fieldOptions
is called beforehook_civicrm_config
; this is issue #1132 (closed), and there's a PR. -
hook_civicrm_container
runs beforehook_civicrm_config
; this ticket discusses that.
Here are some examples (with backtraces):
- Issue #2334 (closed) appears to be this issue.
There's also the backtrace below, which happens when my Drupal cron runs.
This only happens on classes in the \Civi
namespace, which isn't as common in extensions, which is why this isn't more widespread.
I don't know enough about hook_civicrm_config
and hook_civicrm_container
, but it seems we should guarantee that hook_civicrm_config
always runs first.
Mar 02 15:54:01 [debug] $pdfapi-backtrace = #0 /var/www/mysite.org/web/sites/all/civicrm-custom/extensions/org.civicoop.pdfapi/pdfapi.php(14): CRM_Core_Error::backtrace("pdfapi-backtrace", TRUE)
#1 /var/www/mysite.org/web/sites/all/modules/civicrm/CRM/Utils/Hook.php(271): pdfapi_civicrm_container(Object(Symfony\Component\DependencyInjection\ContainerBuilder))
#2 /var/www/mysite.org/web/sites/all/modules/civicrm/CRM/Utils/Hook/DrupalBase.php(73): CRM_Utils_Hook->runHooks((Array:99), "civicrm_container", 1, Object(Symfony\Component\DependencyInjection\ContainerBuilder), NULL, NULL, NULL, NULL, NULL)
#3 /var/www/mysite.org/web/sites/all/modules/civicrm/Civi/Core/CiviEventDispatcher.php(168): CRM_Utils_Hook_DrupalBase->invokeViaUF(1, Object(Symfony\Component\DependencyInjection\ContainerBuilder), NULL, NULL, NULL, NULL, NULL, "civicrm_container")
#4 /var/www/mysite.org/web/sites/all/modules/civicrm/vendor/symfony/event-dispatcher/EventDispatcher.php(214): Civi\Core\CiviEventDispatcher::delegateToUF(Object(Civi\Core\Event\GenericHookEvent), "hook_civicrm_container", Object(Civi\Core\CiviEventDispatcher))
#5 /var/www/mysite.org/web/sites/all/modules/civicrm/vendor/symfony/event-dispatcher/EventDispatcher.php(44): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch((Array:1), "hook_civicrm_container", Object(Civi\Core\Event\GenericHookEvent))
#6 /var/www/mysite.org/web/sites/all/modules/civicrm/Civi/Core/CiviEventDispatcher.php(129): Symfony\Component\EventDispatcher\EventDispatcher->dispatch("hook_civicrm_container", Object(Civi\Core\Event\GenericHookEvent))
#7 /var/www/mysite.org/web/sites/all/modules/civicrm/CRM/Utils/Hook.php(167): Civi\Core\CiviEventDispatcher->dispatch("hook_civicrm_container", Object(Civi\Core\Event\GenericHookEvent))
#8 /var/www/mysite.org/web/sites/all/modules/civicrm/CRM/Utils/Hook.php(2523): CRM_Utils_Hook->invoke((Array:1), Object(Symfony\Component\DependencyInjection\ContainerBuilder), NULL, NULL, NULL, NULL, NULL, "civicrm_container")
#9 /var/www/mysite.org/web/sites/all/modules/civicrm/Civi/Core/Container.php(334): CRM_Utils_Hook::container(Object(Symfony\Component\DependencyInjection\ContainerBuilder))
#10 /var/www/mysite.org/web/sites/all/modules/civicrm/Civi/Core/Container.php(69): Civi\Core\Container->createContainer()
#11 /var/www/mysite.org/web/sites/all/modules/civicrm/Civi/Core/Container.php(564): Civi\Core\Container->loadContainer()
#12 /var/www/mysite.org/web/sites/all/modules/civicrm/CRM/Core/Config.php(93): Civi\Core\Container::boot(TRUE)
#13 /var/www/mysite.org/web/sites/all/modules/civicrm/drupal/civicrm.module(227): CRM_Core_Config::singleton()
#14 /var/www/mysite.org/web/sites/all/modules/civicrm/drupal/modules/civicrmtheme/civicrmtheme.module(98): civicrm_initialize()
#15 /var/www/mysite.org/web/includes/module.inc(965): civicrmtheme_custom_theme()
#16 /var/www/mysite.org/web/includes/menu.inc(1760): module_invoke_all("custom_theme")
#17 /var/www/mysite.org/web/includes/menu.inc(1782): menu_get_custom_theme(TRUE)
#18 /var/www/mysite.org/web/includes/common.inc(5376): menu_set_custom_theme()
#19 /var/www/mysite.org/web/includes/bootstrap.inc(2552): _drupal_bootstrap_full()
#20 /usr/local/src/drush7/lib/Drush/Boot/DrupalBoot7.php(74): drupal_bootstrap(7)
#21 /usr/local/src/drush7/includes/bootstrap.inc(313): Drush\Boot\DrupalBoot7->bootstrap_drupal_full()
#22 /usr/local/src/drush7/includes/bootstrap.inc(429): drush_bootstrap(5, 6)
#23 /usr/local/src/drush7/lib/Drush/Boot/BaseBoot.php(56): drush_bootstrap_to_phase(6)
#24 /usr/local/src/drush7/drush.php(70): Drush\Boot\BaseBoot->bootstrap_and_dispatch()
#25 /usr/local/src/drush7/drush.php(11): drush_main()
#26 {main}