abstract class SourcePluginBase extends PluginBase implements MigrateSourceInterface, RollbackAwareInterface (View source)

The base class for source plugins.

The migration uses the next() method to iterate over rows returned by the source plugin. Information about the row is also tracked using the ID map plugin. For each row, the corresponding tracked map row, if it exists, is deleted before allowing modification to the source row. Then, source plugins can modify the row using the prepareRow() method, which also invokes hook_prepare_row(). The row is now prepared and we can decide if it will be processed.

To be processed the row must meet any of these conditions:

  • The row has not already been imported.
    • This is indicated by an incomplete map row with the status set to \Drupal\migrate\Plugin\MigrateIdMapInterface::STATUS_NEEDS_UPDATE.
  • The row needs an update.
    • Rows can be marked by custom or contrib modules using the \Drupal\migrate\Plugin\MigrateIdMapInterface::prepareUpdate() os \Drupal\migrate\Plugin\MigrateIdMapInterface::setUpdate() methods.
  • The row is above the high-water mark.
    • The high-water mark is the highest encountered value of the property defined by the configuration key high_water_property.
  • The source row has changed.
    • A row is considered changed only if the track_changes property is set on the source plugin and the source values for the row have changed since the last import.

When set to be processed, the row is also marked frozen and no further changes to the row source properties are allowed. The last step is to set the high-water value, if high water is in use.

Available configuration keys:

  • cache_counts: (optional) If set, cache the source count.
  • cache_key: (optional) Uniquely named cache key used for cache_counts.
  • skip_count: (optional) If set, do not attempt to count the source.
  • track_changes: (optional) If set, track changes to incoming data.
  • high_water_property: (optional) It is an array of name & alias values (optional table alias). This high_water_property is typically a timestamp or serial id showing what was the last imported record. Only content with a higher value will be imported.
  • constants: (optional) An array of constants that can be used in the process pipeline. To use the constant 'foo' as a source value use 'constants/foo'.

The high_water_property and track_changes are mutually exclusive.

Example:

Traits

Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.

Provides dependency injection friendly methods for serialization.

Provides a trait for the messenger service.

Constants

DERIVATIVE_SEPARATOR

A string which is used to separate base plugin IDs from the derivative ID.

Properties

protected string $pluginId

The plugin_id.

from  PluginBase
protected array $pluginDefinition

The plugin implementation definition.

from  PluginBase
protected array $configuration

Configuration information passed into the plugin.

from  PluginBase
protected TranslationInterface $stringTranslation

The string translation service.

from  StringTranslationTrait
protected array $_serviceIds

An array of service IDs keyed by property name used for serialization.

from  DependencySerializationTrait
protected array $_entityStorages

An array of entity type IDs keyed by the property name of their storages.

from  DependencySerializationTrait
protected MessengerInterface $messenger

The messenger.

from  MessengerTrait
protected ModuleHandlerInterface $moduleHandler

The module handler service.

protected MigrationInterface $migration

The entity migration object.

protected Row $currentRow

The current row from the query.

protected array $currentSourceIds

The primary key of the current row.

protected array $highWaterProperty

Information on the property used as the high-water mark.

protected KeyValueStoreInterface $highWaterStorage

The key-value storage for the high-water value.

protected int $originalHighWater

The high water mark at the beginning of the import operation.

protected bool $cacheCounts

Whether this instance should cache the source count.

protected string $cacheKey

Key to use for caching counts.

protected bool $skipCount

Whether this instance should not attempt to count the source.

protected bool $trackChanges

Flags whether to track changes to incoming data.

protected bool $mapRowAdded

Flags whether source plugin will read the map row and add to data row.

protected CacheBackendInterface $cache

The backend cache.

protected MigrateIdMapInterface $idMap

The migration ID map.

protected Iterator $iterator

The iterator to iterate over the source rows.

Methods

__construct(array $configuration, string $plugin_id, mixed $plugin_definition, MigrationInterface $migration)

Constructs a \Drupal\Component\Plugin\PluginBase object.

string
getPluginId()

Gets the plugin_id of the plugin instance.

string
getBaseId()

