acquia_dam-1.0.0-rc1/acquia_dam.post_update.php
acquia_dam.post_update.php
<?php /** * @file * Post update functions for Acquia Dam. */ declare(strict_types=1); use Drupal\acquia_dam\Entity\MediaSourceField; use Drupal\acquia_dam\Plugin\Field\FieldType\AssetItem; /** * Populate the external_id field. */ function acquia_dam_post_update_existing_media_external_id_field(&$sandbox) { // Populate existing media. $media_ids = \Drupal::entityTypeManager()->getStorage('media') ->getQuery() ->condition(MediaSourceField::SOURCE_FIELD_NAME, NULL, 'IS NOT NULL') ->execute(); if (empty($sandbox['progress'])) { $sandbox['progress'] = 0; $sandbox['media_ids'] = array_values($media_ids); $sandbox['max'] = count($media_ids); } $start = $sandbox['progress']; $end = min($sandbox['max'], $start + 10); for ($i = $start; $i < $end; $i++) { $media_entity_id = $sandbox['media_ids'][$i]; /** @var \Drupal\media\MediaInterface $media_entity */ $media_entity = \Drupal::entityTypeManager()->getStorage('media') ->load($media_entity_id); $source_field = $media_entity->get(MediaSourceField::SOURCE_FIELD_NAME); if ($source_field->isEmpty()) { continue; } $source_field_item = $source_field->first(); assert($source_field_item instanceof AssetItem); $source = $media_entity->getSource(); $source_data = $source->getSourceFieldValue($media_entity); if (isset($source_data['external_id']) && $source_data['external_id']) { continue; } $external_id = $source->getMetadata($media_entity, "external_id"); $media_entity->get(MediaSourceField::SOURCE_FIELD_NAME)->external_id = $external_id; $media_entity->save(); } if ($sandbox['max'] > 0 && $end < $sandbox['max']) { $sandbox['progress'] = $end; $sandbox['#finished'] = ($end - 1) / $sandbox['max']; } else { $sandbox['#finished'] = 1; } } /** * New service definition crop_new_asset_version_subscriber. */ function acquia_dam_post_update_add_crop_new_asset_version_subscriber(&$sandbox) { // Empty post_update hook to rebuild service container. }