You are here

function __drush_exif_entity_import in Exif 8

Same name and namespace in other branches
  1. 8.2 exif.drush.inc \__drush_exif_entity_import()

Parameters

string $entity_type:

$type:

$uid:

$field:

$file:

$langcode:

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

1 call to __drush_exif_entity_import()
drush_exif_import in ./exif.drush.inc

File

./exif.drush.inc, line 363
Drush extension allowing to run some tasks related to exif.

Code

function __drush_exif_entity_import($entity_type, $type, $uid, $field, $file, $langcode) {
  $title = basename($file);
  $languageManager = \Drupal::getContainer()
    ->get('language_manager');
  if ($langcode == NULL) {
    $langcode = $languageManager
      ->getDefaultLanguage()
      ->getId();
  }
  drush_log(dt('start import of %file as %type entity with title "%title"', [
    '%file' => $file,
    '%type' => $type,
    '%title' => $title,
  ]), "ok");

  // Saving file. automatically added to file entity if present.
  $file_content = file_get_contents($file);

  // Saves a file to the specified destination and creates a database entry.
  $file_temp = file_save_data($file_content, 'public://' . $title, FILE_EXISTS_RENAME);

  // If not file entity, create associated element.
  if ($file_temp && $entity_type != 'file') {
    $entityTypeManager = \Drupal::getContainer()
      ->get('entity_type.manager');
    $entityStorage = $entityTypeManager
      ->getStorage($entity_type);

    // For nodes.
    $attributes = NULL;
    if ($entity_type == 'node') {
      $attributes = [
        'nid' => NULL,
        'type' => $type,
        'title' => $title,
        'alt' => $title,
        'uid' => $uid,
        'revision' => 1,
        'status' => TRUE,
        'promote' => 0,
        'created' => REQUEST_TIME,
        'langcode' => $langcode,
        $field => [
          'target_id' => $file_temp
            ->id(),
        ],
      ];
    }

    // Does not work :( .
    if ($entity_type == 'media') {
      $attributes = [
        'mid' => NULL,
        'bundle' => $type,
        'name' => $title,
        'label' => $title,
        'title' => $title,
        'alt' => $title,
        'uid' => $uid,
        'revision' => 1,
        'status' => TRUE,
        'created' => REQUEST_TIME,
        'langcode' => $langcode,
        $field => [
          'target_id' => $file_temp
            ->id(),
        ],
      ];
    }
    if ($attributes == NULL) {
      drush_log(dt('entity type %entity_type is not supported. %file is not imported.', [
        '%file' => $file,
        '%entity_type' => $entity_type,
      ]), "ko");
    }
    else {

      // Load the node object from the database.
      $entity = $entityStorage
        ->create($attributes);
      $entity
        ->save();
      drush_log(dt('imported %file as %type entity.', [
        '%file' => $file,
        '%type' => $type,
      ]), "ok");
    }
  }
  else {
    drush_log(dt('failed to import %file as %type entity.', [
      '%file' => $file,
      '%type' => $type,
    ]), "ko");
  }
}