Gets the base_plugin_id of the plugin instance.

string|null
getDerivativeId()

Gets the derivative_id of the plugin instance.

array
getPluginDefinition()

Gets the definition of the plugin implementation.

bool
isConfigurable()

Determines if the plugin is configurable.

t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

getStringTranslation()

Gets the string translation service.

$this
setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

setMessenger(MessengerInterface $messenger)

Sets the messenger.

messenger()

Gets the messenger.

initializeIterator()

Initializes the iterator with the source data.

getModuleHandler()

Gets the module handler.

bool
prepareRow(Row $row)

Adds additional data to the row.

getIterator()

Returns the iterator that will yield the row arrays to be processed.

current()

{@inheritdoc}

key()

Gets the iterator key.

valid()

Checks whether the iterator is currently valid.

rewind()

Rewinds the iterator.

next()

{@inheritdoc}

fetchNextRow()

Position the iterator to the following row.

bool
aboveHighWater(Row $row)

Check if the incoming data is newer than what we've previously imported.

bool
rowChanged(Row $row)

Checks if the incoming row has changed since our last import.

getCurrentIds()

Gets the currentSourceIds data member.

int
count(bool $refresh = FALSE)

Gets the source count.

getCache()

Gets the cache object.

int
doCount()

Gets the source count checking if the source is countable or using the iterator_count function.

getHighWaterStorage()

Get the high water storage object.

int|null
getHighWater()

The current value of the high water mark.

saveHighWater(int $high_water)

Save the new high water mark.

array
getHighWaterProperty()

Get information on the property used as the high watermark.

string|null
getHighWaterField()

Get the name of the field used as the high watermark.

preRollback(MigrateRollbackEvent $event)

Performs pre-rollback tasks.

postRollback(MigrateRollbackEvent $event)

Performs post-rollback tasks.

string|null
getSourceModule()

Gets the source module providing the source data.

Details

__construct(array $configuration, string $plugin_id, mixed $plugin_definition, MigrationInterface $migration)

Constructs a \Drupal\Component\Plugin\PluginBase object.

Parameters

array $configuration

A configuration array containing information about the plugin instance.

string $plugin_id

The plugin_id for the plugin instance.

mixed $plugin_definition

The plugin implementation definition.

MigrationInterface $migration

string getPluginId()

Gets the plugin_id of the plugin instance.

Return Value

string

The plugin_id of the plugin instance.

string getBaseId()

Gets the base_plugin_id of the plugin instance.

Return Value

string

The base_plugin_id of the plugin instance.

string|null getDerivativeId()

Gets the derivative_id of the plugin instance.

Return Value

string|null

The derivative_id of the plugin instance NULL otherwise.

array getPluginDefinition()

Gets the definition of the plugin implementation.

Return Value

array

The plugin definition, as returned by the discovery object used by the plugin manager.

bool isConfigurable()

Determines if the plugin is configurable.

Return Value

bool

A boolean indicating whether the plugin is configurable.

protected TranslatableMarkup t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.

In order for strings to be localized, make them available in one of the ways supported by the

Parameters

string $string

A string containing the English text to translate.

array $args

(optional) An associative array of replacements to make after translation. Based on the first character of the key, the value is escaped and/or themed. See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details.

array $options

(optional) An associative array of additional options, with the following elements:

  • 'langcode' (defaults to the current language): A language code, to translate to a language other than what is used to display the page.
  • 'context' (defaults to the empty context): The context the source string belongs to. See the @link i18n Internationalization topic @endlink for more information about string contexts.

Return Value

TranslatableMarkup

An object that, when cast to a string, returns the translated string.

See also

FormattableMarkup::placeholderFormat
TranslatableMarkup::__construct

protected formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

Parameters

$count
$singular
$plural
array $args
array $options

See also

TranslationInterface::formatPlural

protected getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

Parameters

$langcode

See also

PluralFormulaInterface::getNumberOfPlurals

protected TranslationInterface getStringTranslation()

Gets the string translation service.

Return Value

TranslationInterface

The string translation service.

$this setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

Parameters

TranslationInterface $translation

