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

CRM-21093: Move CiviCRM initialization from service constructor to method. #495

Merged
merged 3 commits into from
Oct 9, 2017
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
7 changes: 4 additions & 3 deletions civicrm.module
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function civicrm_entity_extra_field_info() {
* to the user display.
*/
function civicrm_user_view(array &$build, \Drupal\user\UserInterface $account, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();

// We need the $contact_id so that we know what data to pull out of Civicrm.
// And we need the contact_id of the current user ($current_contact_id) so
Expand Down Expand Up @@ -177,7 +177,7 @@ function civicrm_user_view(array &$build, \Drupal\user\UserInterface $account, \
* based on the $contact_id's contact type.
*/
function _civicrm_get_profiles($contact_id) {
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();
$profiles = array();
$ctype = \CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact_id, 'contact_type');

Expand All @@ -202,6 +202,7 @@ function _civicrm_get_profiles($contact_id) {
*/
function civicrm_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$civicrm = \Drupal::service('civicrm');
$civicrm->initialize();
$html = \CRM_Core_BAO_UFGroup::getEditHTML(NULL, '', 1, TRUE, FALSE, NULL, FALSE, $civicrm->getCtype());

$form['civicrm_profile_register'] = array(
Expand All @@ -214,7 +215,7 @@ function civicrm_form_user_register_form_alter(&$form, &$form_state, $form_id) {
* Validation function for additional profile form fields attached to user registration form.
*/
function _civicrm_user_register_form_validate(&$form, &$form_state) {
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();
$errors = CRM_Core_BAO_UFGroup::isValid(NULL, '', TRUE);

if (is_array($errors)) {
Expand Down
4 changes: 2 additions & 2 deletions civicrm.user.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function civicrm_user_insert(AccountInterface $account) {
* Implements hook_user_update().
*/
function civicrm_user_update(AccountInterface $account) {
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();

// Update primary email address of contact if it has changed.
$contact_id = \CRM_Core_BAO_UFMatch::getContactId($account->id());
Expand All @@ -52,6 +52,6 @@ function civicrm_user_update(AccountInterface $account) {
* Implements hook_user_delete().
*/
function civicrm_user_delete(AccountInterface $account) {
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();
\CRM_Core_BAO_UFMatch::deleteUser($account->id());
}
10 changes: 5 additions & 5 deletions drush/civicrm.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ function civicrm_drush_help($section) {
* Implementation of command 'civicrm-ext-list'
*/
function drush_civicrm_ext_list() {
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();
try{
$result = civicrm_api3('extension', 'get', array(
'options' => array(
Expand Down Expand Up @@ -575,7 +575,7 @@ function drush_civicrm_ext_list() {
* Implementation of command 'civicrm-ext-install'
*/
function drush_civicrm_ext_install($extension_name) {
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();
try{
$result = civicrm_api('extension', 'install', array('key' => $extension_name, 'version' => 3));
if($result['values'] && $result['values'] == 1) {
Expand All @@ -597,7 +597,7 @@ function drush_civicrm_ext_install($extension_name) {
* Implementation of command 'civicrm-ext-disable'
*/
function drush_civicrm_ext_disable($extension_name) {
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();
try{
$result = civicrm_api('extension', 'disable', array('key' => $extension_name, 'version' => 3));
if($result['values'] && $result['values'] == 1) {
Expand All @@ -619,7 +619,7 @@ function drush_civicrm_ext_disable($extension_name) {
* Implementation of command 'civicrm-ext-uninstall'
*/
function drush_civicrm_ext_uninstall($extension_name) {
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();
try{
$result = civicrm_api('extension', 'uninstall', array('key' => $extension_name, 'version' => 3));
if($result['values'] && $result['values'] == 1) {
Expand Down Expand Up @@ -1507,7 +1507,7 @@ function drush_civicrm_api() {
break;
}

\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();

global $user;
CRM_Core_BAO_UFMatch::synchronize($user, FALSE, 'Drupal', 'Individual'
Expand Down
4 changes: 2 additions & 2 deletions modules/civicrm_views/civicrm_views.views.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function civicrm_views_views_data() {
$data = array();

try {
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();
}
catch (Exception $e) {
\Drupal::logger('civicrm_views')->error("Unable to initialize CiviCRM. Aborting hook_views_data().");
Expand Down Expand Up @@ -84,7 +84,7 @@ function civicrm_views_views_data() {

function civicrm_views_views_data_alter(&$data) {
try {
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();
}
catch (Exception $e) {
\Drupal::logger('civicrm_views')->error("Unable to initialize CiviCRM. Aborting hook_views_data_alter().");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class CivicrmPseudoconstant extends FieldPluginBase {
public function __construct(array $configuration, $plugin_id, $plugin_definition, Civicrm $civicrm) {
parent::__construct($configuration, $plugin_id, $plugin_definition);

$civicrm->initialize();

$this->pseudovalues = call_user_func_array($this->definition['pseudo callback'], $this->definition['pseudo arguments']);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class CivicrmInOperator extends InOperator {
public function __construct(array $configuration, $plugin_id, $plugin_definition, Civicrm $civicrm) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$civicrm->initialize();
}

public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class CivicrmRelationship extends RelationshipPluginBase {
public function __construct(array $configuration, $plugin_id, $plugin_definition, Civicrm $civicrm) {
parent::__construct($configuration, $plugin_id, $plugin_definition);

$civicrm->initialize();

// relationshipType() returns information about relations as array with fields
// 'name_a_b', 'name_b_a', 'contact_type_a' and 'contact_type_b'.
$this->relationships[0] = t('Any');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class CivicrmUFMatch extends RelationshipPluginBase {
public function __construct(array $configuration, $plugin_id, $plugin_definition, Civicrm $civicrm) {
parent::__construct($configuration, $plugin_id, $plugin_definition);

$civicrm->initialize();

$this->civicrm_current_domain = \CRM_Core_Config::domainID();

$this->civicrm_domains['current'] = t('Current domain');
Expand Down
19 changes: 18 additions & 1 deletion src/Civicrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@
use Drupal\Core\Session\AccountInterface;

class Civicrm {

protected $initialized = FALSE;

/**
* Initialize CiviCRM. Call this function from other modules too if
* they use the CiviCRM API.
*/
public function __construct() {
public function initialize() {
if ($this->initialized) {
return;
}

// Get ready for problems
$docLinkInstall = "http://wiki.civicrm.org/confluence/display/CRMDOC/Drupal+Installation+Guide";
$docLinkTrouble = "http://wiki.civicrm.org/confluence/display/CRMDOC/Installation+and+Configuration+Trouble-shooting";
Expand Down Expand Up @@ -43,9 +50,18 @@ public function __construct() {

// Initialize the system by creating a config object
\CRM_Core_Config::singleton();

// Mark CiviCRM as initialized.
$this->initialized = TRUE;
}

public function isInitialized() {
return $this->initialized;
}

public function invoke($args) {
$this->initialize();

// Civicrm will echo/print directly to stdout. We need to capture it
// so that we can return the output as a renderable array.
ob_start();
Expand All @@ -61,6 +77,7 @@ public function invoke($args) {
* @param string $contact_type
*/
public function synchronizeUser(AccountInterface $account, $contact_type = 'Individual') {
$this->initialize();
\CRM_Core_BAO_UFMatch::synchronize($account, FALSE, 'Drupal', $this->getCtype($contact_type));
}

Expand Down
2 changes: 1 addition & 1 deletion src/CivicrmPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static function create(ContainerInterface $container) {
public function permissions() {
// Initialize civicrm.
// @Todo: Inject this via container injection instead.
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();

$permissions = [];
foreach (\CRM_Core_Permission::basicPermissions() as $permission => $title) {
Expand Down
3 changes: 1 addition & 2 deletions src/Form/UserProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class UserProfile extends FormBase {
protected $uf_group;

public function __construct(Civicrm $civicrm) {
// We don't do anything with the Civicrm service, only ensure that it
// has been initialized.
$civicrm->initialize();
}

static function create(ContainerInterface $container) {
Expand Down
1 change: 1 addition & 0 deletions src/PathProcessor/CivicrmPathProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function processInbound($path, Request $request) {
if (strpos($path, '/civicrm/') === 0) {
// initialize civicrm
$civicrm = new Civicrm();
$civicrm->initialize();
// fetch civicrm menu items
$items = \CRM_Core_Menu::items();
$longest = '';
Expand Down
4 changes: 1 addition & 3 deletions src/Plugin/Block/CivicrmBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
*/
class CivicrmBlock extends BlockBase implements ContainerFactoryPluginInterface {
public function __construct(Civicrm $civicrm, array $configuration, $plugin_id, array $plugin_definition) {
// We don't do anything with the Civicrm service, only ensure that it
// has been initialized.

// Mark all CiviCRM blocks as uncachable.
$configuration['cache']['max_age'] = 0;
parent::__construct($configuration, $plugin_id, $plugin_definition);
$civicrm->initialize();
}

static public function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
Expand Down
3 changes: 1 addition & 2 deletions src/Plugin/Derivative/CivicrmBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

class CivicrmBlock extends DeriverBase implements ContainerDeriverInterface {
public function __construct(Civicrm $civicrm) {
// We don't do anything with the Civicrm service, only ensure that it
// has been initialized.
$civicrm->initialize();
}

static public function create(ContainerInterface $container, $base_plugin_id) {
Expand Down
3 changes: 1 addition & 2 deletions src/Plugin/Derivative/LocalTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

class LocalTasks extends DeriverBase implements ContainerDeriverInterface {
public function __construct(Civicrm $civicrm) {
// We don't do anything with the Civicrm service, only ensure that it
// has been initialized.
$civicrm->initialize();
}

static public function create(ContainerInterface $container, $base_plugin_id) {
Expand Down
2 changes: 1 addition & 1 deletion src/Routing/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function routes() {
$collection = new RouteCollection();

// Initialize CiviCRM.
\Drupal::service('civicrm');
\Drupal::service('civicrm')->initialize();

$items = \CRM_Core_Menu::items();

Expand Down