You are here

function drush_exif_import in Exif 8

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

Parameters

string $entity_type:

string $type:

null $field:

null $path:

null $langcode:

File

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

Code

function drush_exif_import($entity_type = '', $type = '', $field = NULL, $path = NULL, $langcode = NULL) {
  if ($path == NULL or $field == NULL) {
    drush_log(dt('missing at least one parameter.'), "error");
    drush_log(dt('usage: drush exif-import <entity type:(file|media|node)> <type> <fieldname for photo> <path to files>'), "error");
    return;
  }

  // Check path.
  if (!file_exists($path)) {
    drush_log(dt('invalid path %path.', [
      '%path' => $path,
    ]), "error");
    return;
  }

  // Check entity type.
  $entity_types = __check_entity_type($entity_type);
  if (count($entity_types) == 0) {
    return;
  }

  // Check type.
  $selected_types = __check_bundle($entity_types, $type);
  if (count($selected_types) == 0) {
    return;
  }

  // Check field.
  $fields_of_bundle = Drupal::getContainer()
    ->get('entity_field.manager')
    ->getFieldDefinitions($entity_type, $type);
  $selected_field = NULL;
  foreach ($fields_of_bundle as $key => $value) {
    if ($key === $field) {
      $selected_field = $value;
    }
  }
  if ($selected_field == NULL) {
    drush_log(dt('invalid field name %field', [
      '%field' => $field,
    ]), "error");
    drush_log(dt("valid field are"), "error");
    foreach ($fields_of_bundle as $key => $value) {
      drush_log(dt("%key", [
        "%key" => $key,
      ]), "error");
    }
    return;
  }
  if ($selected_field
    ->getType() !== "image" && $selected_field
    ->getType() !== "media") {
    drush_log(dt('field name %field is not a image field', [
      '%field' => $field,
    ]), "error");
    return;
  }

  // Find files
  $files = [];
  if (is_file($path)) {
    $files[] = $path;
  }
  else {
    $paths[] = $path;
    while (count($paths) != 0) {
      $v = array_shift($paths);
      drush_log(dt('looking in path %path.', [
        '%path' => $v,
      ]), "ok");
      foreach (glob($v . '/*') as $item) {
        if ($item != '.' and $item != '..' and is_dir($item)) {
          $paths[] = $item;
        }
        elseif (is_file($item) && exif_imagetype($item) == IMAGETYPE_JPEG) {
          $files[] = $item;
        }
      }
    }
  }

  // Import
  drush_log(dt('importing %count files.', [
    '%count' => count($files),
  ]), "ok");
  foreach ($files as $file) {
    __drush_exif_entity_import($entity_type, $type, 1, $field, $file, $langcode);
  }
}