Core and composer updates

This commit is contained in:
Rob Davies 2017-07-03 16:47:07 +01:00
parent a82634bb98
commit 62cac30480
1118 changed files with 21770 additions and 6306 deletions

View file

@ -14,10 +14,56 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides Configuration Management destination plugin.
*
* Persist data to the config system.
* Persists data to the config system.
*
* When a property is NULL, the default is used unless the configuration option
* 'store null' is set to TRUE.
* Available configuration keys:
* - store null: (optional) Boolean, if TRUE, when a property is NULL, NULL is
* stored, otherwise the default is used. Defaults to FALSE.
* - translations: (optional) Boolean, if TRUE, the destination will be
* associated with the langcode provided by the source plugin. Defaults to
* FALSE.
*
* Destination properties expected in the imported row:
* - config_name: The machine name of the config.
* - langcode: (optional) The language code of the config.
*
* Examples:
*
* @code
* source:
* plugin: variable
* variables:
* - node_admin_theme
* process:
* use_admin_theme: node_admin_theme
* destination:
* plugin: config
* config_name: node.settings
* @endcode
*
* This will add the value of the variable "node_admin_theme" to the config with
* the machine name "node.settings" as "node.settings.use_admin_theme".
*
* @code
* source:
* plugin: i18n_variable
* variables:
* - site_offline_message
* process:
* langcode: language
* message: site_offline_message
* destination:
* plugin: config
* config_name: system.maintenance
* translations: true
* @endcode
*
* This will add the value of the variable "site_offline_message" to the config
* with the machine name "system.maintenance" as "system.maintenance.message",
* coupled with the relevant langcode as obtained from the "i18n_variable"
* source plugin.
*
* @see \Drupal\migrate_drupal\Plugin\migrate\source\d6\i18nVariable
*
* @MigrateDestination(
* id = "config"

View file

@ -15,14 +15,50 @@ use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class for importing configuration entities.
* Base destination class for importing configuration entities.
*
* This class serves as the import class for most configuration entities.
* It can be necessary to provide a specific entity class if the configuration
* entity has a compound ID (see EntityFieldEntity) or it has specific setter
* methods (see EntityDateFormat). When implementing an entity destination for
* the latter case, make sure to add a test not only for importing but also
* for re-importing (if that is supported).
* Available configuration keys:
* - translations: (optional) Boolean, if TRUE, the destination will be
* associated with the langcode provided by the source plugin. Defaults to
* FALSE.
*
* Examples:
*
* @code
* source:
* plugin: d7_block_custom
* process:
* id: bid
* info: info
* langcode: language
* body: body
* destination:
* plugin: entity:block
* @endcode
*
* This will save the migrated, processed row as a block config entity.
*
* @code
* source:
* plugin: d6_i18n_profile_field
* constants:
* entity_type: user
* bundle: user
* process:
* langcode: language
* entity_type: 'constants/entity_type'
* bundle: 'constants/bundle'
* field_name: name
* ...
* translation: translation
* destination:
* plugin: entity:field_config
* translations: true
* @endcode
*
* Because the translations configuration is set to "true", this will save the
* migrated, processed row to a "field_config" entity associated with the
* designated langcode.
*/
class EntityConfigBase extends Entity {

View file

@ -5,6 +5,25 @@ namespace Drupal\migrate\Plugin\migrate\destination;
/**
* Provides entity view mode destination plugin.
*
* See EntityConfigBase for the available configuration options.
* @see \Drupal\migrate\Plugin\migrate\destination\EntityConfigBase
*
* Example:
*
* @code
* source:
* plugin: d7_view_mode
* process:
* mode: view_mode
* label: view_mode
* targetEntityType: entity_type
* destination:
* plugin: entity:entity_view_mode
* @endcode
*
* This will add the results of the process ("mode", "label" and
* "targetEntityType") to an "entity_view_mode" entity.
*
* @MigrateDestination(
* id = "entity:entity_view_mode"
* )

View file

@ -5,6 +5,45 @@ namespace Drupal\migrate\Plugin\migrate\destination;
/**
* This class imports one component of an entity display.
*
* Destination properties expected in the imported row:
* - entity_type: The entity type ID.
* - bundle: The entity bundle.
* - view_mode: The machine name of the view mode.
* - field_name: The machine name of the field to be imported into the display.
* - options: (optional) An array of options for displaying the field in this
* view mode.
*
* Examples:
*
* @code
* source:
* constants:
* entity_type: user
* bundle: user
* view_mode: default
* field_name: user_picture
* type: image
* options:
* label: hidden
* settings:
* image_style: ''
* image_link: content
* process:
* entity_type: 'constants/entity_type'
* bundle: 'constants/bundle'
* view_mode: 'constants/view_mode'
* field_name: 'constants/field_name'
* type: 'constants/type'
* options: 'constants/options'
* 'options/type': '@type'
* destination:
* plugin: component_entity_display
* @endcode
*
* This will add the "user_picture" image field to the "default" view mode of
* the "user" bundle of the "user" entity type with options as defined by the
* "options" constant, for example the label will be hidden.
*
* @MigrateDestination(
* id = "component_entity_display"
* )

View file

@ -5,6 +5,38 @@ namespace Drupal\migrate\Plugin\migrate\destination;
/**
* This class imports one component of an entity form display.
*
* Destination properties expected in the imported row:
* - entity_type: The entity type ID.
* - bundle: The entity bundle.
* - form_mode: The machine name of the form mode.
* - field_name: The machine name of the field to be imported into the display.
* - options: (optional) An array of options for displaying the field in this
* form mode.
*
* Examples:
*
* @code
* source:
* constants:
* entity_type: node
* field_name: comment
* form_mode: default
* options:
* type: comment_default
* weight: 20
* process:
* entity_type: 'constants/entity_type'
* field_name: 'constants/field_name'
* form_mode: 'constants/form_mode'
* options: 'constants/options'
* bundle: node_type
* destination:
* plugin: component_entity_form_display
* @endcode
*
* This will add a "comment" field on the "default" form mode of the "node"
* entity type with options defined by the "options" constant.
*
* @MigrateDestination(
* id = "component_entity_form_display"
* )

View file

@ -174,7 +174,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
/**
* Retrieves the hash of the source identifier values.
*
* It is public only for testing purposes.
* @internal
*
* @param array $source_id_values
* The source identifiers

View file

@ -2,11 +2,8 @@
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\migrate\MigrateException;
use Drupal\Component\Utility\Unicode;
@trigger_error('The ' . __NAMESPACE__ . ' \DedupeEntityBase is deprecated in
Drupal 8.4.x and will be removed before Drupal 9.0.0. Instead, use ' . __NAMESPACE__ . ' \MakeUniqueEntityFieldBase', E_USER_DEPRECATED);
/**
* This abstract base contains the dedupe logic.
@ -17,41 +14,9 @@ use Drupal\Component\Utility\Unicode;
* and incremented until a unique value is created.
*
* @link https://www.drupal.org/node/2345929 Online handbook documentation for dedupebase process plugin @endlink
*
* @deprecated in Drupal 8.4.x and will be removed in Drupal 9.0.x. Use
* \Drupal\migrate\Plugin\migrate\process\MakeUniqueBase instead.
*/
abstract class DedupeBase extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$i = 1;
$postfix = isset($this->configuration['postfix']) ? $this->configuration['postfix'] : '';
$start = isset($this->configuration['start']) ? $this->configuration['start'] : 0;
if (!is_int($start)) {
throw new MigrateException('The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
}
$length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL;
if (!is_null($length) && !is_int($length)) {
throw new MigrateException('The character length configuration key should be an integer. Omit this key to capture the entire string.');
}
// Use optional start or length to return a portion of deduplicated value.
$value = Unicode::substr($value, $start, $length);
$new_value = $value;
while ($this->exists($new_value)) {
$new_value = $value . $postfix . $i++;
}
return $new_value;
}
/**
* This is a query checking the existence of some value.
*
* @param mixed $value
* The value to check.
*
* @return bool
* TRUE if the value exists.
*/
abstract protected function exists($value);
abstract class DedupeBase extends MakeUniqueBase {
}

View file

@ -2,10 +2,8 @@
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@trigger_error('The ' . __NAMESPACE__ . ' \DedupeEntity is deprecated in
Drupal 8.4.x and will be removed before Drupal 9.0.0. Instead, use ' . __NAMESPACE__ . ' \MakeUniqueEntityField', E_USER_DEPRECATED);
/**
* Ensures value is not duplicated against an entity field.
@ -18,68 +16,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* @MigrateProcessPlugin(
* id = "dedupe_entity"
* )
*
* @deprecated in Drupal 8.4.x and will be removed in Drupal 9.0.x. Use
* \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField instead.
*/
class DedupeEntity extends DedupeBase implements ContainerFactoryPluginInterface {
/**
* The entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $entityStorage;
/**
* The current migration.
*
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migration = $migration;
$this->entityStorage = $entity_type_manager->getStorage($this->configuration['entity_type']);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
protected function exists($value) {
// Plugins are cached so for every run we need a new query object.
$query = $this
->entityStorage->getQuery()
->condition($this->configuration['field'], $value);
if (!empty($this->configuration['migrated'])) {
// Check if each entity is in the ID map.
$idMap = $this->migration->getIdMap();
foreach ($query->execute() as $id) {
$dest_id_values[$this->configuration['field']] = $id;
if ($idMap->lookupSourceID($dest_id_values)) {
return TRUE;
}
}
return FALSE;
}
else {
// Just check if any such entity exists.
return $query->count()->execute();
}
}
}
class DedupeEntity extends MakeUniqueEntityField { }

View file

@ -27,7 +27,7 @@ use Drupal\migrate\Row;
* process:
* uid:
* -
* plugin: migration
* plugin: migration_lookup
* migration: users
* source: author
* -

View file

@ -0,0 +1,82 @@
<?php
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* This plugin checks if a given entity exists.
*
* Example usage with configuration:
* @code
* field_tags:
* plugin: entity_exists
* source: tid
* entity_type: taxonomy_term
* @endcode
*
* @MigrateProcessPlugin(
* id = "entity_exists"
* )
*/
class EntityExists extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* The entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $storage;
/**
* EntityExists constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param $storage
* The entity storage.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->storage = $storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')->getStorage($configuration['entity_type'])
);
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (is_array($value)) {
$value = reset($value);
}
$entity = $this->storage->load($value);
if ($entity instanceof EntityInterface) {
return $entity->id();
}
return FALSE;
}
}

View file

@ -0,0 +1,113 @@
<?php
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Converts date/datetime from one format to another.
*
* Available configuration keys
* - from_format: The source format string as accepted by
* @link http://php.net/manual/datetime.createfromformat.php \DateTime::createFromFormat. @endlink
* - to_format: The destination format.
* - timezone: String identifying the required time zone, see
* DateTimePlus::__construct().
* - settings: keyed array of settings, see DateTimePlus::__construct().
*
* Examples:
*
* Example usage for date only fields (DATETIME_DATE_STORAGE_FORMAT):
* @code
* process:
* field_date:
* plugin: format_date
* from_format: 'm/d/Y'
* to_format: 'Y-m-d'
* source: event_date
* @endcode
*
* If the source value was '01/05/1955' the transformed value would be
* 1955-01-05.
*
* Example usage for datetime fields (DATETIME_DATETIME_STORAGE_FORMAT):
* @code
* process:
* field_time:
* plugin: format_date
* from_format: 'm/d/Y H:i:s'
* to_format: 'Y-m-d\TH:i:s'
* source: event_time
* @endcode
*
* If the source value was '01/05/1955 10:43:22' the transformed value would be
* 1955-01-05T10:43:22.
*
* Example usage for datetime fields with a timezone and settings:
* @code
* process:
* field_time:
* plugin: format_date
* from_format: 'Y-m-d\TH:i:sO'
* to_format: 'Y-m-d\TH:i:s'
* timezone: 'America/Managua'
* settings:
* validate_format: false
* source: event_time
* @endcode
*
* If the source value was '2004-12-19T10:19:42-0600' the transformed value
* would be 2004-12-19T10:19:42.
*
* @see \DateTime::createFromFormat()
* @see \Drupal\Component\Datetime\DateTimePlus::__construct()
* @see \Drupal\migrate\Plugin\MigrateProcessInterface
*
* @MigrateProcessPlugin(
* id = "format_date"
* )
*/
class FormatDate extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (empty($value)) {
return '';
}
// Validate the configuration.
if (empty($this->configuration['from_format'])) {
throw new MigrateException('Format date plugin is missing from_format configuration.');
}
if (empty($this->configuration['to_format'])) {
throw new MigrateException('Format date plugin is missing to_format configuration.');
}
$fromFormat = $this->configuration['from_format'];
$toFormat = $this->configuration['to_format'];
$timezone = isset($this->configuration['timezone']) ? $this->configuration['timezone'] : NULL;
$settings = isset($this->configuration['settings']) ? $this->configuration['settings'] : [];
// Attempts to transform the supplied date using the defined input format.
// DateTimePlus::createFromFormat can throw exceptions, so we need to
// explicitly check for problems.
try {
$transformed = DateTimePlus::createFromFormat($fromFormat, $value, $timezone, $settings)->format($toFormat);
}
catch (\InvalidArgumentException $e) {
throw new MigrateException(sprintf('Format date plugin could not transform "%s" using the format "%s". Error: %s', $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);
}
catch (\UnexpectedValueException $e) {
throw new MigrateException(sprintf('Format date plugin could not transform "%s" using the format "%s". Error: %s', $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);
}
return $transformed;
}
}

View file

@ -0,0 +1,42 @@
<?php
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Logs values without changing them.
*
* The log plugin will log the values that are being processed by other plugins.
*
* Example:
* @code
* process:
* bar:
* plugin: log
* source: foo
* @endcode
*
* @see \Drupal\migrate\Plugin\MigrateProcessInterface
*
* @MigrateProcessPlugin(
* id = "log"
* )
*/
class Log extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
// Log the value.
$migrate_executable->saveMessage($value);
// Pass through the same value we received.
return $value;
}
}

View file

@ -0,0 +1,78 @@
<?php
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\migrate\MigrateException;
use Drupal\Component\Utility\Unicode;
/**
* This plugin ensures the source value is unique.
*
* The MakeUniqueBase process plugin is used to avoid duplication at the
* destination. For example, when creating filter format names, the source
* value is checked against the existing filter format names and if it exists,
* a numeric postfix is added and incremented until a unique value is created.
* An optional postfix string can be insert before the numeric postfix.
*
* Available configuration keys
* - start: (optional) The position at which to start reading.
* - length: (optional) The number of characters to read.
* - postfix: (optional) A string to insert before the numeric postfix.
*
* @see \Drupal\migrate\Plugin\MigrateProcessInterface
*/
abstract class MakeUniqueBase extends ProcessPluginBase {
/**
* Creates a unique value based on the source value.
*
* @param string $value
* The input string.
* @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
* The migration in which this process is being executed.
* @param \Drupal\migrate\Row $row
* The row from the source to process.
* @param string $destination_property
* The destination property currently worked on. This is only used together
* with the $row above.
*
* @return string
* The unique version of the input value.
*
* @throws \Drupal\migrate\MigrateException
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$i = 1;
$postfix = isset($this->configuration['postfix']) ? $this->configuration['postfix'] : '';
$start = isset($this->configuration['start']) ? $this->configuration['start'] : 0;
if (!is_int($start)) {
throw new MigrateException('The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
}
$length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL;
if (!is_null($length) && !is_int($length)) {
throw new MigrateException('The character length configuration key should be an integer. Omit this key to capture the entire string.');
}
// Use optional start or length to return a portion of the unique value.
$value = Unicode::substr($value, $start, $length);
$new_value = $value;
while ($this->exists($new_value)) {
$new_value = $value . $postfix . $i++;
}
return $new_value;
}
/**
* This is a query checking the existence of some value.
*
* @param mixed $value
* The value to check.
*
* @return bool
* TRUE if the value exists.
*/
abstract protected function exists($value);
}

View file

@ -0,0 +1,149 @@
<?php
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Ensures the source value is made unique against an entity field.
*
* The make_unique process plugin is typically used to make the entity id
* unique, ensuring that migrated entity data is preserved.
*
* The make_unique process plugin has two required configuration keys,
* entity_type and field. It's typically used with an entity destination, making
* sure that after saving the entity, the field value is unique. For example,
* if the value is foo and there is already an entity where the field value is
* foo, then the plugin will return foo1.
*
* The optional configuration key postfix which will be added between the number
* and the original value, for example, foo_1 for postfix: _. Note that the
* value of postfix is ignored if the value is not changed, if it was already
* unique.
*
* The optional configuration key migrated, if true, indicates that an entity
* will only be considered a duplicate if it was migrated by the current
* migration.
*
* Available configuration keys
* - entity_type: The entity type.
* - field: The entity field for the given value.
* - migrated: (optional) A boolean to indicate that making the field unique
* only occurs for migrated entities.
* - start: (optional) The position at which to start reading.
* - length: (optional) The number of characters to read.
* - postfix: (optional) A string to insert before the numeric postfix.
*
* Examples:
*
* @code
* process:
* format:
* -
* plugin: machine_name
* source: name
* -
* plugin: make_unique_entity_field
* entity_type: filter_format
* field: format
*
* @endcode
*
* This will create a format machine name out the human readable name and make
* sure it's unique.
*
* @code
* process:
* format:
* -
* plugin: machine_name
* source: name
* -
* plugin: make_unique_entity_field
* entity_type: filter_format
* field: format
* postfix: _
* migrated: true
*
* @endcode
*
* This will create a format machine name out the human readable name and make
* sure it's unique if the entity was migrated. The postfix character is
* inserted between the added number and the original value.
*
* @see \Drupal\migrate\Plugin\migrate\process\MakeUniqueBase
* @see \Drupal\migrate\Plugin\MigrateProcessInterface
*
* @MigrateProcessPlugin(
* id = "make_unique_entity_field"
* )
*/
class MakeUniqueEntityField extends MakeUniqueBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The current migration.
*
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migration = $migration;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
protected function exists($value) {
// Plugins are cached so for every run we need a new query object.
$query = $this
->entityTypeManager
->getStorage($this->configuration['entity_type'])
->getQuery()
->condition($this->configuration['field'], $value);
if (!empty($this->configuration['migrated'])) {
// Check if each entity is in the ID map.
$idMap = $this->migration->getIdMap();
foreach ($query->execute() as $id) {
$dest_id_values[$this->configuration['field']] = $id;
if ($idMap->lookupSourceID($dest_id_values)) {
return TRUE;
}
}
return FALSE;
}
else {
// Just check if any such entity exists.
return $query->count()->execute();
}
}
}

View file

@ -2,16 +2,8 @@
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\Plugin\MigratePluginManagerInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
@trigger_error('The ' . __NAMESPACE__ . '\Migration is deprecated in
Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use ' . __NAMESPACE__ . '\MigrationLookup', E_USER_DEPRECATED);
/**
* Calculates the value of a property based on a previous migration.
@ -21,156 +13,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* @MigrateProcessPlugin(
* id = "migration"
* )
*
* @deprecated in Drupal 8.3.x and will be removed in Drupal 9.0.x.
* Use \Drupal\migrate\Plugin\migrate\process\MigrationLookup instead.
*/
class Migration extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* The process plugin manager.
*
* @var \Drupal\migrate\Plugin\MigratePluginManager
*/
protected $processPluginManager;
/**
* The migration plugin manager.
*
* @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
*/
protected $migrationPluginManager;
/**
* The migration to be executed.
*
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MigrationPluginManagerInterface $migration_plugin_manager, MigratePluginManagerInterface $process_plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migrationPluginManager = $migration_plugin_manager;
$this->migration = $migration;
$this->processPluginManager = $process_plugin_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$container->get('plugin.manager.migration'),
$container->get('plugin.manager.migrate.process')
);
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$migration_ids = $this->configuration['migration'];
if (!is_array($migration_ids)) {
$migration_ids = [$migration_ids];
}
if (!is_array($value)) {
$value = [$value];
}
$this->skipOnEmpty($value);
$self = FALSE;
/** @var \Drupal\migrate\Plugin\MigrationInterface[] $migrations */
$destination_ids = NULL;
$source_id_values = [];
$migrations = $this->migrationPluginManager->createInstances($migration_ids);
foreach ($migrations as $migration_id => $migration) {
if ($migration_id == $this->migration->id()) {
$self = TRUE;
}
if (isset($this->configuration['source_ids'][$migration_id])) {
$configuration = ['source' => $this->configuration['source_ids'][$migration_id]];
$source_id_values[$migration_id] = $this->processPluginManager
->createInstance('get', $configuration, $this->migration)
->transform(NULL, $migrate_executable, $row, $destination_property);
}
else {
$source_id_values[$migration_id] = $value;
}
// Break out of the loop as soon as a destination ID is found.
if ($destination_ids = $migration->getIdMap()->lookupDestinationId($source_id_values[$migration_id])) {
break;
}
}
if (!$destination_ids && !empty($this->configuration['no_stub'])) {
return NULL;
}
if (!$destination_ids && ($self || isset($this->configuration['stub_id']) || count($migrations) == 1)) {
// If the lookup didn't succeed, figure out which migration will do the
// stubbing.
if ($self) {
$migration = $this->migration;
}
elseif (isset($this->configuration['stub_id'])) {
$migration = $migrations[$this->configuration['stub_id']];
}
else {
$migration = reset($migrations);
}
$destination_plugin = $migration->getDestinationPlugin(TRUE);
// Only keep the process necessary to produce the destination ID.
$process = $migration->getProcess();
// We already have the source ID values but need to key them for the Row
// constructor.
$source_ids = $migration->getSourcePlugin()->getIds();
$values = [];
foreach (array_keys($source_ids) as $index => $source_id) {
$values[$source_id] = $source_id_values[$migration->id()][$index];
}
$stub_row = new Row($values + $migration->getSourceConfiguration(), $source_ids, TRUE);
// Do a normal migration with the stub row.
$migrate_executable->processRow($stub_row, $process);
$destination_ids = [];
try {
$destination_ids = $destination_plugin->import($stub_row);
}
catch (\Exception $e) {
$migration->getIdMap()->saveMessage($stub_row->getSourceIdValues(), $e->getMessage());
}
if ($destination_ids) {
$migration->getIdMap()->saveIdMapping($stub_row, $destination_ids, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
}
}
if ($destination_ids) {
if (count($destination_ids) == 1) {
return reset($destination_ids);
}
else {
return $destination_ids;
}
}
}
/**
* Skips the migration process entirely if the value is FALSE.
*
* @param mixed $value
* The incoming value to transform.
*
* @throws \Drupal\migrate\MigrateSkipProcessException
*/
protected function skipOnEmpty(array $value) {
if (!array_filter($value)) {
throw new MigrateSkipProcessException();
}
}
}
class Migration extends MigrationLookup { }

View file

@ -0,0 +1,260 @@
<?php
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\Plugin\MigratePluginManagerInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Looks up the value of a property based on a previous migration.
*
* It is important to maintain relationships among content coming from the
* source site. For example, on the source site, a given user account may
* have an ID of 123, but the Drupal user account created from it may have
* a uid of 456. The migration process maintains the relationships between
* source and destination identifiers in map tables, and this information
* is leveraged by the migration_lookup process plugin.
*
* Available configuration keys
* - migration: A single migration ID, or an array of migration IDs.
* - source_ids: (optional) An array keyed by migration IDs with values that are
* a list of source properties.
* - stub_id: (optional) Identifies the migration which will be used to create
* any stub entities.
* - no_stub: (optional) Prevents the creation of a stub entity when no
* relationship is found in the migration map.
*
* Examples:
*
* Consider a node migration, where you want to maintain authorship. If you have
* migrated the user accounts in a migration named "users", you would specify
* the following:
*
* @code
* process:
* uid:
* plugin: migration_lookup
* migration: users
* source: author
* @endcode
*
* This takes the value of the author property in the source data, and looks it
* up in the map table associated with the users migration, returning the
* resulting user ID and assigning it to the destination uid property.
*
* The value of 'migration' can be a list of migration IDs. When using multiple
* migrations it is possible each use different source identifiers. In this
* case one can use source_ids which is an array keyed by the migration IDs
* and the value is a list of source properties.
*
* @code
* process:
* uid:
* plugin: migration_lookup
* migration:
* - users
* - members
* source_ids:
* users:
* - author
* members:
* - id
* @endcode
*
* If the migration_lookup plugin does not find the source ID in the migration
* map it will create a stub entity for the relationship to use. This stub is
* generated by the migration provided. In the case of multiple migrations the
* first value of the migration list will be used, but you can select the
* migration you wish to use by using the stub_id configuration key:
*
* @code
* process:
* uid:
* plugin: migration_lookup
* migration:
* - users
* - members
* stub_id: members
* @endcode
*
* In the above example, the value of stub_id selects the members migration to
* create any stub entities.
*
* To prevent the creation of a stub entity when no relationship is found in the
* migration map, use no_stub:
*
* @code
* process:
* uid:
* plugin: migration_lookup
* migration: users
* no_stub: true
* source: author
* @endcode
*
* @see \Drupal\migrate\Plugin\MigrateProcessInterface
*
* @MigrateProcessPlugin(
* id = "migration_lookup"
* )
*/
class MigrationLookup extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* The process plugin manager.
*
* @var \Drupal\migrate\Plugin\MigratePluginManager
*/
protected $processPluginManager;
/**
* The migration plugin manager.
*
* @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
*/
protected $migrationPluginManager;
/**
* The migration to be executed.
*
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MigrationPluginManagerInterface $migration_plugin_manager, MigratePluginManagerInterface $process_plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migrationPluginManager = $migration_plugin_manager;
$this->migration = $migration;
$this->processPluginManager = $process_plugin_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$container->get('plugin.manager.migration'),
$container->get('plugin.manager.migrate.process')
);
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$migration_ids = $this->configuration['migration'];
if (!is_array($migration_ids)) {
$migration_ids = [$migration_ids];
}
if (!is_array($value)) {
$value = [$value];
}
$this->skipOnEmpty($value);
$self = FALSE;
/** @var \Drupal\migrate\Plugin\MigrationInterface[] $migrations */
$destination_ids = NULL;
$source_id_values = [];
$migrations = $this->migrationPluginManager->createInstances($migration_ids);
foreach ($migrations as $migration_id => $migration) {
if ($migration_id == $this->migration->id()) {
$self = TRUE;
}
if (isset($this->configuration['source_ids'][$migration_id])) {
$configuration = ['source' => $this->configuration['source_ids'][$migration_id]];
$source_id_values[$migration_id] = $this->processPluginManager
->createInstance('get', $configuration, $this->migration)
->transform(NULL, $migrate_executable, $row, $destination_property);
}
else {
$source_id_values[$migration_id] = $value;
}
// Break out of the loop as soon as a destination ID is found.
if ($destination_ids = $migration->getIdMap()->lookupDestinationId($source_id_values[$migration_id])) {
break;
}
}
if (!$destination_ids && !empty($this->configuration['no_stub'])) {
return NULL;
}
if (!$destination_ids && ($self || isset($this->configuration['stub_id']) || count($migrations) == 1)) {
// If the lookup didn't succeed, figure out which migration will do the
// stubbing.
if ($self) {
$migration = $this->migration;
}
elseif (isset($this->configuration['stub_id'])) {
$migration = $migrations[$this->configuration['stub_id']];
}
else {
$migration = reset($migrations);
}
$destination_plugin = $migration->getDestinationPlugin(TRUE);
// Only keep the process necessary to produce the destination ID.
$process = $migration->getProcess();
// We already have the source ID values but need to key them for the Row
// constructor.
$source_ids = $migration->getSourcePlugin()->getIds();
$values = [];
foreach (array_keys($source_ids) as $index => $source_id) {
$values[$source_id] = $source_id_values[$migration->id()][$index];
}
$stub_row = new Row($values + $migration->getSourceConfiguration(), $source_ids, TRUE);
// Do a normal migration with the stub row.
$migrate_executable->processRow($stub_row, $process);
$destination_ids = [];
try {
$destination_ids = $destination_plugin->import($stub_row);
}
catch (\Exception $e) {
$migration->getIdMap()->saveMessage($stub_row->getSourceIdValues(), $e->getMessage());
}
if ($destination_ids) {
$migration->getIdMap()->saveIdMapping($stub_row, $destination_ids, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
}
}
if ($destination_ids) {
if (count($destination_ids) == 1) {
return reset($destination_ids);
}
else {
return $destination_ids;
}
}
}
/**
* Skips the migration process entirely if the value is FALSE.
*
* @param mixed $value
* The incoming value to transform.
*
* @throws \Drupal\migrate\MigrateSkipProcessException
*/
protected function skipOnEmpty(array $value) {
if (!array_filter($value)) {
throw new MigrateSkipProcessException();
}
}
}

