Jump to content

Do something if page field is not empty


joe_ma
 Share

Recommended Posts

Hello

I have a template with a page reference field for categories. Now I'd like to list content from all the pages that have the same categories. My code looks like this:

// list all addresses that have rubrik = adr_rubrik.
$out = "";

if($page->fields->adr_rubrik) {	
	$rubrik = $page->adr_rubrik;

		$out .= '<table id="myTable" class="tablesorter adressliste display">
	<thead>
	<tr>
		<th class="inst">Institution</th>
		<th class="plz">PLZ</th>
		<th class="ort">Ort</th>
		<th class="inst_info">Beschreibung</th>
	</tr>
    </thead>
    <tbody>';
	foreach($rubrik as $r) {
	
	$list = $pages->find("template=adress_daten, adr_rubrik=$r, include=all");
		foreach($list as $l) {
		$out .= "<tr><td>";
			
			if($l->adr_firma) $out .= "$l->adr_firma <br>";
			if($l->adr_abteilung) $out .= "$l->adr_abteilung <br>";
			if($l->adr_vorname) $out .= "$l->adr_vorname <br>";
			if($l->adr_name) $out .= "$l->adr_name <br>";
			if($l->adr_strasse) $out .= "$l->adr_strasse  <br>";
			if($l->adr_postfach) $out .= "$l->adr_postfach <br>";
			if($l->adr_tel) $out .= "$l->adr_tel <br>";
			if($l->adr_fax) $out .= "$l->adr_fax <br>";
			if($l->adr_email) $out .= "$l->adr_email <br>";
			if($l->adr_web) $out .= "<a target='_blank' href='$l->adr_web'>website</a> <br>";
			if($l->adr_webzusatz) $out .= "<a target='_blank' href='$l->adr_webzusatz'>zusätzliche Website</a>";
		$out .= "</td><td>"; $out .= $l->adr_plz; $out .= "</td>";
		$out .= "<td>"; $out .= $l->adr_ort; $out .= "</td>";
		$out .= "<td>"; $out .= $l->adr_beschreibung; $out .= "</td></tr>";
		
		}
	}
		$out .= '</tbody></table>';
		$content .= $out;
}

So far everything works fine.

Except when the page reference field (adr_rubrik) has no pages selected. Then the output is a table without rows, i.e. the table head and end.

What I like to have is, that nothing at all is being output if the field adr_rubrik is empty.

Thanks for help.

Link to comment
Share on other sites

I think diogo fell in the copy/paste trap here. You need to check the count of the field instance of the page. $page->fields->… will get you the field's properties, but not actual data.

if($page->adr_rubrik->count()){ // just syntax sugar for: count($page->adr_rubrik)
  …
}
  • Like 1
Link to comment
Share on other sites

Hm, I don't think so. At the moment the code looks like this.

  if($page->adr_rubrik->count()){	
	$rubrik = $page->adr_rubrik;
	
		$out .= '<table id="myTable" class="tablesorter adressliste display">
	<thead>
	<tr>
		<th class="inst">Institution</th>
		<th class="plz">PLZ</th>
		<th class="ort">Ort</th>
		<th class="inst_info">Beschreibung</th>
	</tr>
    </thead>
    <tbody>';
	foreach($rubrik as $r) {
	
	$list = $pages->find("template=adress_daten, adr_rubrik=$r, include=all");
		foreach($list as $l) {
		$out .= "<tr><td>";

I think the line in question IS above the table. Isn't it?

Link to comment
Share on other sites

var_dump($rubrik) where field is not empty:

object(PageArray)#352 (7) { ["hooks"]=> array(2) { ["PageArray::render"]=> string(60) 
"MarkupPageArray->renderPageArray() in MarkupPageArray.module" ["PageArray::renderPager"]=> string(56) 
"MarkupPageArray->renderPager() in MarkupPageArray.module" } ["count"]=> int(1) ["items"]=> array(1) 
{ [0]=> string(31) "/de/adressen/rubriken/religion/" } 
["total"]=> int(1) ["start"]=> int(0) ["limit"]=> int(0) ["selectors"]=> string(0) "" } 

var_dump($rubrik) where field is empty:

object(PageArray)#352 (7) { ["hooks"]=> array(2) { ["PageArray::render"]=> string(60) 
"MarkupPageArray->renderPageArray() in MarkupPageArray.module" ["PageArray::renderPager"]=> string(56) 
"MarkupPageArray->renderPager() in MarkupPageArray.module" } ["count"]=> int(1) ["items"]=> array(1) 
{ [0]=> string(30) "/de/adressen/rubriken/vereine/" } 
["total"]=> int(1) ["start"]=> int(0) ["limit"]=> int(0) ["selectors"]=> string(0) "" } 

var_dump($list) where field is not empty:

