You are here

public function SalesforcePushCommands::pushUnmapped in Salesforce Suite 5.0.x

Same name and namespace in other branches
  1. 8.4 modules/salesforce_push/src/Commands/SalesforcePushCommands.php \Drupal\salesforce_push\Commands\SalesforcePushCommands::pushUnmapped()
  2. 8.3 modules/salesforce_push/src/Commands/SalesforcePushCommands.php \Drupal\salesforce_push\Commands\SalesforcePushCommands::pushUnmapped()

Push entities of a mapped type that are not linked to Salesforce Objects.

@option count The number of entities to try to sync. (Default is 50). @usage drush sfpu foo Push 50 drupal entities without mapped objects for mapping "foo" @usage drush sfpu foo --count=42 Push 42 unmapped drupal entities without mapped objects for mapping "foo"

@command salesforce_push:push-unmapped @aliases sfpu,salesforce-push-unmapped,salesforce_push:unmapped

Parameters

string $name: The Drupal machine name of the mapping for the entities.

array $options: An associative array of options whose values come from cli, aliases, config, etc.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

modules/salesforce_push/src/Commands/SalesforcePushCommands.php, line 195

Class

SalesforcePushCommands
A Drush commandfile.

Namespace

Drupal\salesforce_push\Commands

Code

public function pushUnmapped($name, array $options = [
  'count' => 50,
]) {
  $mappings = $this
    ->getPushMappingsFromName($name);
  foreach ($mappings as $mapping) {
    $entity_type = $mapping
      ->get('drupal_entity_type');
    $entity_storage = $this->etm
      ->getStorage($entity_type);
    $entity_keys = $this->etm
      ->getDefinition($entity_type)
      ->getKeys();
    $id_key = $entity_keys['id'];
    $bundle_key = empty($entity_keys['bundle']) ? FALSE : $entity_keys['bundle'];
    $query = $this->database
      ->select($entity_storage
      ->getBaseTable(), 'b');
    $query
      ->leftJoin('salesforce_mapped_object', 'm', "b.{$id_key} = m.drupal_entity__target_id AND m.drupal_entity__target_type = '{$entity_type}'");
    if ($bundle_key) {
      $query
        ->condition("b.{$bundle_key}", $mapping
        ->get('drupal_bundle'));
    }
    $query
      ->fields('b', [
      $id_key,
    ]);
    $query
      ->isNull('m.drupal_entity__target_id');
    $results = $query
      ->range(0, $options['count'])
      ->execute()
      ->fetchAllAssoc($id_key);
    $entities = $entity_storage
      ->loadMultiple(array_keys($results));
    $log = [];
    foreach ($entities as $entity) {
      salesforce_push_entity_crud($entity, 'push_create');
      $log[] = $entity
        ->id();
    }
    $this->logger
      ->info(dt("!mapping: !count unmapped entities found and push to Salesforce attempted. See logs for more details.", [
      '!count' => count($log),
      '!mapping' => $mapping
        ->label(),
    ]));
  }
}