项目作者: colorfield

项目描述 :
CiviCRM API wrapper and helpers for Drupal 8
高级语言: PHP
项目地址: git://github.com/colorfield/civicrm_tools.git
创建时间: 2018-04-23T15:26:58Z
项目社区:https://github.com/colorfield/civicrm_tools

开源协议:

下载


CiviCRM Tools

CiviCRM API wrapper for Drupal 8.
The wrapper is currently being extended by syntactic sugar and REST helpers.

The API code is originally extracted from the
CiviCRM Entity module.
It has started as a separation of concern and is subject to evolve by

  • implementing other methods like clone, getTokens, getSingle, …
  • defining API version (currently v3)
  • providing pagination helpers
  • providing syntactic sugar over the API for complex relations
  • providing default exception handlers.

Dependencies

CiviCRM Core
and CiviCRM Drupal 8 module.

Due to the current development of CiviCRM for Drupal, these packages are
not currently required by Composer so you have to install them manually with your
preferred setup.

Here is one possible setup
that has been used during the development of this module.

API Documentation

The API is available as a Drupal service.

  1. // Prefer dependency injection.
  2. $civiCrmApi = \Drupal::service('civicrm_tools.api');

Get

  1. $params = [
  2. 'email' => 'donald@example.com',
  3. ];
  4. $result = $civiCrmApi->get('Contact', $params);

Create

  1. $params = [
  2. 'first_name' => 'Elijah',
  3. 'last_name' => 'Baley',
  4. 'contact_type' => 'Individual',
  5. ]
  6. $result = $civiCrmApi->create('Contact', $params);

Delete

  1. $params = [
  2. 'id' => 42,
  3. ];
  4. $result = $civiCrmApi->delete('Contact', $params);

Syntactic sugar

Some other services are on their way.

Contact

  1. // Prefer dependency injection.
  2. $civiCrmContact = \Drupal::service('civicrm_tools.contact');
  3. // Smart group id and optinal parameters
  4. $civiCrmContact->getFromSmartGroup(42, []);
  5. $civiCrmContact->getFromGroups([1,2]);
  6. // User id and domain id
  7. $civiCrmContact->getFromUserId(1, 1);
  8. // Domain id
  9. $civiCrmContact->getFromLoggedInUser(1);
  10. // Contact id and domain id
  11. $civiCrmContact->getUserFromContactId(1, 1);

Group

  1. // Prefer dependency injection.
  2. $civiCrmGroup = \Drupal::service('civicrm_tools.group');
  3. // Contact id, load: get the full group array or only the group id
  4. $civiCrmGroup->getGroupsFromContact(1, TRUE);

Database

Prerequisite: add the CiviCRM database reference in your Drupal settings.php

  1. $databases['civicrm']['default'] = array (
  2. 'database' => 'civicrm_db_name',
  3. 'username' => 'civicrm_db_user',
  4. 'password' => 'civicrm_db_password',
  5. 'prefix' => '',
  6. 'host' => '127.0.0.1',
  7. 'port' => '3306',
  8. 'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  9. 'driver' => 'mysql',
  10. );

Then

  1. // Prefer dependency injection.
  2. $civiCrmDatabase = \Drupal::service('civicrm_tools.database');
  3. $query = 'SELECT * FROM {civicrm_log}';
  4. $result = $civiCrmDatabase->execute($query);

Or

  1. // Switch to a custom CiviCRM database.
  2. Database::setActiveConnection('civicrm');
  3. $db = Database::getConnection();
  4. $query = $db->query("SELECT group_id FROM {civicrm_group_contact} WHERE contact_id = :contact_id AND status = :status", [
  5. ':contact_id' => $contact_id,
  6. ':status' => 'Added',
  7. ]);
  8. $queryResult = $query->fetchAll();
  9. // Switch back to the default database (Drupal).
  10. Database::setActiveConnection();

The CiviCRM Tools REST sub module aims to provide several endpoints for
CiviCRM and Drupal entities.
It could also make use of entity Normalizer, … to provide fields for JSON API, and core REST.

Currently, the only available endpoint gets Drupal users by CiviCRM group.

Possible use cases:

  • a callback for a Single Sign On application, that provides a set of permissions based on the CiviCRM Group.
  • a decoupled application

Get Drupal users by CiviCRM group

/api/civicrm_tools/users/group/{group_id}

Authenticate with BasicAuth.
The authenticated user needs the permission to view user profiles.

Usage

  1. curl --include --request GET --user admin:admin --header 'Content-type: application/json' http://example.com/api/civicrm_tools/users/group/42