1

Drupal 7.78 Civi 5.28.2

I am setting up a custom contact dashboard following Demerit's answer using UserDashBoard.extra.tpl and smarty's crmAPI.

But I only want it to be for contact sub-type = Student. How would I write an IF statement for that?

Here is my best guess but it does not work.

    {crmAPI var='result' entity='Contact' action='get' id=$contactId sequential=0}
{foreach from=$result.values item=contact}
  {if $contact.contact_sub_type = 'Student'}

<table class="dashboard-custom"><tr><td><strong>Student Demographics</strong></td></tr>
{crmAPI entity='Contact' action='get' var='result' id=$contactId return="birth_date,gender_id,custom_108,custom_107,custom_109,custom_110" sequential=0}
{foreach from=$result.values item=contact}
  <tr><th>Date of Birth</th><th>Gender</th><th>Student Status</th> <th>Marital Status</th><th>Anniversary</th><th># of Children</th></tr>
  <tr><td>{$contact.birth_date}</td>
  <td>{$contact.gender_id}</td>
  <td>{$contact.custom_108}</td>
  <td>{$contact.custom_107}</td>
  <td>{$contact.custom_110}</td>
  <td>{$contact.custom_109}</td></tr>
{/foreach}
<tr><td><strong>Class Info</strong></td></tr>
{crmAPI var='result' entity='Contact' action='get' id=$contactId return="custom_118,custom_112,custom_113,custom_111"}
{foreach from=$result.values item=contact}
<tr><th>Class of</th><th>Campus</th><th>CG Leader</th> <th>Met Spouse at RI</th></tr>
  <tr><td>{$contact.custom_118}</td>
  <td>{$contact.custom_112}</td>
  <td>{$contact.custom_113}</td>
  <td>{$contact.custom_111}</td></tr>
{/foreach}
</table>
{/if}
{/foreach}

Thanks

1

Might just be a typo but you need == instead of =.

{if $contact.contact_sub_type == 'Student'}

But contact_sub_type is an array, so you could foreach over it and check if one of the values is Student.

6
  • Thanks! That was not a typo--just the scribbles of a bumbling idiot giving it a shot in the dark. I added the double == but it still does not work for any logins. If I remove the IF statement it does work but for all contact types. – Iowa Boy Mar 16 at 22:14
  • Oh, it's because contact_sub_type is an array, so you could foreach thru it and check if one of the values is Student. – Demerit Mar 16 at 22:21
  • I am trying to just get to the contact_sub_type sub array, but not getting there. How do I get to the sub array? {crmAPI var='result' entity='Contact' action='get' id=$contactId sequential=0} {foreach from=$result.values item=contact name=outer_loop} Name: {$contact.display_name}</br> Contact Type: {$contact.contact_type}</br> Sub Contact Type: {foreach from=$contact.contact_sub_type item=sub name=inner_loop}{$sub}, {foreachelse}<i>Nothing to see here</i>{/foreach} {/foreach} – Iowa Boy Mar 17 at 2:02
  • 1
    Thanks!!! Duh, the $ContactId I was testing on did not have a sub_type so of course it was not turning up any results. Once I saw the debug was empty I realized my error. – Iowa Boy Mar 17 at 4:18
  • 1
    In case someone is looking for some code like this...{crmAPI var='result' entity='Contact' action='get' id=$contactId sequential=0} {debug} {foreach from=$result.values item=contact name=outer_loop} {foreach from=$contact.contact_sub_type item=sub name=inner_loop} {if $sub == 'Student'} {* Put in what you want to display for sub_types "Students" *} {/if} {/foreach} {/foreach} – Iowa Boy Mar 17 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.