Created
March 18, 2021 23:27
-
-
Save gheydon/18cbc5bd267003be593675c2e63f6cb5 to your computer and use it in GitHub Desktop.
Set the media entities thumbnail to an uploaded thumbnail.
This file contains hidden or 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
<?php | |
/** | |
* Implements hook_ENTITY_TYPE_presave(). | |
*/ | |
function example_media_presave(EntityInterface $entity) { | |
if ($entity->bundle() == 'resource') { | |
if (!$entity->get('field_thumbnail')->isEmpty()) { | |
$file = $entity->field_thumbnail->entity; | |
$entity->set('thumbnail', $file); | |
} | |
else { | |
// If there is nothing in the field_thumbnail then delete the existing | |
// thumbnail. | |
$entity->set('thumbnail'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment