migrate_spip-1.0.0/modules/examples/src/Plugin/SpipRichText/MediaEmbeds.php
modules/examples/src/Plugin/SpipRichText/MediaEmbeds.php
<?php
declare(strict_types=1);
namespace Drupal\migrate_spip_examples\Plugin\SpipRichText;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\migrate_spip\Attribute\SpipRichText;
/**
* Manage SPIP embed tags into media.
*
* Need to be executed before tables.
*/
#[SpipRichText(
id: 'media_embeds',
label: new TranslatableMarkup('Media embeds'),
weight: -40,
)]
final class MediaEmbeds extends MediaBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function applyMediaAlignment(string $text, ?string $align = NULL): string {
/*
* Search "<emb123|center>" SPIP tags.
*
* Consider that you have previously migrated SPIP images and videos
* to media using "media_images" and "media_videos" migration tasks.
*
* @see migrate_spip_plus
*/
if (!preg_match_all('#<emb([^|>]+)' . ($align ? '\|' . $align : '') . '>#s', $text, $matches)) {
return $text;
}
foreach ($matches[1] as $index => $match) {
$replace = '';
if (!empty($match)) {
$destId1 = FALSE;
if ($this->connection->schema()->tableExists('migrate_map_media_images')) {
$destId1 = $this->connection->select('migrate_map_media_images', 'mmmi')
->condition('mmmi.sourceid1', $match)
->fields('mmmi', ['destid1'])
->execute()
->fetchField();
}
if (
empty($destId1) &&
$this->connection->schema()->tableExists('migrate_map_media_videos')
) {
$destId1 = $this->connection->select('migrate_map_media_videos', 'mmv')
->condition('mmv.sourceid1', $match)
->fields('mmv', ['destid1'])
->execute()
->fetchField();
}
if (!empty($destId1)) {
/** @var \Drupal\media\MediaInterface|null $media */
$media = $this->entityTypeManager->getStorage('media')
->load($destId1);
if (isset($media)) {
$replace = $this->getMediaTag($media, $align);
}
}
}
$text = str_replace(
$matches[0][$index],
$replace,
$text
);
}
return $text;
}
}
