Last active
August 29, 2015 14:09
-
-
Save somatonic/49f9e0a7faa8f6e6cfa3 to your computer and use it in GitHub Desktop.
AddImagesFromUrl.module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* AddImagesFromUrl | |
* | |
* On a page with fields | |
* "add_images_url" text field | |
* "images" images field | |
* | |
* This basic example module will add the image from the url to the images field on page save. | |
* | |
* This would be just meant as an example! | |
* | |
* - Maybe this could lead to problems if you don't test for if the image is really an image before loading into the images field (local disk). Would need confirmation through if PW isn't already checking (?) | |
* - Copyrights? | |
* - Probably could need some check for allow_url_fopen (not sure either on this one) | |
* | |
* (Make it your own and modify) | |
* | |
* | |
*/ | |
class AddImagesFromUrl extends WireData implements Module { | |
/** | |
* getModuleInfo is a module required by all modules to tell ProcessWire about them | |
* | |
* @return array | |
* | |
*/ | |
public static function getModuleInfo() { | |
return array( | |
'title' => 'AddImagesFromUrl', | |
'version' => 101, | |
'summary' => 'Add images from url to images field', | |
'href' => 'http://www.processwire.com', | |
'singular' => true, | |
'autoload' => "template=admin", | |
'icon' => 'smile-o', | |
); | |
} | |
public function init() { | |
$this->addHookAfter('Pages::saveReady', $this, 'addImage'); | |
} | |
public function addImage(HookEvent $event) { | |
$page = $event->arguments("page"); | |
if($page->template != "basic-page") return; | |
if(!$page->add_images_url) return; | |
if(strpos($page->add_images_url, ".jpg") != false) { | |
$page->images->add($page->add_images_url); | |
$this->message("Added image from '$page->add_images_url' to images field"); | |
$page->add_images_url = ''; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not sure if it depends on Browsers or Systems, but for interactive adding URLs to imagesfields I just can input URLs into the FilesUpload Inputfield of the core imagesfield. (Win7 FireFox)