You are here

class NodeSaveHandler in Services Client 7.2

Adds extra logic for saving remote nodes.

Hierarchy

Expanded class hierarchy of NodeSaveHandler

2 string references to 'NodeSaveHandler'
services_client_migrate_hook in ./services_client.legacy.inc
Migrate old hook with mapping to new system.
services_client_services_client_event_handler in ./services_client.plugins.inc
List availalable event handler plugins.

File

include/event.inc, line 1125

View source
class NodeSaveHandler extends EntitySaveHandler {

  /**
   * Load default plugin configuration.
   */
  public function getDefaultConfiguration() {
    $conf = parent::getDefaultConfiguration();
    $conf += array(
      'node_type' => '',
      'node_author' => 'load_remote',
      'node_author_value' => 0,
      'node_author_load_remote' => 0,
    );
    return $conf;
  }

  /**
   * Configuration form.
   */
  public function configForm(&$form, &$form_state) {
    parent::configForm($form, $form_state);
    $form['node'] = array(
      '#type' => 'fieldset',
      '#title' => t('Node configuration'),
      '#tree' => FALSE,
    );
    $form['node']['node_type'] = array(
      '#type' => 'textfield',
      '#title' => t('Node type'),
      '#description' => t('Override node type which will be sent to remote site'),
      '#default_value' => $this->config['node_type'],
    );
    $form['node']['node_author'] = array(
      '#type' => 'select',
      '#title' => t('Node author'),
      '#description' => t('How to set remote node author'),
      '#default_value' => $this->config['node_author'],
      '#options' => array(
        'value' => t('Hardcode value for each node'),
        'load_remote' => t('Map current user to remote site'),
        'no_value' => t("Don't send any author value"),
      ),
    );
    $form['node']['node_author_value'] = array(
      '#type' => 'textfield',
      '#title' => t('Hardcoded author value'),
      '#description' => t('Enter remote site UID'),
      '#default_value' => $this->config['node_author_value'],
      '#states' => array(
        'visible' => array(
          ':input[name="node_author"]' => array(
            'value' => 'value',
          ),
        ),
      ),
    );
    $form['node']['node_author_load_remote'] = array(
      '#type' => 'textfield',
      '#title' => t('Default author value'),
      '#description' => t('Enter remote site UID which will be used if mapping to remote uid will fail.'),
      '#default_value' => $this->config['node_author_load_remote'],
      '#states' => array(
        'visible' => array(
          ':input[name="node_author"]' => array(
            'value' => 'load_remote',
          ),
        ),
      ),
    );
  }

  /**
   * Save configuration.
   */
  public function configFormSubmit(&$form, &$form_state) {
    parent::configFormSubmit($form, $form_state);
    $this->config['node_type'] = $form_state['values']['node_type'];
    $this->config['node_author'] = $form_state['values']['node_author'];
    $this->config['node_author_value'] = $form_state['values']['node_author_value'];
    $this->config['node_author_load_remote'] = $form_state['values']['node_author_load_remote'];
  }

  /**
   * Before sync event.
   */
  protected function beforeSync($object) {
    if (empty($object->type) && !empty($this->config['node_type'])) {
      $object->type = $this->config['node_type'];
    }

    // Load uid from remote source
    if ($this->config['node_author'] == 'load_remote') {
      try {
        if (!empty($this
          ->getEntity()->uid)) {
          $remote_uid = $this
            ->getRemoteUserId($this
            ->getEntity()->uid);
          $object->uid = $remote_uid !== NULL ? $remote_uid : $this->config['node_author_load_remote'];
        }
      } catch (ServicesClientConnectionResponseException $e) {
        $e
          ->log();
        $object->uid = $this->config['node_author_load_remote'];
      }
    }
    elseif ($this->config['node_author'] == 'value' && !empty($this->config['node_author_value'])) {
      $object->uid = $this->config['node_author_value'];
    }
  }

