class GatherContentSubProcess in GatherContent 8.5
Perform custom value transformation.
@\Drupal\migrate\Annotation\MigrateProcessPlugin( id = "gather_content_sub_process", handle_multiples = TRUE )
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\migrate\Plugin\migrate\process\SubProcess
- class \Drupal\gathercontent\Plugin\migrate\process\GatherContentSubProcess implements ContainerFactoryPluginInterface
- class \Drupal\migrate\Plugin\migrate\process\SubProcess
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of GatherContentSubProcess
File
- src/
Plugin/ migrate/ process/ GatherContentSubProcess.php, line 24
Namespace
Drupal\gathercontent\Plugin\migrate\processView source
class GatherContentSubProcess extends SubProcess implements ContainerFactoryPluginInterface {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('database'), $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$data = parent::transform($value, $migrate_executable, $row, $destination_property);
if (is_array($data)) {
foreach ($data as $dataKey => $dataValue) {
$data[$dataKey] = $this
->replaceUrls($dataValue);
}
return $data;
}
return $this
->replaceUrls($data);
}
/**
* Replaces the GC urls to Drupal entity url.
*
* @param mixed $data
* String or array containing GC urls.
*
* @return array|string
* Returns the replaced string or array.
*/
protected function replaceUrls($data) {
if (empty($data)) {
return $data;
}
if (!is_array($data)) {
return $this
->gcUrlToDrupal($data);
}
foreach ($data as $key => $value) {
$data[$key] = $this
->gcUrlToDrupal($value);
}
return $data;
}
/**
* Returns formatted string with replaced urls.
*
* @param string $text
* String containing the urls.
*
* @return string
* Formatted string containing the replaced urls.
*/
protected function gcUrlToDrupal(string $text) {
$collectedUrls = [];
preg_match_all("/https?:\\/\\/([a-z0-9]+)\\.gathercontent\\.com\\/item\\/(\\d+)/", $text, $collectedUrls);
if (empty($collectedUrls[0])) {
return $text;
}
if (!is_array($collectedUrls[0])) {
$collectedUrls[0] = [
$collectedUrls[0],
];
}
$gcUrls = array_unique(array_combine($collectedUrls[2], $collectedUrls[0]));
$query = $this->database
->select('gathercontent_entity_mapping')
->fields('gathercontent_entity_mapping', [
'gc_id',
'entity_id',
'entity_type',
]);
$query
->condition('gc_id', array_unique($collectedUrls[2]), 'IN');
$results = $query
->execute()
->fetchAll();
if (empty($results)) {
return $text;
}
$validGcUrls = [];
$internalUrls = [];
foreach ($results as $result) {
$validGcUrls[] = $gcUrls[$result->gc_id];
try {
$entity = $this->entityTypeManager
->getStorage($result->entity_type)
->load($result->entity_id);
} catch (InvalidPluginDefinitionException $e) {
} catch (PluginNotFoundException $e) {
}
$entityType = $entity
->getEntityTypeId();
$entityId = $entity
->id();
$internalUrls[] = Url::fromUri("base:/{$entityType}/{$entityId}")
->setAbsolute()
->toString();
}
$text = str_replace($validGcUrls, $internalUrls, $text);
return $text;
}
/**
* {@inheritdoc}
*/
public function multiple() {
return TRUE;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
GatherContentSubProcess:: |
protected | property | The database connection. | |
GatherContentSubProcess:: |
protected | property | Entity type manager. | |
GatherContentSubProcess:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
GatherContentSubProcess:: |
protected | function | Returns formatted string with replaced urls. | |
GatherContentSubProcess:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides SubProcess:: |
|
GatherContentSubProcess:: |
protected | function | Replaces the GC urls to Drupal entity url. | |
GatherContentSubProcess:: |
public | function |
Performs the associated process. Overrides SubProcess:: |
|
GatherContentSubProcess:: |
public | function |
SubProcess constructor. Overrides SubProcess:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
SubProcess:: |
protected | function | Runs the process pipeline for the key to determine its dynamic name. |