You are here

protected function MediaWysiwygFilter::getEmbedCode in Media Migration 8

Creates the replacement token for the specified embed filter.

1 call to MediaWysiwygFilter::getEmbedCode()
MediaWysiwygFilter::transform in src/Plugin/migrate/process/MediaWysiwygFilter.php
Performs the associated process.

File

src/Plugin/migrate/process/MediaWysiwygFilter.php, line 305

Class

MediaWysiwygFilter
Processes [[{"type":"media","fid":"1234",...}]] tokens in content.

Namespace

Drupal\media_migration\Plugin\migrate\process

Code

protected function getEmbedCode(array $embed_metadata) {
  if (empty($embed_metadata['id']) || empty($embed_metadata['view_mode'])) {
    return NULL;
  }
  $destination_filter_plugin_id = MediaMigration::getEmbedTokenDestinationFilterPlugin();
  $embed_media_reference_method = MediaMigration::getEmbedMediaReferenceMethod();
  $filter_destination_is_entity_embed = $destination_filter_plugin_id === MediaMigration::MEDIA_TOKEN_DESTINATION_FILTER_ENTITY_EMBED;
  $reference_method_is_id = $embed_media_reference_method === MediaMigration::EMBED_MEDIA_REFERENCE_METHOD_ID;
  $tag = $filter_destination_is_entity_embed ? 'drupal-entity' : 'drupal-media';

  // Add the static attributes first.
  $attributes = $filter_destination_is_entity_embed ? [
    'data-embed-button' => 'media',
  ] : [];
  $attributes['data-entity-type'] = 'media';

  // Add the attribute that references the embed media.
  $reference_attribute = $reference_method_is_id ? 'data-entity-id' : 'data-entity-uuid';
  if ($reference_method_is_id && $this->configuration['media_migrations']) {
    $embed_metadata['id'] = $this
      ->findDestId($embed_metadata['id'], $this->configuration['media_migrations']);
  }
  $attributes[$reference_attribute] = $reference_method_is_id ? $embed_metadata['id'] : $this->mediaUuidOracle
    ->getMediaUuid((int) $embed_metadata['id']);

  // Add attribute that controls how the embed media is displayed.
  $display_mode_property = $destination_filter_plugin_id === MediaMigration::MEDIA_TOKEN_DESTINATION_FILTER_ENTITY_EMBED ? 'data-entity-embed-display' : 'data-view-mode';
  $attributes[$display_mode_property] = $this
    ->getDisplayPluginId($embed_metadata['view_mode'], $destination_filter_plugin_id);

  // Alt, title, caption and align should be handled conditionally.
  $conditional_attributes = [
    'alt',
    'title',
    'data-caption',
    'data-align',
  ];
  foreach ($conditional_attributes as $conditional_attribute) {
    if (!empty($embed_metadata[$conditional_attribute])) {
      $attributes[$conditional_attribute] = $embed_metadata[$conditional_attribute];
    }
  }
  $attribute = new Attribute($attributes);
  return "<{$tag}{$attribute->__toString()}></{$tag}>";
}