Same name and namespace in other branches
- 8.x-1.x exif.drush.inc \__drush_exif_entity_import() 1 comment
The drush entity.
Parameters
string $entity_type: The entity type.
string $type: The type.
int $uid: The user id.
string $field: The field.
mixed $file: THe file.
string $langcode: The lang code.
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 - The drush import.
File
-
./
exif.drush.inc, line 390
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();
}
\Drupal::logger(dt('start import of %file as %type entity with title "%title"', [
'%file' => $file,
'%type' => $type,
'%title' => $title,
]));
// 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 = \Drupal::service('file.repository')->writeData($file_content, 'public://' . $title, FileSystemInterface::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' => \Drupal::time()->getRequestTime(),
'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' => \Drupal::time()->getRequestTime(),
'langcode' => $langcode,
$field => [
'target_id' => $file_temp->id(),
],
];
}
if ($attributes == NULL) {
\Drupal::logger(dt('entity type %entity_type is not supported. %file is not imported.', [
'%file' => $file,
'%entity_type' => $entity_type,
]));
}
else {
// Load the node object from the database.
$entity = $entityStorage->create($attributes);
$entity->save();
\Drupal::logger(dt('imported %file as %type entity.', [
'%file' => $file,
'%type' => $type,
]));
}
}
else {
\Drupal::logger(dt('failed to import %file as %type entity.', [
'%file' => $file,
'%type' => $type,
]));
}
}