Jump to content

How to test for empty fields?


antknight
 Share

Recommended Posts

First question. How does PW deal with empty fields?

Second question. I have made a repeater for a slideshow where you can set image, delay and caption. I only want the h6 to display if there are actually items in the repeater. How can I test for this?

<h6>Images</h6>

 <?php
 foreach($page->gallery as $slide) {
	 echo "<a rel='prettyPhoto[pp_gal]' title='{$slide->gallery_caption}' href='{$slide->gallery_image->url}'>
	 <img class='peKbGallery' data-delay='{$slide->gallery_delay}' src='{$slide->gallery_image->url}' alt='{$slide->gallery_caption} (Click to enlarge)' />
	 </a>";
 }
 ?>

Thanks!

Link to comment
Share on other sites

You could use the count function:

if(count($page->gallery)) {
// here your code (for displaying h6 ...)
} else {
// ...
}

For more information look at here http://processwire.c...ypes/repeaters/ or here http://processwire.c...pi/arrays/page/

The value of a repeater field is a PageArray. As a result, you may interact with them exactly like you would any other group of pages in ProcessWire.
  • Like 1
Link to comment
Share on other sites

First question. How does PW deal with empty fields?
if($page->body){
//not empty
}else{
//empty
}

other options

if($page->body) echo "body not empty";
if(!$page->body) echo "body empty";

for the second question:

<?php if($page->gallery->count() > 0){?>
<h6>Images</h6>

<?php
foreach($page->gallery as $slide) {
   echo "<a rel='prettyPhoto[pp_gal]' title='{$slide->gallery_caption}' href='{$slide->gallery_image->url}'>
   <img class='peKbGallery' data-delay='{$slide->gallery_delay}' src='{$slide->gallery_image->url}' alt='{$slide->gallery_caption} (Click to enlarge)' />
   </a>";
    }
}
?>
  • Like 2
Link to comment
Share on other sites

Some PW template syntax to the table?


<? if(count($page->gallery)): ?>
   <h6>Images</h6>
   <? foreach($page->gallery as $slide): ?>
       <a rel='prettyPhoto[pp_gal]'
           title='<?= $slide->gallery_caption ?>'
           href='<?= $slide->gallery_image->url ?>'>
           <img class='peKbGallery'
               data-delay='<?= $slide->gallery_delay ?>'
               src='<?= $slide->gallery_image->url ?>'
               alt='<?= $slide->gallery_caption ?> (Click to enlarge)' />
       </a>";
   <? endforeach; ?>
<? endif; ?>
  • Like 1
Link to comment
Share on other sites

if(count($page->gallery_image)) didn't work, I'm guessing gallery_image is not a repeater field? Try this to see what you get:

echo "<pre>gallery_image is: "; 
if(is_object($page->gallery_image)) {
 echo "object of type " . get_class($page->gallery_image); 
 if(WireArray::iterable($page->gallery_image)) {
   echo " with " . count($page->gallery_image) . " items";
 }
} else {
 var_dump($page->gallery_image); 
}
echo "</pre>";
Link to comment
Share on other sites

Can you explain what you mean by

Quote

Some PW template syntax to the table?

Are you referring to this? http://processwire.c...why-php-syntax/

Yes, but I think Soma is reporting to this post from Apeisa http://processwire.c...ine/#entry12629 :)

I tried to answer without modifying your code too much, but I admit it came out kind of ugly. Soma's code is much better ;)

edit: also, if someone has troubles to understand these <? if(count($page->gallery)): ?> , <? endif; ?>, just have a look here http://php.net/manua...tive-syntax.php

  • Like 1
Link to comment
Share on other sites

gallery_image is a field within a repeater which is populated. That code returns gallery_image is: NULL.

mizcdl.png

Also, I used a tinymce textarea to enable multiple youtube video embeds, best way to do that you think? I couldn't think how else to do it, but it does rely on them pasting each video link in a separate paragraph...

Link to comment
Share on other sites

I think in this case a repeatable would be more friendly

I agree, but with what fieldtype? URL? Then how to embed the video? They would have to copy the embed URL with the correct size from youtube. It would be good if the youtube video embed module worked with URL field

Link to comment
Share on other sites

I agree, but with what fieldtype?

You have many options, can even be a text field. If you ask for the entire URL, You just have to output like this:

<iframe width="420" height="315" src="<?=$repeater->video?>"></iframe>

If you ask for the video ID, do this instead:

<iframe width="420" height="315" src="https://www.youtube.com/embed/<?=$repeater->video?>"></iframe>

There is also a module for this http://modules.proce...er-video-embed/

  • Like 1
Link to comment
Share on other sites

Never used it myself, but since it's a text formatter module it should work also in any text field. On the field options, go to the "details" tab, and add it from the text formatters dropdown.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...