WeStandWithUkraine

6 years ago
6
Topic

I am wishing to use the Code Pack beforerender field in order to change the output on an image field. I have a list, with 9 results and I would like to display a default image if there is no image in a field called 'image', plus wrap the that field in a div. 7 of these results have an image uploaded, 2 do not.

So I tried the following code -  which is a guess because of the lack of documentation:

if ( $fields['image']->value ) {

print $fields['image']->form;

} else {

print ('<img src="/default.jpg" alt="Hello" />');

}

Which returns 2 instances of this code:

<img src="/default.jpg" alt="Hello" />

at the top of the list.

Well, that's wrong, but I know that the BeforeRender field is working at some level. What I am unable to do is affect any of the fields in the list with any variation of my code above. There does not seem to be any tutorial explaining the basics of the Code Pack field in terms of how to place it, where to place it and what to do with the other fields it is affecting.

So could somebody please help!

Get a VIP membership
6 years ago
5
Level 1

Okay I have now tried the following:

$fields['art_title']->value = "some title";

from here: http://www.seblod.com/resources/tutorials/using-code-pack

and

$new = 'value="Hey"';
$old = 'value="'.$fields['art_title']->value.'"';
$fields['art_title']->form = str_replace( $old, $new, $fields['art_title']->form );

from here: http://www.seblod.com/resources/tracker/15641

and

$fields['title']->form = $fields['user_id']->value . '-' . $fields['cat_id']->value;

from here: http://www.seblod.com/community/forums/fields-plug-ins/learning-beforerender-form-not-value

Not one of those codes samples works! I must be doing something wrong unless the field is broken in 3.8.2?

1279 Posts
Bucklash
6 years ago
1
Level 2

Hi

Have you tried a position override, wouldn't it be something like this:

<?php
$someImage = $cck->getValue('some_field');

 if ($someImage == '') {
     $someImage = $cck->getValue('some_default_image') //   or $someImage = 'some-default-image.jpg';
}
?>

And then have your output...

<div class="img-wrapper"><img src="/<?php echo $someImage; ?>  class="img-thumbnail"></div>

  

A thing to think about: maybe the before render is ignorant of individual list items and that's why you had 

Which returns 2 instances of this code:

<img src="/default.jpg" alt="Hello" />

Bucklash

6 years ago
0
Level 3

Hi Bucklash, many thanks for replying. I managed to get things working after many hours experimenting! I was having very different results from your tutorial, and I'll make a post showing what I did to get things working.

4229 Posts
Kadministrator
6 years ago
2
Level 2

Hi,

first of all - you need to replace 'art_title' with your actual field name (if you haven't already).

Form property is only used on the form view. To change value on the content view you genneraly need to change fields value property - you should not use print. But for some fields that use multiple datas to render output you also need to change html, typoe or some other property - for upload image you need to change html property

if (empty($fields['image_field']->value))
{
   $fields['image_field']->value = 'test.jpg';
   $fields['image_field']->html = '<img src="/images/test.jpg" title="test" alt="test" />';
}
6 years ago
1
Level 3

Hi Klas, many thanks for replying. I managed to make it work for me, and I will post a proper reply showing how I did things.

It's interesting to note that you use:

$fields['image_field']->html

I did not know that was a property! What does it do differently from

$fields['image_field']->typo

Because I was having some problems wrapping a link on the field I was pre-rendering, thanks.

1279 Posts
Bucklash
6 years ago
0
Level 4

Hi offthehookdesign

Sorry my reply didn't help. Hope it didn't lead you down a wrong and bad path

Could you show the method you used in the tutorial section... that would be cool

Bucklash

Get a Book for SEBLOD