CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Recommended approach to creating a Drupal DB connection from a CiviCRM extension
Pages: [1]

Author Topic: Recommended approach to creating a Drupal DB connection from a CiviCRM extension  (Read 669 times)

mmikitka

  • I’m new here
  • *
  • Posts: 23
  • Karma: 0
Recommended approach to creating a Drupal DB connection from a CiviCRM extension
June 28, 2013, 10:35:30 am
Hello: I am creating a CiviCRM extension that needs to connect to the Drupal DB, which is installed alongside. From a CiviCRM extension, what is the recommended approach to establishing a Drupal DB connection?

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Recommended approach to creating a Drupal DB connection from a CiviCRM extension
June 29, 2013, 01:40:57 am
That's a curious question. The general goal of the extension system is to make code that doesn't depend on any particular CMS. If the extension needs to connect to the Drupal DB, then the extension will only work with Drupal -- not Joomla/WordPress.

With that said, a few possible answers:

 * Maybe you don't need to write a CiviCRM extension -- most things that an extension can do are also possible with a Drupal module (or Joomla plugin, FWIW). Doing it that way will set the correct expectation that the code depends on Drupal+CiviCRM (rather than depending on just CiviCRM).
 * There's a global configuration variable called $config->userFrameworkDSN which points to the CMS's DB. There's an example of using it at https://github.com/totten/civicrm-core/blob/master/CRM/Utils/System/Drupal.php#L565

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Recommended approach to creating a Drupal DB connection from a CiviCRM extension
June 29, 2013, 02:12:41 am
A few more asides:

* If there are one or two small things that you need to load from Drupal, and if those things are similar across the CMS's, then you might find or add a helper function somewhere under CRM/Utils/System.  This is the code that lets CiviCRM access CMS features (like "create user") while working on several different CMSs.

* If the code *must* depend on Drupal in a substantive way (and regardless of whether the code is *packaged* as a CiviCRM extension or as Drupal module), then it's generally easier to call Drupal's API functions (db_query, node_load, user_load, etc) rather using $config->userFrameworkDSN. Most of the Civi's integration code (e.g. CRM_Utils_System_Drupal) calls the Drupal API rather than the Drupal DB.

* The main decision to make is whether the code will depend directly on Drupal.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Recommended approach to creating a Drupal DB connection from a CiviCRM extension

This forum was archived on 2017-11-26.