Make Image Place Holder in Custom Block

Permalink
Hi

Wondering if someone can help or point me in the right direction...

I've used Designer Content to make a custom block.

One of the options I've worked in is to upload an image, though I haven't made it a required element.

If the site editor doesn't add an image, I would like the block to choose a default image in it's place once they add the block.

Any ideas?

Cheers! :)

PS - I'm not a php guru, just an html and css designer...

PatrickCassidy
 
mnakalay replied on at Permalink Best Answer Reply
mnakalay
Hello,
In the controller created by Designer content, you can modify the view() function to do what you want.

In this example I have only one field and it's an image selector
public function view() {
   $this->set('field_1_image', (empty($this->field_1_image_fID) ? null : $this->get_image_object($this->field_1_image_fID, 350, 0, false)));
}

You can modify it to be
public function view() {
   $this->set('field_1_image', (empty($this->field_1_image_fID) ? $this->get_image_object(default_image_id, 350, 0, false) : $this->get_image_object($this->field_1_image_fID, 350, 0, false)));
   }


What happened is that I replaced the null value with a function to get the default image. All you need to do is replace "default_image_id" with the id of the default image. You can find it in the file manager.
PatrickCassidy replied on at Permalink Reply
PatrickCassidy
Thanks mnakaly,

My code I've got in the view looks a little different to yours.
I can't see a 'null' in my code. I've wrapped it in a div too.

My code:

<?php  if (!empty($field_2_image)): ?>
<div class="product-thumb">
   <img src="<?php  echo $field_2_image->src; ?>" width="<?php  echo $field_2_image->width; ?>" height="<?php  echo $field_2_image->height; ?>" alt="" />
   </div>
<?php  endif; ?>
mnakalay replied on at Permalink Reply
mnakalay
Hi again,
I said in the controller not in the view. It is in the view function in the file controller.php not view.php
PatrickCassidy replied on at Permalink Reply
PatrickCassidy
Oh cheers friend... :)