DestinationBase.php 3.86 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\migrate\Plugin\migrate\destination;

use Drupal\Core\Plugin\PluginBase;
6
use Drupal\migrate\Plugin\MigrationInterface;
7
use Drupal\migrate\Exception\RequirementsException;
8
use Drupal\migrate\Plugin\MigrateDestinationInterface;
9
use Drupal\migrate\Plugin\MigrateIdMapInterface;
10
use Drupal\migrate\Plugin\RequirementsInterface;
11

12 13 14
/**
 * Base class for migrate destination classes.
 *
15
 * Migrate destination plugins perform the import operation of the migration.
16 17 18 19 20
 * Destination plugins extend this abstract base class. A destination plugin
 * must implement at least fields(), getIds() and import() methods. Destination
 * plugins can also support rollback operations. For more
 * information, refer to \Drupal\migrate\Plugin\MigrateDestinationInterface.
 *
21 22 23
 * @see \Drupal\migrate\Plugin\MigrateDestinationPluginManager
 * @see \Drupal\migrate\Annotation\MigrateDestination
 * @see plugin_api
24 25
 *
 * @ingroup migration
26
 */
27 28
abstract class DestinationBase extends PluginBase implements MigrateDestinationInterface, RequirementsInterface {

29 30 31 32 33 34 35
  /**
   * Indicates whether the destination can be rolled back.
   *
   * @var bool
   */
  protected $supportsRollback = FALSE;

36 37 38 39 40 41 42
  /**
   * The rollback action to be saved for the last imported item.
   *
   * @var int
   */
  protected $rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;

43 44 45
  /**
   * The migration.
   *
46
   * @var \Drupal\migrate\Plugin\MigrationInterface
47 48 49 50 51 52 53 54 55 56
   */
  protected $migration;

  /**
   * Constructs an entity destination plugin.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
57
   * @param mixed $plugin_definition
58
   *   The plugin implementation definition.
59
   * @param \Drupal\migrate\Plugin\MigrationInterface $migration
60 61
   *   The migration.
   */
62
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
63 64 65 66
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->migration = $migration;
  }

67 68 69 70 71 72 73
  /**
   * {@inheritdoc}
   */
  public function rollbackAction() {
    return $this->rollbackAction;
  }

74 75 76 77
  /**
   * {@inheritdoc}
   */
  public function checkRequirements() {
78 79 80
    if (empty($this->pluginDefinition['requirements_met'])) {
      throw new RequirementsException();
    }
81
  }
82

83 84 85
  /**
   * {@inheritdoc}
   */
86
  public function rollback(array $destination_identifier) {
87
    // By default we do nothing.
88 89
  }

90 91 92 93 94 95
  /**
   * {@inheritdoc}
   */
  public function supportsRollback() {
    return $this->supportsRollback;
  }
96 97 98 99 100 101

  /**
   * For a destination item being updated, set the appropriate rollback action.
   *
   * @param array $id_map
   *   The map row data for the item.
102 103
   * @param int $update_action
   *   The rollback action to take if we are updating an existing item.
104
   */
105
  protected function setRollbackAction(array $id_map, $update_action = MigrateIdMapInterface::ROLLBACK_PRESERVE) {
106 107 108 109 110 111 112 113
    // If the entity we're updating was previously migrated by us, preserve the
    // existing rollback action.
    if (isset($id_map['sourceid1'])) {
      $this->rollbackAction = $id_map['rollback_action'];
    }
    // Otherwise, we're updating an entity which already existed on the
    // destination and want to make sure we do not delete it on rollback.
    else {
114
      $this->rollbackAction = $update_action;
115 116
    }
  }
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  /**
   * {@inheritdoc}
   */
  public function getDestinationModule() {
    if (!empty($this->configuration['destination_module'])) {
      return $this->configuration['destination_module'];
    }
    if (!empty($this->pluginDefinition['destination_module'])) {
      return $this->pluginDefinition['destination_module'];
    }
    if (is_string($this->migration->provider)) {
      return $this->migration->provider;
    }
    else {
      return reset($this->migration->provider);
    }
  }

136
}