View file

@ -21,6 +21,9 @@ use Drupal\migrate\MigrateSkipRowException;
* - row: Skips the entire row when an empty value is encountered.
* - process: Prevents further processing of the input property when the value
* is empty.
* - message: (optional) A message to be logged in the {migrate_message_*} table
* for this row. Messages are only logged for the 'row' skip level. If not
* set, nothing is logged in the message table.
*
* Examples:
*
@ -30,9 +33,11 @@ use Drupal\migrate\MigrateSkipRowException;
* plugin: skip_on_empty
* method: row
* source: field_name
* message: 'Field field_name is missed'
* @endcode
*
* If field_name is empty, skips the entire row.
* If field_name is empty, skips the entire row and the message 'Field
* field_name is missed' is logged in the message table.
*
* @code
* process:
@ -79,7 +84,8 @@ class SkipOnEmpty extends ProcessPluginBase {
*/
public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!$value) {
throw new MigrateSkipRowException();
$message = !empty($this->configuration['message']) ? $this->configuration['message'] : '';
throw new MigrateSkipRowException($message);
}
return $value;
}

View file

@ -16,6 +16,8 @@ use Drupal\migrate\MigrateSkipRowException;
*
* Available configuration keys:
* - index: The source property to check for.
* - message: (optional) A message to be logged in the {migrate_message_*} table
* for this row. If not set, nothing is logged in the message table.
*
* Example:
*
@ -26,10 +28,12 @@ use Drupal\migrate\MigrateSkipRowException;
* plugin: skip_row_if_not_set
* index: contact
* source: data
* message: "Missed the 'data' key"
* @endcode
*
* This will return $data['contact'] if it exists. Otherwise, the row will be
* skipped.
* skipped and the message "Missed the 'data' key" will be logged in the
* message table.
*
* @see \Drupal\migrate\Plugin\MigrateProcessInterface
*
@ -45,7 +49,8 @@ class SkipRowIfNotSet extends ProcessPluginBase {
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!isset($value[$this->configuration['index']])) {
throw new MigrateSkipRowException();
$message = !empty($this->configuration['message']) ? $this->configuration['message'] : '';
throw new MigrateSkipRowException($message);
}
return $value[$this->configuration['index']];
}