object(PageArray)#363 (7) { ["hooks"]=> array(2) { ["PageArray::render"]=> string(60) 
"MarkupPageArray->renderPageArray() in MarkupPageArray.module" 
["PageArray::renderPager"]=> string(56) "MarkupPageArray->renderPager() in MarkupPageArray.module" } 
["count"]=> int(3) ["items"]=> array(3) { [0]=> string(44) "/de/adressen/adressdaten/familientreff-bern/" 
[1]=> string(50) "/de/adressen/adressdaten/muetterzentrum-bern-west/" [2]=> string(45) 
"/de/adressen/adressdaten/stiftung-idee-sport/" } ["total"]=> int(3) ["start"]=> int(0) 
["limit"]=> int(0) ["selectors"]=> string(38) "template=adress_daten, adr_rubrik=1273" } 
object(PageArray)#363 (7) { ["hooks"]=> array(2) { ["PageArray::render"]=> string(60) 
"MarkupPageArray->renderPageArray() in MarkupPageArray.module" 
["PageArray::renderPager"]=> string(56) "MarkupPageArray->renderPager() in MarkupPageArray.module" } 
["count"]=> int(3) ["items"]=> array(3) { [0]=> string(44) "/de/adressen/adressdaten/familientreff-bern/"
[1]=> string(50) "/de/adressen/adressdaten/muetterzentrum-bern-west/" 
[2]=> string(45) "/de/adressen/adressdaten/stiftung-idee-sport/" } 
["total"]=> int(3) ["start"]=> int(0) ["limit"]=> int(0) ["selectors"]=> string(38) 
"template=adress_daten, adr_rubrik=1273" } object(PageArray)#363 (7) { ["hooks"]=> array(2) 
{ ["PageArray::render"]=> string(60) "MarkupPageArray->renderPageArray() in MarkupPageArray.module" 
["PageArray::renderPager"]=> string(56) "MarkupPageArray->renderPager() in MarkupPageArray.module" } 
["count"]=> int(3) ["items"]=> array(3) { [0]=> string(44) "/de/adressen/adressdaten/familientreff-bern/" 
[1]=> string(50) "/de/adressen/adressdaten/muetterzentrum-bern-west/" [2]=> string(45)
"/de/adressen/adressdaten/stiftung-idee-sport/" } ["total"]=> int(3)
["start"]=> int(0) ["limit"]=> int(0) ["selectors"]=> string(38) "template=adress_daten, adr_rubrik=1273" }
 

var_dump($list) where field is empty:

no output.

Link to comment
Share on other sites

You've a complicated setup :-),

the below is written in the browser.

<?php

$out = '';
$rubrik = false;

if ($page->template->has('adr_rubrik')) {
    $rubrik = $page->adr_rubrik;
}

if ($rubik && count($rubik)) {

    $list = $pages->find("template=adress_daten, adr_rubrik=$rubik, include=all");

    if (count($list)) {
        $out .= '<table id="myTable" class="tablesorter adressliste display">';
        // ..... etc. etc.
        
        foreach($list as $l) {
            // ..... etc. etc.
        }
    
    }
}
Link to comment
Share on other sites

You've a complicated setup :-),

Doesn't really surprise me … :(

The adr_rubrik field allows more than one category to be selected. Therefore I thought, I had to iterate through these as well.

I tried to adopt your code. It now looks like this:

$out = '';
$rubrik = false;

if ($page->template->has('adr_rubrik')) {
    $rubrik = $page->adr_rubrik;
}

if ($rubrik && count($rubrik)) {
    $list = $pages->find("template=adress_daten, adr_rubrik=$rubrik, include=all");
    if (count($list)) {
        $out .= '<table id="myTable" class="tablesorter adressliste display">
            <thead>
                <tr>
                    <th class="inst">Institution</th>
                    <th class="plz">PLZ</th>
                    <th class="ort">Ort</th>
                    <th class="inst_info">Beschreibung</th>
                </tr>
            </thead>
            <tbody>';
        
        foreach($list as $l) {
            $out .= "<tr><td>";
            
            if($l->adr_firma) $out .= "$l->adr_firma <br>";
            if($l->adr_abteilung) $out .= "$l->adr_abteilung <br>";
            if($l->adr_vorname) $out .= "$l->adr_vorname <br>";
            if($l->adr_name) $out .= "$l->adr_name <br>";
            if($l->adr_strasse) $out .= "$l->adr_strasse  <br>";
            if($l->adr_postfach) $out .= "$l->adr_postfach <br>";
            if($l->adr_tel) $out .= "$l->adr_tel <br>";
            if($l->adr_fax) $out .= "$l->adr_fax <br>";
            if($l->adr_email) $out .= "$l->adr_email <br>";
            if($l->adr_web) $out .= "<a target='_blank' href='$l->adr_web'>website</a> <br>";
            if($l->adr_webzusatz) $out .= "<a target='_blank' href='$l->adr_webzusatz'>zusätzliche Website</a>";
            $out .= "</td><td>"; $out .= $l->adr_plz; $out .= "</td>";
            $out .= "<td>"; $out .= $l->adr_ort; $out .= "</td>";
            $out .= "<td>"; $out .= $l->adr_beschreibung; $out .= "</td></tr>";
        
        }
    
    }
        $out .= '</tbody></table>';
        $content .= $out;
}

This leads to nothing being output in both cases.

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...