The string translation service.

Return Value

$this

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

setMessenger(MessengerInterface $messenger)

Sets the messenger.

Parameters

MessengerInterface $messenger

The messenger.

MessengerInterface messenger()

Gets the messenger.

Return Value

MessengerInterface

The messenger.

abstract protected Iterator initializeIterator()

Initializes the iterator with the source data.

Return Value

Iterator

Returns an iterable object of data for this source.

protected ModuleHandlerInterface getModuleHandler()

Gets the module handler.

Return Value

ModuleHandlerInterface

The module handler.

bool prepareRow(Row $row)

Adds additional data to the row.

Parameters

Row $row

The row object.

Return Value

bool

FALSE if this row needs to be skipped.

protected Iterator getIterator()

Returns the iterator that will yield the row arrays to be processed.

Return Value

Iterator

The iterator that will yield the row arrays to be processed.

current()

{@inheritdoc}

key()

Gets the iterator key.

Implementation of \Iterator::key() - called when entering a loop iteration, returning the key of the current row. It must be a scalar - we will serialize to fulfill the requirement, but using getCurrentIds() is preferable.

valid()

Checks whether the iterator is currently valid.

Implementation of \Iterator::valid() - called at the top of the loop, returning TRUE to process the loop and FALSE to terminate it.

rewind()

Rewinds the iterator.

Implementation of \Iterator::rewind() - subclasses of SourcePluginBase should implement initializeIterator() to do any class-specific setup for iterating source records.

next()

{@inheritdoc}

protected fetchNextRow()

Position the iterator to the following row.

protected bool aboveHighWater(Row $row)

Check if the incoming data is newer than what we've previously imported.

Parameters

Row $row

The row we're importing.

Return Value

bool

TRUE if the high-water value in the row is greater than our current value.

protected bool rowChanged(Row $row)

Checks if the incoming row has changed since our last import.

Parameters

Row $row

The row we're importing.

Return Value

bool

TRUE if the row has changed otherwise FALSE.

getCurrentIds()

Gets the currentSourceIds data member.

int count(bool $refresh = FALSE)

Gets the source count.

Return a count of available source records, from the cache if appropriate. Returns MigrateSourceInterface::NOT_COUNTABLE if the source is not countable.

Parameters

bool $refresh

(optional) Whether or not to refresh the count. Defaults to FALSE. Not all implementations support the reset flag. In such instances this parameter is ignored and the result of calling the method will always be up to date.

Return Value

int

The count.

protected CacheBackendInterface getCache()

Gets the cache object.

Return Value

CacheBackendInterface

The cache object.

protected int doCount()

Gets the source count checking if the source is countable or using the iterator_count function.

Return Value

int

protected KeyValueStoreInterface getHighWaterStorage()

Get the high water storage object.

Return Value

KeyValueStoreInterface

The storage object.

protected int|null getHighWater()

The current value of the high water mark.

The high water mark defines a timestamp stating the time the import was last run. If the mark is set, only content with a higher timestamp will be imported.

Return Value

int|null

A Unix timestamp representing the high water mark, or NULL if no high water mark has been stored.

protected saveHighWater(int $high_water)

Save the new high water mark.

Parameters

int $high_water

The high water timestamp.

protected array getHighWaterProperty()

Get information on the property used as the high watermark.

Array of 'name' & (optional) db 'alias' properties used for high watermark.

Return Value

array

The property used as the high watermark.

See also

SqlBase::initializeIterator

protected string|null getHighWaterField()

Get the name of the field used as the high watermark.

The name of the field qualified with an alias if available.

Return Value

string|null

The name of the field for the high water mark, or NULL if not set.

See also

SqlBase::initializeIterator

preRollback(MigrateRollbackEvent $event)

Performs pre-rollback tasks.

Parameters

MigrateRollbackEvent $event

The pre-rollback event object.

postRollback(MigrateRollbackEvent $event)

Performs post-rollback tasks.

Parameters

MigrateRollbackEvent $event

The post-rollback event object.

string|null getSourceModule()

Gets the source module providing the source data.

Return Value

string|null

The source module or NULL if not found.