You are here

public function Image::alterMediaEntityMigrationDefinition in Media Migration 8

Same name in this branch
  1. 8 src/Plugin/media_migration/file_entity/Image.php \Drupal\media_migration\Plugin\media_migration\file_entity\Image::alterMediaEntityMigrationDefinition()
  2. 8 src/Plugin/media_migration/file/Image.php \Drupal\media_migration\Plugin\media_migration\file\Image::alterMediaEntityMigrationDefinition()

Overrides FileBase::alterMediaEntityMigrationDefinition

File

src/Plugin/media_migration/file_entity/Image.php, line 33

Class

Image
Image media migration plugin for local image media entities.

Namespace

Drupal\media_migration\Plugin\media_migration\file_entity

Code

public function alterMediaEntityMigrationDefinition(array &$migration_definition, Connection $connection) : void {
  parent::alterMediaEntityMigrationDefinition($migration_definition, $connection);
  $source_field_name = $this
    ->getDestinationMediaSourceFieldName();
  $migration_definition['process'][$source_field_name . '/width'] = 'width';
  $migration_definition['process'][$source_field_name . '/height'] = 'height';
  $migration_definition['process']['thumbnail/target_id'] = 'fid';
  $migration_definition['process']['thumbnail/width'] = 'width';
  $migration_definition['process']['thumbnail/height'] = 'height';

  // These property fields only exist when the file_entity module is
  // installed on the source site.
  foreach (static::PROPERTY_FIELD_NAME_MAP as $property => $field_name) {
    $migration_definition['process']["{$property}_from_media"] = [
      [
        'plugin' => 'skip_on_empty',
        'source' => $field_name,
        'method' => 'process',
      ],
      [
        'plugin' => 'extract',
        'index' => [
          '0',
          'value',
        ],
        // It is impossible to set 'NULL' as default value. Using
        // ['default => NULL'] is equal with not setting the key at all (so
        // every image media migration that should be migrated from an image
        // field will be skipped). Setting the default to a predefined
        // constant that equals to NULL doesn't work either. For example, if
        // we set 'default' to 'constants/alt_and_title_default', then every
        // image that's alt is empty will be migrated with the
        // "constants/alt_and_title_default" string set as alt property. If we
        // don't set 'default' at all, we will get a 'Notice: Undefined index:
        // default_value' exception for empty source values.
        // @todo Revisit after https://www.drupal.org/node/3133516 is
        //   fixed.
        'default' => '',
      ],
      [
        'plugin' => 'default_value',
        'default_value' => NULL,
      ],
    ];
    $property_process = [
      [
        'plugin' => 'null_coalesce',
        'source' => [
          $property,
          "@{$property}_from_media",
        ],
        'default_value' => NULL,
      ],
    ];
    $migration_definition['process'][$source_field_name . '/' . $property] = $migration_definition['process']['thumbnail/' . $property] = $property_process;
  }
}