class CKEditorLinkFileToLinkitFilter in Media Migration 8
Transforms <a href="/file/%"> tags to <a data-entity-substitution="media" …>.
Plugin annotation
@MigrateProcessPlugin(
id = "ckeditor_link_file_to_linkit"
)
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\media_migration\Plugin\migrate\process\CKEditorLinkFileToLinkitFilter implements ContainerFactoryPluginInterface
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of CKEditorLinkFileToLinkitFilter
1 file declares its use of CKEditorLinkFileToLinkitFilter
- CKEditorLinkFileToLinkitFilterTest.php in tests/
src/ Unit/ Plugin/ migrate/ process/ CKEditorLinkFileToLinkitFilterTest.php
File
- src/
Plugin/ migrate/ process/ CKEditorLinkFileToLinkitFilter.php, line 23
Namespace
Drupal\media_migration\Plugin\migrate\processView source
class CKEditorLinkFileToLinkitFilter extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* The migration entity.
*
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* The media UUID oracle.
*
* @var \Drupal\media_migration\MediaMigrationUuidOracleInterface
*/
protected $mediaUuidOracle;
/**
* Constructs a new CKEditorLinkFileToLinkitFilter object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\migrate\Plugin\MigrationInterface $migration
* The migration entity.
* @param \Drupal\media_migration\MediaMigrationUuidOracleInterface $media_uuid_oracle
* The media UUID oracle.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MediaMigrationUuidOracleInterface $media_uuid_oracle) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migration = $migration;
$this->mediaUuidOracle = $media_uuid_oracle;
}
/**
* {@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('media_migration.media_uuid_oracle'));
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$value_is_array = is_array($value);
$text = (string) ($value_is_array ? $value['value'] : $value);
if (!preg_match(MediaMigration::PCRE_PATTERN_LINKIT_FILE_LINK, $text)) {
return $value;
}
$html5 = new HTML5([
'disable_html_ns' => TRUE,
]);
// Compatibility for older HTML5 versions (e.g. in Drupal core 8.9.x).
try {
$dom = $html5
->parse('<html><body>' . $text . '</body></html>');
} catch (\TypeError $e) {
$text_stream = new StringInputStream('<html><body>' . $text . '</body></html>');
$dom = $html5
->parse($text_stream);
}
foreach ($dom
->getElementsByTagName('a') as $node) {
/** @var \DOMElement $node */
$src = rawurldecode($node
->getAttribute('href'));
$url_parts = parse_url($src);
$path = $url_parts['path'] ?? '';
if (strpos($path, '/file/') !== 0) {
continue;
}
$file_entity_id = basename($path);
if (!preg_match('/^\\d+$/', $file_entity_id)) {
continue;
}
$media_uuid = $this->mediaUuidOracle
->getMediaUuid((int) $file_entity_id);
// Add the additional attributes to allow the linkit filter to work.
$node
->setAttribute('data-entity-substitution', 'media');
$node
->setAttribute('data-entity-type', 'media');
$node
->setAttribute('data-entity-uuid', $media_uuid);
}
$result = $html5
->saveHTML($dom->documentElement->firstChild->childNodes);
if ($value_is_array) {
$value['value'] = $result;
}
else {
$value = $result;
}
return $value;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CKEditorLinkFileToLinkitFilter:: |
protected | property | The media UUID oracle. | |
CKEditorLinkFileToLinkitFilter:: |
protected | property | The migration entity. | |
CKEditorLinkFileToLinkitFilter:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
CKEditorLinkFileToLinkitFilter:: |
public | function |
Performs the associated process. Overrides ProcessPluginBase:: |
|
CKEditorLinkFileToLinkitFilter:: |
public | function |
Constructs a new CKEditorLinkFileToLinkitFilter object. Overrides PluginBase:: |
|
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 | |
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. | |
ProcessPluginBase:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface:: |
3 |
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. |