class EntitySaveHandler in Services Client 7.2
General entity save handler.
Hierarchy
- class \ServicesClientPlugin implements ServicesClientConfigurableInterface
- class \EventHandler
- class \EntitySaveHandler
- class \EventHandler
Expanded class hierarchy of EntitySaveHandler
4 string references to 'EntitySaveHandler'
- hook_services_client_event_handler in ./
services_client.api.php - ctools hook for Event Hanlder plugin discover.
- ServicesClientBaseWebTestCase::createSCEvent in tests/
services_client.test - Create new services client event.
- ServicesClientPluginsTestCase::createFakeEvent in tests/
plugin.test - Create new event.
- services_client_services_client_event_handler in ./
services_client.plugins.inc - List availalable event handler plugins.
File
- include/
event.inc, line 844
View source
class EntitySaveHandler extends EventHandler {
public function getDefaultConfiguration() {
$config = parent::getDefaultConfiguration();
$config['mapping'] = array();
return $config;
}
public function configForm(&$form, &$form_state) {
// Show conditions
$mapping = array();
foreach ($this->config['mapping'] as $uuid => $plugin) {
try {
$handler = $this
->getPluginInstance($plugin['type'], $plugin['config'], $uuid);
$mapping[] = array(
$handler
->getReaderSummary(),
$handler
->getFormatterSummary(),
implode(' | ', array(
l(t('Edit'), $this
->getUrl('plugin/mapping/' . $uuid . '/edit')),
l(t('Remove'), $this
->getUrl('plugin/mapping/' . $uuid . '/remove'), array(
'query' => array(
'token' => drupal_get_token($uuid),
),
)),
)),
);
} catch (Exception $e) {
watchdog_exception('services_client', $e);
}
}
$form['mapping']['title'] = array(
'#markup' => '<h2>' . t('Mapping') . '</h2>',
);
$form['mapping']['table'] = array(
'#theme' => 'table',
'#header' => array(
t('Source (local)'),
t('Destination (remote)'),
t('Actions'),
),
'#rows' => $mapping,
'#empty' => t('There are no mappings added'),
);
$form['mapping']['add_mapping'] = array(
'#theme_wrappers' => array(
'container',
),
'#markup' => l('+ ' . t('Add mapping'), $this
->getUrl('add_plugin/mapping')),
);
parent::configForm($form, $form_state);
}
/**
* Retrieve object that should be send to remote site.
*
* @return stdClass
* Mapped object.
*/
protected function getMappedObject() {
$entity = $this
->getEntity();
$mapped = new stdClass();
foreach ($this->config['mapping'] as $uuid => $plugin) {
try {
$handler = $this
->getPluginInstance($plugin['type'], $plugin['config'], $uuid);
$source = $handler
->getReader()
->read($entity);
if (!empty($source)) {
$formatted = $handler
->getFormatter()
->format($source);
if (!empty($formatted['key'])) {
// Merge fields
if (isset($mapped->{$formatted['key']}) && is_array($mapped->{$formatted['key']})) {
$mapped->{$formatted['key']} = array_replace_recursive($mapped->{$formatted['key']}, $formatted['value']);
}
else {
$mapped->{$formatted['key']} = $formatted['value'];
}
}
}
} catch (Exception $e) {
watchdog_exception('services_client', $e);
}
}
// Automatically map UUIDs
if (isset($entity->uuid) && !isset($mapped->uuid)) {
$mapped->uuid = $entity->uuid;
}
$this
->log(ServicesClientLogLevel::DEVEL, "MAPPED DATA; source : <pre>@source</pre>, mapped: <pre>@mapped</pre>", array(
'@source' => $this
->debugObject($entity),
'@mapped' => $this
->debugObject($mapped),
));
return $mapped;
}
/**
* Execute event and send event to remove endpoint.
*
* @return ServicesClientEventResult
*/
public function execute() {
// Load mapped object.
$object = $this
->getMappedObject();
// Set services client control data.
$control = $this
->getControlData();
$control
->setData($object);
$this
->beforeSync($object);
// Allow other modules to alter mapping.
drupal_alter('services_client_mapped_object', $this, $object);
// Create action result.
$result = new ServicesClientEventResult();
$result
->setHandler($this);
$result->event = $this
->getEvent();
$result->object = $object;
$result->entity = $this
->getEntity();
$result->entity_type = $result->event->entity_type;
// Allow other modules to react on before request.
module_invoke_all('services_client_before_request', $this, $object);
if ($control
->isLooping()) {
$this
->log(ServicesClientLogLevel::ERROR, "LOOP; entity_type: @type, entity_id: @id, event: @event", array(
'@type' => $this->event->entity_type,
'@id' => $this
->getEntityId(),
'@event' => $this->event->name,
), WATCHDOG_ERROR);
// Mark error as loop
$result->error_type = ServicesClientErrorType::LOOP;
}
else {
$this
->log(ServicesClientLogLevel::INFO, "SENDING; connection : @connection, event : @event, entity_type : @entity_type, entity_id : @entity_id, uuid : @uuid, object : <pre>@object</pre>", array(
'@event' => $this->event->name,
'@connection' => $this
->getConnectionId(),
'@entity_type' => $this->event->entity_type,
'@entity_id' => $this
->getEntityId(),
'@uuid' => isset($this
->getEntity()->uuid) ? $this
->getEntity()->uuid : NULL,
'@object' => $this
->debugObject($object),
));
try {
$result->response = $this
->doSync($object);
$result->request = $this
->getConnection()
->getRequest();
} catch (ServicesClientConnectionResponseException $e) {
$e
->log();
$result->error_message = $e
->getServicesMessage();
$result->error_code = $e
->getErrorCode();
$result->request = $e->request;
$result->response = $e->response;
// Determien what error type, by default we assume remote server failed.
$error_type = ServicesClientErrorType::REMOTE_SERVER;
// Logic errors that came from remote site, like can't login
if ($e
->getErrorCode() >= 400 && $e
->getErrorCode() < 500) {
$error_type = ServicesClientErrorType::REMOTE_LOGIC;
}
elseif ($e
->getErrorCode() < 100) {
$error_type = ServicesClientErrorType::NETWORK;
}
// Set error type
$result->error_type = $error_type;
} catch (Exception $e) {
$result->error_message = $e
->getMessage();
$result->error_type = ServicesClientErrorType::UNKNOWN;
}
}
$this
->logErrorResult($result);
$this
->afterSync($object, $result);
$this
->log(ServicesClientLogLevel::DEVEL, "RESULT; <pre>@result</pre>", array(
'@result' => print_r($result, TRUE),
));
// Allow other modules to react on after request.
module_invoke_all('services_client_after_request', $this, $object, $result);
return $result;
}
/**
* Execute sync action.
*/
protected function doSync($object) {
$object = (array) $object;
$id = $this
->getRemoteEntityId();
if (empty($id)) {
$this
->getConnection()
->create($this->config['resource'], $object);
return $this
->getConnection()
->getResponse();
}
else {
$this
->getConnection()
->update($this->config['resource'], $id, $object);
return $this
->getConnection()
->getResponse();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntitySaveHandler:: |
public | function |
Configuration form. Overrides EventHandler:: |
2 |
EntitySaveHandler:: |
protected | function |
Execute sync action. Overrides EventHandler:: |
|
EntitySaveHandler:: |
public | function |
Execute event and send event to remove endpoint. Overrides EventHandler:: |
|
EntitySaveHandler:: |
public | function |
Retrieve default event configuration. Overrides EventHandler:: |
2 |
EntitySaveHandler:: |
protected | function | Retrieve object that should be send to remote site. | |
EventHandler:: |
protected | property | Holds connection to remote server. | |
EventHandler:: |
protected | property | Processed drupal entity in event. | |
EventHandler:: |
protected | property | Initialized plugin classes. | |
EventHandler:: |
protected | property | Stores tags assigned to event. | |
EventHandler:: |
public | function | Add configuration plugin. | |
EventHandler:: |
public | function | Add event tag. | |
EventHandler:: |
protected | function | Allow plugin to react on before sync event. | |
EventHandler:: |
protected | function | Allow plugin to react on before sync event. | 2 |
EventHandler:: |
public | function | Break any edit lock for current event. | |
EventHandler:: |
public | function | Clear current object cache. | |
EventHandler:: |
public | function |
Submit handler; Store plugin configuration. Overrides ServicesClientPlugin:: |
2 |
EventHandler:: |
protected | function | Retrieve printed version of any variable. Should be used for logging. | |
EventHandler:: |
public | function | Enqueue item if should be queue. | |
EventHandler:: |
protected | function | Put current entity to queue. | |
EventHandler:: |
protected | function | Generates a UUID v4 using PHP code. | |
EventHandler:: |
public | function | Retrieve UI base url for event. | |
EventHandler:: |
protected | function | Retrieves event connection to remote site. | |
EventHandler:: |
protected | function | Retrieve remote connection id. | |
EventHandler:: |
protected | function | Retrieve controll data for current entity. | |
EventHandler:: |
public | function | Retrieve edit lock if exists (other user is editing same event). | |
EventHandler:: |
public | function | Retrieve current event entity. | |
EventHandler:: |
public | function | Retrieve entity ID. | |
EventHandler:: |
public | function | Retrieve event definition. | |
EventHandler:: |
public | function | Retrieve object cached version of event. | |
EventHandler:: |
public | function | Retrieve current object from cache. If not currently in object cache adds object to object cache. | |
EventHandler:: |
public | function | Retrieve existing plugin. | |
EventHandler:: |
protected | function | Retrieve plugin instance by name and configuration. | |
EventHandler:: |
public | function | Retrieve remote entity ID. | 1 |
EventHandler:: |
protected | function | Load remote ID by UUID based on remote UUID configuration. | |
EventHandler:: |
public | function | Get path prefixed with event specific URL. | |
EventHandler:: |
public | function | Check if event has specific tag. | |
EventHandler:: |
public | function | Determine weather event should be fired automatically on drupal object action like node_save or node_delete. | |
EventHandler:: |
public | function | Determine weather object has been changed by editing configuration and not which isn't stored in permanent storage. | |
EventHandler:: |
public | function | Determine wheather entity is matching event conditions. | |
EventHandler:: |
protected | function | Log messages to Drupal watchdog. | |
EventHandler:: |
protected | function | Log error result from services client operation. | |
EventHandler:: |
public | function | Retrieve instance of object initialized with object cache. | |
EventHandler:: |
public | function | Remove existing plugin from configuration. This does is not saved to DB until save() method is called. | |
EventHandler:: |
public | function | Remove tag from event. | |
EventHandler:: |
public | function | Save current event configuration to database. | |
EventHandler:: |
public | function | Set new connection to remote site. | |
EventHandler:: |
public | function | Set entity for current event. | |
EventHandler:: |
public | function | Store current object state to object cache. | |
EventHandler:: |
public | function | Update plugin configuration. This does is not saved to DB until save() method is called. | |
EventHandler:: |
public | function | Return TRUE if this entity shouldn't be send automatically to all connections. | |
EventHandler:: |
public | function |
Constructor. Overrides ServicesClientPlugin:: |
|
ServicesClientPlugin:: |
protected | property | Plugin specific configuration | |
ServicesClientPlugin:: |
protected | property | Event definition | |
ServicesClientPlugin:: |
public | function |
Validate configuration form. Overrides ServicesClientConfigurableInterface:: |
1 |
ServicesClientPlugin:: |
public | function |
Retrieve current plugin configuration. Overrides ServicesClientConfigurableInterface:: |
|
ServicesClientPlugin:: |
public | function |
Set configuration of plugin. Overrides ServicesClientConfigurableInterface:: |