8383e587e564f531e297d25e9bcb029bbab23763
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / destination / DestinationBase.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\destination;
4
5 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  * Base class for migrate destination classes.
14  *
15  * Migrate destination plugins perform the import operation of the migration.
16  * Destination plugins extend this abstract base class. A destination plugin
17  * must implement at least fields(), getIds() and import() methods. Destination
18  * plugins can also support rollback operations. For more
19  * information, refer to \Drupal\migrate\Plugin\MigrateDestinationInterface.
20  *
21  * @see \Drupal\migrate\Plugin\MigrateDestinationPluginManager
22  * @see \Drupal\migrate\Annotation\MigrateDestination
23  * @see plugin_api
24  *
25  * @ingroup migration
26  */
27 abstract class DestinationBase extends PluginBase implements MigrateDestinationInterface, RequirementsInterface {
28
29   /**
30    * Indicates whether the destination can be rolled back.
31    *
32    * @var bool
33    */
34   protected $supportsRollback = FALSE;
35
36   /**
37    * The rollback action to be saved for the last imported item.
38    *
39    * @var int
40    */
41   protected $rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
42
43   /**
44    * The migration.
45    *
46    * @var \Drupal\migrate\Plugin\MigrationInterface
47    */
48   protected $migration;
49
50   /**
51    * Constructs an entity destination plugin.
52    *
53    * @param array $configuration
54    *   A configuration array containing information about the plugin instance.
55    * @param string $plugin_id
56    *   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    *   The migration.
61    */
62   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
63     parent::__construct($configuration, $plugin_id, $plugin_definition);
64     $this->migration = $migration;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function rollbackAction() {
71     return $this->rollbackAction;
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function checkRequirements() {
78     if (empty($this->pluginDefinition['requirements_met'])) {
79       throw new RequirementsException();
80     }
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function rollback(array $destination_identifier) {
87     // By default we do nothing.
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function supportsRollback() {
94     return $this->supportsRollback;
95   }
96
97   /**
98    * For a destination item being updated, set the appropriate rollback action.
99    *
100    * @param array $id_map
101    *   The map row data for the item.
102    * @param int $update_action
103    *   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     // If the entity we're updating was previously migrated by us, preserve the
107     // existing rollback action.
108     if (isset($id_map['sourceid1'])) {
109       $this->rollbackAction = $id_map['rollback_action'];
110     }
111     // Otherwise, we're updating an entity which already existed on the
112     // destination and want to make sure we do not delete it on rollback.
113     else {
114       $this->rollbackAction = $update_action;
115     }
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function getDestinationModule() {
122     if (!empty($this->configuration['destination_module'])) {
123       return $this->configuration['destination_module'];
124     }
125     if (!empty($this->pluginDefinition['destination_module'])) {
126       return $this->pluginDefinition['destination_module'];
127     }
128     if (is_string($this->migration->provider)) {
129       return $this->migration->provider;
130     }
131     else {
132       return reset($this->migration->provider);
133     }
134   }
135
136 }