View file

@ -10,9 +10,98 @@ use Drupal\migrate\Row;
use Drupal\migrate\MigrateSkipRowException;
/**
* This plugin changes the current value based on a static lookup map.
* Changes the source value based on a static lookup map.
*
* @link https://www.drupal.org/node/2143521 Online handbook documentation for static_map process plugin @endlink
* Maps the input value to another value using an associative array specified in
* the configuration.
*
* Available configuration keys:
* - source: The input value - either a scalar or an array.
* - map: An array (of 1 or more dimensions) that identifies the mapping between
* source values and destination values.
* - bypass: (optional) Whether the plugin should proceed when the source is not
* found in the map array. Defaults to FALSE.
* - TRUE: Return the unmodified input value, or another default value, if one
* is specified.
* - FALSE: Throw a MigrateSkipRowException.
* - default_value: (optional) The value to return if the source is not found in
* the map array.
*
* Examples:
*
* @code
* process:
* bar:
* plugin: static_map
* source: foo
* map:
* from: to
* this: that
* @endcode
*
* If the value of the source property foo was "from" then the value of the
* destination property bar will be "to". Similarly "this" becomes "that".
* static_map can do a lot more than this: it supports a list of source
* properties. This is super useful in module-delta to machine name conversions.
*
* @code
* process:
* id:
* plugin: static_map
* source:
* - module
* - delta
* map:
* filter:
* 0: filter_html_escape
* 1: filter_autop
* 2: filter_url
* 3: filter_htmlcorrector
* 4: filter_html_escape
* php:
* 0: php_code
* @endcode
*
* If the value of the source properties module and delta are "filter" and "2"
* respectively, then the returned value will be "filter_url". By default, if a
* value is not found in the map, an exception is thrown.
*
* When static_map is used to just rename a few things and leave the others, a
* "bypass: true" option can be added. In this case, the source value is used
* unchanged, e.g.:
*
* @code
* process:
* bar:
* plugin: static_map
* source: foo
* map:
* from: to
* this: that
* bypass: TRUE
* @endcode
*
* If the value of the source property "foo" is "from" then the returned value
* will be "to", but if the value of "foo" is "another" (a value that is not in
* the map) then the source value is used unchanged so the returned value will
* be "from" because "bypass" is set to TRUE.
*
* @code
* process:
* bar:
* plugin: static_map
* source: foo
* map:
* from: to
* this: that
* default_value: bar
* @endcode
*
* If the value of the source property "foo" is "yet_another" (a value that is
* not in the map) then the default_value is used so the returned value will
* be "bar".
*
* @see \Drupal\migrate\Plugin\MigrateProcessInterface
*
* @MigrateProcessPlugin(
* id = "static_map"

View file

@ -1,10 +1,41 @@
<?php
namespace Drupal\migrate\Plugin\migrate\source;
use Drupal\migrate\Plugin\MigrationInterface;
/**
* Source which takes its data directly from the plugin config.
* Allows source data to be defined in the configuration of the source plugin.
*
* The embedded_data source plugin is used to inject source data from the plugin
* configuration. One use case is when some small amount of fixed data is
* imported, so that it can be referenced by other migrations. Another use case
* is testing.
*
* Available configuration keys
* - data_rows: The source data array.
* - ids: The unique ID field of the data.
*
* Examples:
*
* @code
* source:
* plugin: embedded_data
* data_rows:
* -
* channel_machine_name: music
* channel_description: Music
* -
* channel_machine_name: movies
* channel_description: Movies
* ids:
* channel_machine_name:
* type: string
* @endcode
*
* This example migrates a channel vocabulary.
*
* @see \Drupal\migrate\Plugin\MigrateSourceInterface
*
* @MigrateSource(
* id = "embedded_data"

View file

@ -234,6 +234,7 @@ abstract class SqlBase extends SourcePluginBase implements ContainerFactoryPlugi
// OR above high water).
$conditions = $this->query->orConditionGroup();
$condition_added = FALSE;
$added_fields = [];
if (empty($this->configuration['ignore_map']) && $this->mapJoinable()) {
// Build the join to the map table. Because the source key could have
// multiple fields, we need to build things up.
@ -259,18 +260,21 @@ abstract class SqlBase extends SourcePluginBase implements ContainerFactoryPlugi
for ($count = 1; $count <= $n; $count++) {
$map_key = 'sourceid' . $count;
$this->query->addField($alias, $map_key, "migrate_map_$map_key");
$added_fields[] = "$alias.$map_key";
}
if ($n = count($this->migration->getDestinationIds())) {
for ($count = 1; $count <= $n; $count++) {
$map_key = 'destid' . $count++;
$this->query->addField($alias, $map_key, "migrate_map_$map_key");
$added_fields[] = "$alias.$map_key";
}
}
$this->query->addField($alias, 'source_row_status', 'migrate_map_source_row_status');
$added_fields[] = "$alias.source_row_status";
}
// 2. If we are using high water marks, also include rows above the mark.
// But, include all rows if the high water mark is not set.
if ($this->getHighWaterProperty() && ($high_water = $this->getHighWater()) !== '') {
if ($this->getHighWaterProperty() && ($high_water = $this->getHighWater())) {
$high_water_field = $this->getHighWaterField();
$conditions->condition($high_water_field, $high_water, '>');
$this->query->orderBy($high_water_field);
@ -279,6 +283,15 @@ abstract class SqlBase extends SourcePluginBase implements ContainerFactoryPlugi
if ($condition_added) {
$this->query->condition($conditions);
}
// If the query has a group by, our added fields need it too, to keep the
// query valid.
// @see https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
$group_by = $this->query->getGroupBy();
if ($group_by && $added_fields) {
foreach ($added_fields as $added_field) {
$this->query->groupBy($added_field);
}
}
}
// Download data in batches for performance.
@ -357,6 +370,14 @@ abstract class SqlBase extends SourcePluginBase implements ContainerFactoryPlugi
return FALSE;
}
// FALSE if driver is PostgreSQL and database doesn't match.
if ($id_map_database_options['driver'] === 'pgsql' &&
$source_database_options['driver'] === 'pgsql' &&
$id_map_database_options['database'] != $source_database_options['database']
) {
return FALSE;
}
foreach (['username', 'password', 'host', 'port', 'namespace', 'driver'] as $key) {
if (isset($source_database_options[$key])) {
if ($id_map_database_options[$key] != $source_database_options[$key]) {

View file

@ -106,10 +106,11 @@ class Row {
* Retrieves the values of the source identifiers.
*
* @return array
* An array containing the values of the source identifiers.
* An array containing the values of the source identifiers. Returns values
* in the same order as defined in $this->sourceIds.
*/
public function getSourceIdValues() {
return array_intersect_key($this->source, $this->sourceIds);
return array_merge($this->sourceIds, array_intersect_key($this->source, $this->sourceIds));
}
/**