Jump to content

adding images to a page via the api


Ben
 Share

Recommended Posts

I've been working on an automating an import of some pages. For all intents and purposes, it's basically a form submission to a module process that creates/updates a page of a certain template. For the literal minded, I'm importing an existing news archive (headline, story, byline, image) by scripting the form submission.

So far, everything has worked great, and I can update the text/textarea fields without any issues, but I'm not able to figure out how to attach an image in the $_FILES superglobal to the $page object. I suspect my thinking on how to do this is backwards, and there may be an API method that handles some of the lifting - but I don't mind getting my hands dirty either if I need to code it closer to the metal.

I've starting to look at the FieldtypeImage source and the inheritance thereof, but I'm starting to find myself out in the weeds. Does anyone know of a way to affix an uploaded image (outside of the page admin) to a $page object?

The input handling of the submission currently looks a little like this:

foreach($page->template->fields as $template_field){

$field_name = $template_field->name;
$field_class = $template_field->type->className;
$update_value = $this->input->post($field_name);

switch($field_class){
	case 'TitleFieldtype':
	case 'TextareaFieldtype':
		$page->{$field_name} = $update_value;
		break;

	case 'ImageFieldtype': // <----- the confounder
		// not sure what to do here if the field
		// if name is represented in $_FILES
		break;
	...
}
...
}

EDIT: Simplified code block.

Link to comment
Share on other sites

My suspicions were correct, there's no need to do anything crazy. The API handles it fine - $page->images->add() was what I was looking for. Part of the frustration was that I mislabeled the switch case, it should have been 'FieldtypeImage' as opposed to 'ImageFieldtype'.

Now if I could only control the name of the phpRANDOM.tmp filename being created, but that's a different matter.

For anyone looking for a solution, here's an updated snippet:

foreach($page->template->fields as $template_field){

$field_name = $template_field->name;
$field_class = $template_field->type->className;
$update_value = $this->input->post($field_name);

switch($field_class){
	case 'TitleFieldtype':
	case 'TextareaFieldtype':
		$page->{$field_name} = $update_value;
		break;

	case 'FieldtypeImage':

	    if (array_key_exists($field_name,$_FILES)){

			$img_path = $_FILES[$field_name]['tmp_name'];
			$img_name = $_FILES[$field_name]['name'];

			// $_FILES array singular vs. multiple
			// normalize by converting single to array prior to looping.
			// http://php.net/manual/en/reserved.variables.files.php  

			if (!(is_array($img_path))){
				$img_path = array($img_path);
				$img_name = array($img_name);
			}

			for($i=0; $i < count($img_path); $i++){
				$page->images->add($img_path[$i]);
				// TODO, change image name from phpXYZ.tmp
			}

		}

		break;			
	...
}	
...
}
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...