You are here

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

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\process
View 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

Namesort descending Modifiers Type Description Overrides
CKEditorLinkFileToLinkitFilter::$mediaUuidOracle protected property The media UUID oracle.
CKEditorLinkFileToLinkitFilter::$migration protected property The migration entity.
CKEditorLinkFileToLinkitFilter::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
CKEditorLinkFileToLinkitFilter::transform public function Performs the associated process. Overrides ProcessPluginBase::transform
CKEditorLinkFileToLinkitFilter::__construct public function Constructs a new CKEditorLinkFileToLinkitFilter object. Overrides PluginBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.