Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handles File field expansion for D8 codebase #123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/Drupal/Driver/Fields/Drupal8/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,37 @@
/**
* File field handler for Drupal 8.
*/
class FileHandler extends EntityReferenceHandler {
class FileHandler extends AbstractHandler {

/**
* {@inheritdoc}
*/
public function expand($values) {
$data = file_get_contents($values[0]);
if (FALSE === $data) {
throw new \Exception("Error reading file");
}

$ext = pathinfo($values[0], PATHINFO_EXTENSION);
$ext = !empty($ext) ? '.' . $ext : '';

/* @var \Drupal\file\FileInterface $file */
$file = file_save_data(
$data,
'public://' . uniqid() . $ext);

if (FALSE === $file) {
throw new \Exception("Error saving file");
}

$file->save();

$return = array(
'target_id' => $file->id(),
'display' => '1',
'description' => 'Behat test file',
);
return $return;
}

}