  /**
   * Retrieve remote user ID.
   *
   * @return mixed|NULL
   *   If no remote entity exists returns NULL
   */
  public function getRemoteUserId($uid) {
    $account = user_load($uid);
    $result = $this
      ->getRemoteIdByUUID($account->uuid, 'user', 'user', 'uid');

    // Log remote id.
    if (!empty($result)) {
      $this
        ->log(ServicesClientLogLevel::DEVEL, "FOUND AUTHOR ID; local uuid : @local_uuid, remote id : @remote_id", array(
        '@local_uuid' => $account->uuid,
        '@remote_id' => $result,
      ));
    }
    else {
      $this
        ->log(ServicesClientLogLevel::DEVEL, "NOT AUTHOR FOUND ID; local uuid : @local_uuid", array(
        '@local_uuid' => $account->uuid,
        '@remote_id' => $result,
      ));
    }
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntitySaveHandler::doSync protected function Execute sync action. Overrides EventHandler::doSync
EntitySaveHandler::execute public function Execute event and send event to remove endpoint. Overrides EventHandler::execute
EntitySaveHandler::getMappedObject protected function Retrieve object that should be send to remote site.
EventHandler::$connection protected property Holds connection to remote server.
EventHandler::$entity protected property Processed drupal entity in event.
EventHandler::$plugin_instances protected property Initialized plugin classes.
EventHandler::$tags protected property Stores tags assigned to event.
EventHandler::addPlugin public function Add configuration plugin.
EventHandler::addTag public function Add event tag.
EventHandler::afterSync protected function Allow plugin to react on before sync event.
EventHandler::breakEditLock public function Break any edit lock for current event.
EventHandler::clearObjectCache public function Clear current object cache.
EventHandler::debugObject protected function Retrieve printed version of any variable. Should be used for logging.
EventHandler::enqueue public function Enqueue item if should be queue.
EventHandler::enqueueEntity protected function Put current entity to queue.
EventHandler::generateUuid protected function Generates a UUID v4 using PHP code.
EventHandler::getBaseUrl public function Retrieve UI base url for event.
EventHandler::getConnection protected function Retrieves event connection to remote site.
EventHandler::getConnectionId protected function Retrieve remote connection id.
EventHandler::getControlData protected function Retrieve controll data for current entity.
EventHandler::getEditLock public function Retrieve edit lock if exists (other user is editing same event).
EventHandler::getEntity public function Retrieve current event entity.
EventHandler::getEntityId public function Retrieve entity ID.
EventHandler::getEvent public function Retrieve event definition.
EventHandler::getObjectCache public function Retrieve object cached version of event.
EventHandler::getObjectCacheOrCache public function Retrieve current object from cache. If not currently in object cache adds object to object cache.
EventHandler::getPlugin public function Retrieve existing plugin.
EventHandler::getPluginInstance protected function Retrieve plugin instance by name and configuration.
EventHandler::getRemoteEntityId public function Retrieve remote entity ID. 1
EventHandler::getRemoteIdByUUID protected function Load remote ID by UUID based on remote UUID configuration.
EventHandler::getUrl public function Get path prefixed with event specific URL.
EventHandler::hasTag public function Check if event has specific tag.
EventHandler::isAutoTriggered public function Determine weather event should be fired automatically on drupal object action like node_save or node_delete.
EventHandler::isChanged public function Determine weather object has been changed by editing configuration and not which isn't stored in permanent storage.
EventHandler::isMatching public function Determine wheather entity is matching event conditions.
EventHandler::log protected function Log messages to Drupal watchdog.
EventHandler::logErrorResult protected function Log error result from services client operation.
EventHandler::objectCached public function Retrieve instance of object initialized with object cache.
EventHandler::removePlugin public function Remove existing plugin from configuration. This does is not saved to DB until save() method is called.
EventHandler::removeTag public function Remove tag from event.
EventHandler::save public function Save current event configuration to database.
EventHandler::setConnection public function Set new connection to remote site.
EventHandler::setEntity public function Set entity for current event.
EventHandler::setObjectCache public function Store current object state to object cache.
EventHandler::setPluginConfig public function Update plugin configuration. This does is not saved to DB until save() method is called.
EventHandler::skipAutosync public function Return TRUE if this entity shouldn't be send automatically to all connections.
EventHandler::__construct public function Constructor. Overrides ServicesClientPlugin::__construct
NodeSaveHandler::beforeSync protected function Before sync event. Overrides EventHandler::beforeSync
NodeSaveHandler::configForm public function Configuration form. Overrides EntitySaveHandler::configForm
NodeSaveHandler::configFormSubmit public function Save configuration. Overrides EventHandler::configFormSubmit
NodeSaveHandler::getDefaultConfiguration public function Load default plugin configuration. Overrides EntitySaveHandler::getDefaultConfiguration
NodeSaveHandler::getRemoteUserId public function Retrieve remote user ID.
ServicesClientPlugin::$config protected property Plugin specific configuration
ServicesClientPlugin::$event protected property Event definition
ServicesClientPlugin::configFormValidate public function Validate configuration form. Overrides ServicesClientConfigurableInterface::configFormValidate 1
ServicesClientPlugin::getConfiguration public function Retrieve current plugin configuration. Overrides ServicesClientConfigurableInterface::getConfiguration
ServicesClientPlugin::setConfiguration public function Set configuration of plugin. Overrides ServicesClientConfigurableInterface::setConfiguration