SugarCRM Logic Hook Improvements

Edit 1: In response to comments from Jason Eggers I have updated my closure concept to include labels so that they can be identified and uninstalled.

Edit 2: Chad Hutchins further contributed to this concept with a basic outline of a helper class. This post has been updated accordingly.

Like a lot of you SugarCRM dev’s, I find myself creating simple logic hooks fairly often. While this is a simple task I feel that it could be even simpler. Lets look at the current process of creating a logic hook.

1. Create the file /custom/modules/{module}/logic_hooks.php

2. Create your logic hook class at /custom/modules/{module}/logic_hooks_class.php

Two easy steps. That’s all it takes. While this is pretty straight forward I cant help but feel like its an unnecessary amount of work for executing 1 function.

If logic_hooks.php were to support closures (PHP 5.3.0 +) we could achieve all of the above in 1 file (logic_hooks.php) and eliminate the need for the “logic_hooks_class” class all together.

Here is a brief example of how it could potentially work:

In 1 file and roughly 10% of the initial code required we have included all of our logic which makes the logic hook easier to maintain and quicker to write. Personally, this would eliminate at least 75% of my logic hook related files.

Somewhere deeper in the SugarCRM platform this code could be executed with something along the following lines:

To further make using logic hooks more efficient Chad Hutchins has contributed the following helper class concept to make adding and removing logic hooks that much easier.

Thoughts? Opinions? I would love to hear some feedback on this concept!

Using SugarCRM’s New RESTful API With Guzzle

Today I saw a post from Mr. John Mertic that described how to interact with the new API via cURL. While it’s a great piece of information, I find working with the Guzzle library to be much more efficient due to Guzzle’s support for event listeners. That and I hate working directly with cURL.

That said, lets get down to business and start working with the new API.

First, lets update composer.json to install Guzzle

{
    "require": {
        "guzzle/guzzle": "~3.1.1"
    }
}

Next, install the library via composer

$ composer install

Now with Guzzle auto loaded we can start to have our fun with the new API.

<?php
use Guzzle\Common\Event;
use Guzzle\Http\Client;

// specify the REST web service to interact with
$url = 'http://sugarinstance/rest/v10';

// And admin username/password
$username = 'username';
$password = 'password';

$client = new Client($url);

//Authenticate and retrieve an OAuth2 Token
$request = $client->post('oauth2/token', null, array(
  'grant_type' => 'password',
  'client_id' => 'sugar',
  'username' => $username,
  'password' => $password,
));

$results = $request->send()->json();

$token = $results['access_token'];

//Register an Event Listener to automatically include 
//the OAuth2 token on all requests to the REST web service
$client->getEventDispatcher()->addListener(
  'request.before_send', function(Event $event) {
    $event['request']->setHeader('OAuth-Token', $token);
  }
);

//Retrieve all documents related to the specified account
$request = $client->get('/Accounts/' . $account_id . '/link/documents');
$result = $request->send()->json();

var_dump($result);

As you can see, using Guzzle makes interacting with the new API a breeze and has an added bonus of making the code much easier to understand.

Stay tuned, on my next post I’ll show how you can use the SugarCRM 7 API Wrapper Class to accomplish the same task in just a few lines of code.