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 »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Drupal and CiviCRM: Basic code architecture
Pages: [1]

Author Topic: Drupal and CiviCRM: Basic code architecture  (Read 1204 times)

kung

  • I’m new here
  • *
  • Posts: 17
  • Karma: 0
  • CiviCRM version: 5
  • CMS version: Drupal 6 or 7
  • MySQL version: 5
  • PHP version: 5
Drupal and CiviCRM: Basic code architecture
February 15, 2012, 03:21:03 pm
I have CiviCRM running in Drupal 7.  I want to manage contacts via php code. For example, I want my php code to create a contact, and then immediately assign some custom field values to it.
Where do I put this code, and how can I trigger it? I've created Drupal modules; should I use that approach? How would I trigger my code? I tried writing a php file that requires api.php correctly and calls various civicrm_api()  functions, but nothing happens when I browse to the file's URL.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Drupal and CiviCRM: Basic code architecture
February 15, 2012, 03:45:28 pm
Yes, you should write a drupal module. I think there are some examples that ship with Civi but basically you should use drupal's form system to add a url to drupal's 'understanding of the world' that calls a civi function that you write.

If you haven't done anything like this before I'd suggest trying to track down a copy of Pro Drupal Development to help you
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Drupal and CiviCRM: Basic code architecture
February 16, 2012, 07:44:17 am
Quote from: kung on February 15, 2012, 03:21:03 pm
I tried writing a php file that requires api.php correctly and calls various civicrm_api()  functions, but nothing happens when I browse to the file's URL.

As noted, you should be writing a Drupal module, not just a PHP file. It is possible to use a PHP file but you need to bootstrap Drupal--the easier and preferred method is via a Drupal module.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

kung

  • I’m new here
  • *
  • Posts: 17
  • Karma: 0
  • CiviCRM version: 5
  • CMS version: Drupal 6 or 7
  • MySQL version: 5
  • PHP version: 5
Re: Drupal and CiviCRM: Basic code architecture
February 17, 2012, 10:34:46 am
Thanks!  I've now written my module and it works.  But it seems that doing it that way I have to trigger my code via some Drupal or CiviCRM event, and I didn't really want that.  I wanted to call my module's functions as parts of a cron run's maintenance tasks. 
To me, it's a problem with the whole "hooks" way of thinking. Or am I missing something?     

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Drupal and CiviCRM: Basic code architecture
February 17, 2012, 11:38:36 am

You can invoke your code via a cron run also (we do that for quite a few cron jobs)

To take advantage of drupal's hook stuff, u bootstrap drupal in your php script

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

kung

  • I’m new here
  • *
  • Posts: 17
  • Karma: 0
  • CiviCRM version: 5
  • CMS version: Drupal 6 or 7
  • MySQL version: 5
  • PHP version: 5
Re: Drupal and CiviCRM: Basic code architecture
February 17, 2012, 12:19:26 pm
Okay! I've got it running as a hook, which is nice, AND now I can also call CiviCRM functions directly, without hooks. Here's how I did the latter:

//Bootstrap Drupal
chdir('/my/drupal/root/');
define('DRUPAL_ROOT', '/my/drupal/root/');  //This is a Drupal 7 issue -- it doesn't find the DRUPAL_ROOT otherwise (see http://drupal.org/node/1024624)
require_once DRUPAL_ROOT . './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

//Call CiviCRM functions
$params = array(
  'first_name' => 'abc3abc3abc3',
  'last_name' => 'xyz3xyz3xyz3',
  'contact_type' => 'Individual',
  'version' => 3,
  'email' => 'xyz3xyz3xyz3@yahoo.com'
);

civicrm_initialize( );
$results = civicrm_api("Contact","create", $params);
print_r($results);

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Drupal and CiviCRM: Basic code architecture

This forum was archived on 2017-11-26.