| @@ 8-38 (lines=31) @@ | ||
| 5 | /** |
|
| 6 | * Image field handler for Drupal 7. |
|
| 7 | */ |
|
| 8 | class ImageHandler extends AbstractHandler { |
|
| 9 | ||
| 10 | /** |
|
| 11 | * {@inheritdoc} |
|
| 12 | */ |
|
| 13 | public function expand($values) { |
|
| 14 | $data = file_get_contents($values[0]); |
|
| 15 | if (FALSE === $data) { |
|
| 16 | throw new \Exception("Error reading file"); |
|
| 17 | } |
|
| 18 | ||
| 19 | /* @var \Drupal\file\FileInterface $file */ |
|
| 20 | $file = file_save_data( |
|
| 21 | $data, |
|
| 22 | 'public://' . uniqid() . '.jpg'); |
|
| 23 | ||
| 24 | if (FALSE === $file) { |
|
| 25 | throw new \Exception("Error saving file"); |
|
| 26 | } |
|
| 27 | ||
| 28 | $file->save(); |
|
| 29 | ||
| 30 | $return = array( |
|
| 31 | 'target_id' => $file->id(), |
|
| 32 | 'alt' => 'Behat test image', |
|
| 33 | 'title' => 'Behat test image', |
|
| 34 | ); |
|
| 35 | return $return; |
|
| 36 | } |
|
| 37 | ||
| 38 | } |
|
| 39 | ||
| @@ 18-50 (lines=33) @@ | ||
| 15 | * weight = -100, |
|
| 16 | * ) |
|
| 17 | */ |
|
| 18 | class ImageDrupal8 extends DriverFieldPluginDrupal8Base |
|
| 19 | { |
|
| 20 | ||
| 21 | /** |
|
| 22 | * {@inheritdoc} |
|
| 23 | */ |
|
| 24 | protected function processValue($value) |
|
| 25 | { |
|
| 26 | $data = file_get_contents($value['target_id']); |
|
| 27 | if (false === $data) { |
|
| 28 | throw new \Exception("Error reading file"); |
|
| 29 | } |
|
| 30 | ||
| 31 | /* @var \Drupal\file\FileInterface $file */ |
|
| 32 | $file = file_save_data( |
|
| 33 | $data, |
|
| 34 | 'public://' . uniqid() . '.jpg' |
|
| 35 | ); |
|
| 36 | ||
| 37 | if (false === $file) { |
|
| 38 | throw new \Exception("Error saving file"); |
|
| 39 | } |
|
| 40 | ||
| 41 | $file->save(); |
|
| 42 | ||
| 43 | $return = array( |
|
| 44 | 'target_id' => $file->id(), |
|
| 45 | 'alt' => 'Behat test image', |
|
| 46 | 'title' => 'Behat test image', |
|
| 47 | ); |
|
| 48 | return $return; |
|
| 49 | } |
|
| 50 | } |
|
| 51 | ||