Skip to content

Commit

Permalink
Merge pull request #15 from jfhovinne/drush-integration
Browse files Browse the repository at this point in the history
Implement export all items - #13
  • Loading branch information
ademarco committed Apr 15, 2016
2 parents d72b6f4 + 08f4f74 commit 886e856
Showing 1 changed file with 28 additions and 9 deletions.
Expand Up @@ -39,25 +39,44 @@ function drush_integration_producer_integration_export($backend_name, $producer_
return drush_log(dt('Please specify at least a backend and a producer as arguments.'), 'error');
}

$backend = \Drupal\integration\Backend\BackendFactory::getInstance($backend_name);
$producer = \Drupal\integration_producer\ProducerFactory::getInstance($producer_name);
$entity_id = (int) drush_get_option('entity-id', NULL);
$entity_type = $producer->getEntityType();

if ($entity_id) {
$entity = entity_load_single($entity_type, $entity_id);
if ($entity) {
$document = $producer->build($entity);
if ($document) {
$backend = \Drupal\integration\Backend\BackendFactory::getInstance($backend_name);
$document = $backend->create($producer->getConfiguration()->getResourceSchema(), $document);
drush_log(dt('The item !id has been exported.', ['!id' => $document->getId()]), 'success');
}
else {
drush_log(dt('The item could not be exported.'), 'error');
_drush_integration_producer_integration_export_entity($backend, $producer, $entity);
}
}
else {
$bundle = $producer->getConfiguration()->getEntityBundle();
$query = new EntityFieldQuery;
$result = $query
->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', $bundle)
->execute();
if (!empty($result[$entity_type])) {
$ids = array_keys($result[$entity_type]);
$entities = entity_load($entity_type, $ids);
foreach($entities as $entity) {
_drush_integration_producer_integration_export_entity($backend, $producer, $entity);
}
}
}
}

/**
* Export an entity.
*/
function _drush_integration_producer_integration_export_entity($backend, $producer, $entity) {
$document = $producer->build($entity);
if ($document) {
$document = $backend->create($producer->getConfiguration()->getResourceSchema(), $document);
drush_log(dt('The item !id has been exported.', ['!id' => $document->getId()]), 'success');
}
else {
// @todo: export all the data provided by this producer.
drush_log(dt('The item could not be exported.'), 'error');
}
}

0 comments on commit 886e856

Please sign in to comment.