Skip to content

Instantly share code, notes, and snippets.

@gheydon
Created March 18, 2021 23:27
Show Gist options
  • Save gheydon/18cbc5bd267003be593675c2e63f6cb5 to your computer and use it in GitHub Desktop.
Save gheydon/18cbc5bd267003be593675c2e63f6cb5 to your computer and use it in GitHub Desktop.
Set the media entities thumbnail to an uploaded thumbnail.
<?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