protected function ContentModelUpdater::setEntityBundle in Panopoly 8.2
Sets an entity's bundle.
Parameters
\Drupal\Core\Entity\ContentEntityInterface $entity: The entity.
string $bundle: The bundle.
1 call to ContentModelUpdater::setEntityBundle()
- ContentModelUpdater::convertMediaEntity in modules/
panopoly/ panopoly_media/ src/ Update/ ContentModelUpdater.php - Converts media entities to new panopoly_* types.
File
- modules/
panopoly/ panopoly_media/ src/ Update/ ContentModelUpdater.php, line 1038
Class
- ContentModelUpdater
- Applies changes to media content model from schema versions 8204 to 8205.
Namespace
Drupal\panopoly_media\UpdateCode
protected function setEntityBundle(ContentEntityInterface $entity, $bundle) {
if (!$entity instanceof ContentEntityBase) {
return;
}
// Update the bundle in the database, and clear entity from cache.
\Drupal::database()
->update('media')
->condition('mid', $entity
->id())
->fields([
'bundle' => $bundle,
])
->execute();
\Drupal::entityTypeManager()
->getStorage('media')
->resetCache([
$entity
->id(),
]);
// Grab the original fields.
$fields = $entity
->getFields(FALSE);
// Load a fresh copy of the entity with updated field definitions.
$new = Media::load($entity
->id());
// Copy the values of each of the original fields into the new fields.
// Convert field_media_video_embed_field to field_media_oembed_video and
// skip setting the bundle.
foreach ($fields as $field => $value) {
$field = $field == 'field_media_video_embed_field' ? 'field_media_oembed_video' : $field;
/** @var \Drupal\Core\Field\FieldItemListInterface $new_field */
$new_field = $new
->get($field);
if ($field != 'bundle') {
$new_field
->setValue($value
->getValue());
}
}
// Set the original property in order to prevent the media source from being
// compared.
$new->original = clone $new;
// Save with new revision.
if ($new instanceof RevisionableInterface) {
$new
->setNewRevision();
if ($new instanceof RevisionLogInterface) {
$new
->setRevisionLogMessage('Converted to ' . $bundle);
}
}
$new
->save();
}