Solution to show icon by using FilterBy

Hej again,

lets say, I got following field:
Food-type: Pizza

Now I want to display a Pizza icon when I have such text like in field above. First what I thought what should work (but it doesn’t) is:

<?php if ($receipeCard->filterby("food_type", "==", "Pizza")): ?>
 <i class="icon-pizza"></i> <?php endif; ?>

How to do that corect way? :slight_smile:

This should work. But as it doesn’t, it’s hard to say why not if we don’t have more context.

What is in $receipeCard?

Edit:
PS: I always try to avoid “-” or similar in field names.

Hi Bart!

Here is whole codeblock, everything works except that filterby for icon

<?php foreach($receipeList as $receipeCard): ?>
	<div class="receipeList">
	
			<div class="list-cell w10p">
				<?php if ($receipeCard->filterby("foodType", "==", "Pizza")): ?>
				<div class="icon pizza">Here comes the icon</div>
				<?php endif; ?>
			</div>
	
			<div class="list-cell w30p"><h2><?= $receipeCard->title()->html() ?></h2></div>
	
			<?php if ($receipeCard->description()->isNotEmpty()): ?>
				<div class="list-cell w30p description"><?= $receipeCard->description()->html() ?></div>
			<?php else: ?>
				<div class="list-cell w30p description">–</div>
			<?php endif; ?>

			<?php if ($receipeCard->receipeFile()->isNotEmpty()): ?>
				<div class="list-cell w20p"><a href="<?= $receipeCard->url() ?>/<?= $receipeCard->receipeFile() ?>" > Herunterladen</a></div>
			<?php endif; ?>
	<
	/div>
<?php endforeach ?>

Can you post the output of 1 var_dump($receipeCard); which should match the “Pizza” filter here please?

tbh - I don’t know how to do that - I’m rather beginner (Frontend developer) learning to do backend :pensive:

edit: when I use this

<?= $receipeCard->foodType()->html() ?>

it does show “Pizza” as text.

Immediately after this:

put this:

<?php var_dump($receipeCard); die; ?>

And paste the output from that here :slight_smile:

OK this is weird, here is full code (I used Pizza analogy as simplified example), here is full thing:

   <?php $odinCards = $page->children()->sortBy('title', 'desc') ?>
<?php foreach($odinCards as $odinCard): ?>
	<div class="odinInfo list">
			<div class="list-cell w10p"><?= $odinCard->date('d.m.Y')  ?></div>
			<div class="list-cell w10p">
				<?= $odinCard->odinType()->html() ?>
			</div>
			<div class="list-cell w30p"><h2><?= $odinCard->title()->html() ?></h2></div>
			<?php if ($odinCard->description()->isNotEmpty()): ?>
				<div class="list-cell w30p description"><?= $odinCard->description()->html() ?></div>
			<?php else: ?>
				<div class="list-cell w30p description">–</div>
			<?php endif; ?>
			<?php if ($odinCard->odinFile()->isNotEmpty()): ?>
				<div class="list-cell w20p"><a href="<?= $odinCard->url() ?>/<?= $odinCard->odinFile() ?>" > Herunterladen</a></div>
			<?php endif; ?>
	</div>
<?php endforeach ?>

The result is this screenshot:

Now I would like to have an icon istead for text in column “Typ”, that is where I tried the filterby above
<?php if ($odinCard->filterBy('odinType', "==", 'Arbeitshilfe')): ?><div class="icon ah">AH</div><?php endif; ?>

But as I described it doesn’t work, now here is dump output - there i cannt find the odinType… weird

object(Page)#108 (30) { ["title"]=> string(33) "Zeichnungsberechtigungen Bayreuth" ["id"]=> string(19) "datenbank/135-018-d" ["uid"]=> string(9) "135-018-d" ["slug"]=> string(9) "135-018-d" ["parent"]=> string(9) "datenbank" ["uri"]=> string(19) "datenbank/135-018-d" ["url"]=> string(52) "http://192.168.2.103:5757/odin/kirby/datenbank/135-018-d" ["contentUrl"]=> string(64) "http://192.168.2.103:5757/odin/kirby/content/2-datenbank/1-135-018-d" ["tinyUrl"]=> string(42) "http://192.168.2.103:5757/odin/kirby/x/1ibx1qd" ["root"]=> string(77) "/Users/user/OneDrive/Lokalserver/odin/kirby/content/2-datenbank/1-135-018-d" ["dirname"]=> string(11) "1-135-018-d" ["diruri"]=> string(23) "2-datenbank/1-135-018-d" ["depth"]=> int(2) ["num"]=> string(1) "1" ["hash"]=> string(7) "1ibx1qd" ["modified"]=> string(25) "2018-10-01T14:12:53+00:00" ["template"]=> string(8) "odinfile" ["intendedTemplate"]=> string(8) "odinfile" ["isVisible"]=> bool(true) ["isOpen"]=> bool(false) ["isActive"]=> bool(false) ["isHomePage"]=> bool(false) ["isErrorPage"]=> bool(false) ["isCachable"]=> bool(true) ["isWritable"]=> bool(true) ["content"]=> object(Content)#124 (2) { ["root"]=> string(90) "/Users/user/OneDrive/Lokalserver/odin/kirby/content/2-datenbank/1-135-018-d/odinfile.txt" ["fields"]=> array(13) { ["title"]=> string(33) "Zeichnungsberechtigungen Bayreuth" ["coverimage"]=> string(0) "" ["subheader"]=> string(0) "" ["description"]=> string(26) "Das ist ein Testkommentar." ["code"]=> string(9) "135-018-D" ["index"]=> string(3) "ixz" ["tags"]=> string(28) "beratung,verfahrensanweisung" ["date"]=> string(10) "2018-09-30" ["odintype"]=> string(8) "Formular" ["odin_file"]=> string(14) "135-018-d.docx" ["odin_code"]=> string(9) "135-018-D" ["odincode"]=> string(9) "135-018-D" ["odinfile"]=> string(14) "135-018-d.docx" } } ["headers"]=> NULL ["children"]=> object(Children)#163 (0) { } ["siblings"]=> object(Children)#160 (2) { [0]=> string(19) "datenbank/136-018-a" [1]=> string(28) "datenbank/urlaubsantrag-2009" } ["files"]=> object(Files)#164 (1) { [0]=> string(14) "135-018-d.docx" } }

:no_mouth:

You can’t filter a single page object. Just compare the field. value.

if($odinCard->odinType() === 'xyz') {
  //. do stuff
}

unfortunately it doesn’t work, maybe I wrote it wrong

<?php if($odinCard->odinType() === 'Arbeitshilfe'): ?><div class="icon ah">AH</div><?php endif; ?>

Try with. two ==only (because the type is not the same, actually)

<?php if($odinCard->odinType() == 'Arbeitshilfe'): ?>

The filterBy() method only works with collection objects, not with a single page object.

That worked! Thank you for your time!

Yeah, to expand on what texnixe said, $odinCard->odinType() returns a Field object. If you want the value, use $odinCard->odinType()->value().

The reason it works without the value() is the magic __toString() function, but I don’t like to rely on this and would rather be explicit.

1 Like