Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save azurams/280e5985083145add3be3662775792f8 to your computer and use it in GitHub Desktop.
Save azurams/280e5985083145add3be3662775792f8 to your computer and use it in GitHub Desktop.
Patch handles File field expansion for D8 codebase
diff --git a/src/Drupal/Driver/Fields/Drupal8/FileHandler.php b/src/Drupal/Driver/Fields/Drupal8/FileHandler.php
index bbef214..ee4a1f5 100644
--- a/src/Drupal/Driver/Fields/Drupal8/FileHandler.php
+++ b/src/Drupal/Driver/Fields/Drupal8/FileHandler.php
@@ -5,5 +5,37 @@ namespace Drupal\Driver\Fields\Drupal8;
/**
* 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;
+ }
+
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment