migrate_media_handler-8.x-1.0-rc4/src/Plugin/migrate/process/UpdateFileToDocument.php
src/Plugin/migrate/process/UpdateFileToDocument.php
<?php
namespace Drupal\migrate_media_handler\Plugin\migrate\process;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\migrate_media_handler\MediaMaker;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Convert existing file field references to media entities.
*
* Available config keys:
* - source_field: the field on the entity with the file reference.
* - target_bundle: the media type to which this gets migrated. Defaults to
* 'document'.
*
* Examples:
*
* @code
* process:
* field_document:
* -
* plugin: migration_lookup
* source: field_old_document
* migration: example_file
* no_stub: true
* -
* plugin: update_file_to_document
* @endcode
*
* A basic file->media migration can't include display & desc, as that info
* is not stored on the file, but is part of an documen t/file field in nodes or
* other entities. Update File to Document allows you to use that entity data to
* update the new media entity with description & display data.
*
* * Description & Display usage.
* @code
* process:
* field_document:
* -
* plugin: migration_lookup
* source: field_old_document
* migration: example_file
* no_stub: true
* -
* plugin: update_file_to_document
* source_field: field_old_document
* @endcode
*
* If you have your documents stored in a bundle other than 'document', you can use
* the target_bundle param to change which bundle they are saved in.
*
* @code
* process:
* field_document:
* -
* plugin: migration_lookup
* source: field_old_document
* migration: example_file
* no_stub: true
* -
* plugin: update_file_to_document
* target_bundle: whitepapers
* @endcode
*
* @see \Drupal\migrate\Plugin\MigrateProcessInterface
*
* @MigrateProcessPlugin(
* id = "update_file_to_document"
* )
*/
class UpdateFileToDocument extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* The media maker.
*
* @var \Drupal\migrate_media_handler\MediaMaker
*/
protected $mediaMaker;
/**
* Constructs a UpdateFileToDocument process plugin instance.
*
* @param array $configuration
* The plugin configuration.
* @param string $plugin_id
* The plugin ID.
* @param array $plugin_definition
* The plugin definition.
* @param \Drupal\migrate_media_handler\MediaMaker $media_maker
* Media Maker service instance.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, MediaMaker $media_maker) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->mediaMaker = $media_maker;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('migrate_media_handler.mediamaker')
);
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
// If the migration lookup returned a file reference, make its media entity.
if (!empty($value)) {
$media = $this->mediaMaker->makeDocumentEntity($value, $row, $this->configuration);
if ($media) {
return $media->id();
}
}
return NULL;
}
}
