Created
July 16, 2019 19:00
-
-
Save xurizaemon/bf45c859fa8034702f23af804eb486a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/migrate_media_handler.info.yml b/migrate_media_handler.info.yml | |
index 0f9eb39..0e8e053 100644 | |
--- a/migrate_media_handler.info.yml | |
+++ b/migrate_media_handler.info.yml | |
@@ -5,7 +5,4 @@ package: 'Migrate' | |
core: '8.x' | |
dependencies: | |
- drupal:migrate | |
- - drupal:migrate_plus | |
- - drupal:migrate_tools | |
- - drupal:migrate_drupal | |
- drupal:media | |
diff --git a/src/MediaMaker.php b/src/MediaMaker.php | |
index 110d316..3bfd47e 100644 | |
--- a/src/MediaMaker.php | |
+++ b/src/MediaMaker.php | |
@@ -132,26 +132,39 @@ class MediaMaker implements ContainerInjectionInterface { | |
* @return bool|\Drupal\media\Entity\Media | |
* Return media entity or FALSE | |
*/ | |
- public function makeImageEntity($file_id, $path = '', $alt = '', $title = '') { | |
- // Load file entity. | |
+ public function makeImageEntity($file_id, array $params = []) { | |
+ /* @var Drupal\file\Entity\File */ | |
$file = $this->entityManager->getStorage('file')->load($file_id); | |
+ | |
+ $defaults = [ | |
+ 'bundle' => 'image', | |
+ 'langcode' => 'en', | |
+ 'alt' => '', | |
+ 'title' => '', | |
+ 'uid' => 0, | |
+ ]; | |
+ $params = array_merge($defaults, $params); | |
+ | |
// If that's successful, carry on. | |
if ($file) { | |
// Create media entity with saved file. | |
// Please note accessibility concerns around empty alt & title. | |
$media = $this->entityManager->getStorage('media')->create([ | |
- 'bundle' => 'image', | |
+ 'bundle' => $params['bundle'], | |
'field_original_ref' => $path, | |
'field_media_image' => [ | |
'target_id' => $file_id, | |
- 'alt' => $alt, | |
- 'title' => $title, | |
+ 'alt' => $params['alt'], | |
+ 'title' => $params['title'], | |
], | |
- 'langcode' => 'en', | |
+ 'langcode' => $params['langcode'], | |
+ 'uid' => $params['uid'], | |
]); | |
- $owner = $file->getOwnerId(); | |
+ | |
+ if ($owner = $file->getOwnerId()) { | |
+ $media->setOwnerId($owner); | |
+ } | |
$filename = $file->getFilename(); | |
- $media->setOwnerId($owner); | |
$media->setName($filename); | |
$media->save(); | |
return $media; | |
diff --git a/src/Plugin/migrate/process/UpdateFileToMedia.php b/src/Plugin/migrate/process/UpdateFileToMedia.php | |
index be4c89f..cf63487 100644 | |
--- a/src/Plugin/migrate/process/UpdateFileToMedia.php | |
+++ b/src/Plugin/migrate/process/UpdateFileToMedia.php | |
@@ -63,8 +63,14 @@ class UpdateFileToMedia extends ProcessPluginBase implements ContainerFactoryPlu | |
* {@inheritdoc} | |
*/ | |
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { | |
+ $params = [ | |
+ 'alt' => (isset($this->configuration['alt'])) ? $this->configuration['alt'] : '', | |
+ 'bundle' => (isset($this->configuration['bundle'])) ? $this->configuration['bundle'] : 'image', | |
+ 'langcode' => (isset($this->configuration['langcode'])) ? $this->configuration['langcode'] : '', | |
+ 'title' => (isset($this->configuration['title'])) ? $this->configuration['title'] : '', | |
+ ]; | |
if (!empty($value)) { | |
- $media = $this->mediaMaker->makeImageEntity($value); | |
+ $media = $this->mediaMaker->makeImageEntity($value, $params); | |
return $media->id(); | |
} | |
return NULL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment