You are here

protected function Acquiadam::prepareEntities in Media: Acquia DAM 8

1 call to Acquiadam::prepareEntities()
Acquiadam::submit in src/Plugin/EntityBrowser/Widget/Acquiadam.php

File

src/Plugin/EntityBrowser/Widget/Acquiadam.php, line 773

Class

Acquiadam
Uses a view to provide entity listing in a browser's widget.

Namespace

Drupal\media_acquiadam\Plugin\EntityBrowser\Widget

Code

protected function prepareEntities(array $form, FormStateInterface $form_state) {

  // Get asset id's from form state.
  $asset_ids = $form_state
    ->getValue('current_selections', []) + array_filter($form_state
    ->getValue('assets', []));

  // Load type information.

  /** @var \Drupal\media\MediaTypeInterface $media_type */
  $media_type = $this->entityTypeManager
    ->getStorage('media_type')
    ->load($this->configuration['media_type']);

  // Get the source field for this type which stores the asset id.
  $source_field = $media_type
    ->getSource()
    ->getSourceFieldDefinition($media_type)
    ->getName();

  // Query for existing entities.
  $existing_ids = $this->entityTypeManager
    ->getStorage('media')
    ->getQuery()
    ->condition('bundle', $media_type
    ->id())
    ->condition($source_field, $asset_ids, 'IN')
    ->execute();

  // Load the entities found.
  $entities = $this->entityTypeManager
    ->getStorage('media')
    ->loadMultiple($existing_ids);

  // Loop through the existing entities.
  foreach ($entities as $entity) {

    // Set the asset id of the current entity.
    $asset_id = $entity
      ->get($source_field)->value;

    // If the asset id of the entity is in the list of asset id's selected.
    if (in_array($asset_id, $asset_ids)) {

      // Remove the asset id from the input so it does not get fetched
      // and does not get created as a duplicate.
      unset($asset_ids[$asset_id]);
    }
  }

  // Fetch the assets.
  $assets = $this->acquiadam
    ->getAssetMultiple($asset_ids);

  // Loop through the returned assets.
  foreach ($assets as $asset) {

    // Initialize entity values.
    $entity_values = [
      'bundle' => $media_type
        ->id(),
      // This should be the current user id.
      'uid' => $this->user
        ->id(),
      // This should be the current language code.
      'langcode' => $this->languageManager
        ->getCurrentLanguage()
        ->getId(),
      // This should map the asset status to the drupal entity status.
      'status' => $asset->status === 'active',
      // Set the entity name to the asset name.
      'name' => $asset->name,
      // Set the chosen source field for this entity to the asset id.
      $source_field => $asset->id,
    ];

    // Create a new entity to represent the asset.
    $entity = $this->entityTypeManager
      ->getStorage('media')
      ->create($entity_values);

    // Save the entity.
    $entity
      ->save();

    // Reload the entity to make sure we have everything populated properly.
    $entity = $this->entityTypeManager
      ->getStorage('media')
      ->load($entity
      ->id());

    // Add the new entity to the array of returned entities.
    $entities[] = $entity;
  }

  // Return the entities.
  return $entities;
}