public function execute($selected_entity_uuid, $selected_entity_type, &$context) { // Load the entity. $entity = Entity::loadByUUID($selected_entity_uuid, $selected_entity_type); if (!$entity) { drupal_set_message(t('An invalid entity was found.'), 'error'); $this->updateContextMessages(null, $context); return; } // Get the transaction session, and fail if we don't have it. $transaction_session = TransactionSession::getFromSession(); if (!$transaction_session) { $current_set =& _batch_current_set(); $current_set['success'] = false; drupal_set_message(t('We lost the transaction session.'), 'error'); $this->updateContextMessages($entity, $context); return; } // Prepare to send the entity over to the receiving server. $entities = array(); $transaction_entities = $transaction_session->getAllEntities(); // Make sure the transaction entity exists. if (array_key_exists($entity->uuid(), $transaction_entities)) { $transaction_entity = $transaction_entities[$entity->uuid()]; } else { drupal_set_message(t('An invalid entity was found.'), 'error'); $this->updateContextMessages($entity, $context); return; } // Get the relationships for the entity. $relationships = array(); foreach ($transaction_session->getRelationships() as $relationship) { if ($relationship['source_uuid'] == $entity->uuid() && (!$entity->supportsRevisions() || $relationship['source_vuuid'] == $entity->vuuid())) { $relationships[] = $relationship; } } // Send the entity revisions. $entities[] = array('entity_type' => $entity->type(), 'uuid' => $entity->uuid(), 'vuuid' => $entity->vuuid(), 'revisions' => $transaction_entity['revisions_payload'], 'relationships' => $relationships); $payload = array('entities' => $entities, 'metadata' => $transaction_session->getMetadata($entity->uuid())); // Send the request. $transaction = new Transaction($transaction_session->getRemote()); $response = $transaction->send('import', $payload); if (!$response['success']) { $message = "There was an error sending the <strong>{$entity->type()}</strong> <code>{$entity->id()}</code> to the remote. Look at the recent log messages for more details."; drupal_set_message($message, 'error'); watchdog('publisher', $message, array(), WATCHDOG_WARNING); Debug::wd($response); } elseif (empty($response['messages'])) { drupal_set_message('The operation was successful, but nothing was done on the remote. This probably means there weren\'t any updates to move.'); } // Mark the entity as synced if the response was successful. if ($response['success']) { publisher_entity_tracking_mark_as_synced($entity, $transaction_session->getRemote()); } $this->updateContextMessages($entity, $context); }
protected function importEntity($entity_payload) { // Verify the entity payload. if (($message = $this->verifyEntityPayload($entity_payload)) !== true) { throw new InvalidEntityPayloadException($message); } // Check to see if any relationships exist. if (array_key_exists('relationships', $entity_payload) && is_array($entity_payload['relationships'])) { foreach ($entity_payload['relationships'] as $relationship) { if (!Entity::exists($relationship['destination_uuid'], $relationship['destination_type'])) { publisher_relationships_save($relationship); } } } // Load the entity. $entity = Entity::loadByUUID($entity_payload['uuid'], $entity_payload['entity_type']); if (!$entity) { $entity = $this->importRevisionAsNew($entity_payload); if (!$entity) { drupal_set_message("There was an error creating the <strong>{$entity_payload['entity_type']}</strong> <code>{$entity_payload['uuid']}</code>. Please check the recent log messages for more details.", 'error'); return false; } } $original_vuuid = $entity->vuuid(); // Import the remaining revisions for the entity. foreach ($entity_payload['revisions'] as $revision_key => $revision) { // Verify the revision. if (($message = $this->verifyRevisionPayload($revision)) !== true) { throw new InvalidRevisionPayloadException($message); } // Apply the revision. $result = $this->applyRevision($entity, $revision); // Do some logic to check and see if we should report on this... $should_report = true; $revision_key_segments = explode('|', $revision_key); if (count($revision_key_segments) == 3 && $revision_key_segments[2] == '1') { $should_report = false; } if ($result && $should_report) { // Add a success message to the transaction data. drupal_set_message("Updated <strong>{$entity_payload['entity_type']}</strong> <code>{$entity_payload['uuid']}</code> to revision <code>{$revision_key}</code>"); } } // Mark the entity as synced. $entity->vuuid($original_vuuid); publisher_entity_tracking_mark_as_synced($entity, $this->remote); // Run the entity handlers after the entire entity has been saved. foreach (EntityHandlerRegistry::getEntityHandlers($entity) as $handler) { if (!$handler instanceof EntityHandlerBase) { continue; } $handler->entity = $entity; $handler->original_entity = $entity; $handler->unhandleEntity($this->getEntityFromMetadata($entity->uuid())); } // Call hook_publisher_entity_received() module_invoke_all('publisher_entity_received', $entity, $this->remote); return true; }