Firesphere

It should afaik, except when the upload fails

nickspiel

Which uploads the image but does not write the record into the File db.

nickspiel
Files: Requires logging in with Slack to view/download
Untitled
nickspiel

Does anyone have a any idea how to upload images from a front end form and store them in the File table? This is what I have at the moment:

silbinarywolf

^ This might help someone out. For when you need to replace the <img> tag with say, a <figure> block or whatever and you want uploading to be intuitive in the WYSIWYG (ie. not end up being populated with <figure> in the $Content DB)

silbinarywolf
  1. <?php
  2.  
  3. class ContentFieldExtension extends Extension {
  4. /**
  5. * Override <img> in SiteTree $Content
  6. */
  7. public function Content() {
  8. return $this->owner->replaceImgWithTemplate($this->owner->getField('Content'));
  9. }
  10.  
  11. /**
  12. * Override <img> in ElementContent $HTML
  13. */
  14. public function HTML() {
  15. return $this->owner->replaceImgWithTemplate($this->owner->getField('HTML'));
  16. }
  17.  
  18. /**
  19. * Replace <img> with 'http://WYSIWYG_Img.ss' template.
  20. *
  21. * @return string
  22. */
  23. public function replaceImgWithTemplate($value) {
  24. $value = preg_replace_callback('#<img([^>]+)>#i', function($val) {
  25. $matches = array();
  26. preg_match_all('/src="([^"]+)"/', $val[1], $matches);
  27. if (!isset($matches[1][0])) {
  28. return $val[0];
  29. }
  30. $src = $matches[1][0];
  31.  
  32. // todo(Jake): better regex to just get all attribute and their values.
  33. // Get class
  34. preg_match_all('/class="([^"]+)"/', $val[1], $matches);
  35. $class = isset($matches[1][0]) ? $matches[1][0] : false;
  36.  
  37. // Get alt
  38. preg_match_all('/alt="([^"]+)"/', $val[1], $matches);
  39. $alt = isset($matches[1][0]) ? $matches[1][0] : false;
  40.  
  41. // Get title
  42. preg_match_all('/title="([^"]+)"/', $val[1], $matches);
  43. $title = isset($matches[1][0]) ? $matches[1][0] : false;
  44.  
  45. $image = File::find(urldecode(Director::makeRelative($src)));
  46.  
  47. $result = $this->owner->customise(array(
  48. 'Src' => $src,
  49. 'Alt' => $alt,
  50. 'Class' => $class,
  51. 'Title' => $title,
  52. 'Link' => $src,
  53. 'Image' => $image,
  54. ))->renderWith(array('WYSIWYG_Img'));
  55. $result = $result->RAW();
  56. return $result;
  57. }, $value);
  58. return $value;
  59. }
  60. }