Jump to content

access the description field of an image (media manager)


froot
 Share

Recommended Posts

I have added a preview field to the media-manager-image template which I populated on many pages.
Then I realised that there's a description field in each image anyway. 
Now I want to populate the description field with the value from the preview fields.
(I could also set the description field directly with the CSV file that I used for the preview field, but…)

…first want to see if I can access the field(s) alright. Then I would try to set populate the fields and save the pages. 
Here's my code so far:

	$images = $pages("template=media-manager-image, include=all");

	foreach ($images as $image) {
		echo 'ID: ' . $image->id;
		echo '<br>';
		echo 'PREVIEW: ' . $image->preview;
		echo '<br>';
		foreach($image as $ii) {
			if ($ii) { // cause I keep getting warnings...
				foreach ($ii as $iii) {
					echo 'DESCRIPTION: ' . $iii['description'];
					echo '<br>';
				}	
			}
		}
		echo '<hr>';
	}

with the above code, I pretty much get what I want but a lot of warnings:

PHP Warning: foreach() argument must be of type array|object, int given
PHP Warning: foreach() argument must be of type array|object, string given 

How can I bail out of those warnings? 

Link to comment
Share on other sites

after a bit of more investigation I was able to get rid of the warnings yet output the preview an description alright.

However, cannot seem to set the values for the images…

    $images = $pages("template=media-manager-image, include=all, id=3102");

    foreach ($images as $image) {
        foreach($image as $ii) {
            $image->of(false);
            if (gettype($ii) != "object" || gettype($ii->first) != "object") { continue; }
            $ii->first->description = $image->preview;
            foreach ($ii as $iii) {
                $item = $iii;
            }
            $image->save($iii);
            $image->of(true);
        }
    }

Please help! ? 

Link to comment
Share on other sites

OK I figured it out.

    $images = $pages("template=media-manager-image, include=all");

    foreach ($images as $image) {

        $preview = $image->preview;
        $description = $image->media_manager_image->first()->description;

        if ($preview != '') {
            $image->of(false);
            $image->media_manager_image->first()->description = $preview;
            $image->save();
            $image->of(true);    
        }

    }

 

  • Like 1
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...