CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • How to reference a custom field in a custom template
Pages: [1]

Author Topic: How to reference a custom field in a custom template  (Read 1583 times)

mdavid

  • I post occasionally
  • **
  • Posts: 31
  • Karma: 0
  • CiviCRM version: 4.2
  • CMS version: Drupal 7.15
  • MySQL version: 5.1.63
  • PHP version: 5.2.17
How to reference a custom field in a custom template
November 30, 2010, 12:17:23 am
What is the proper way to reference a custom field in a custom template? My custom template for Individual.tpl is working great except when I try to add a custom field.

I'm using the custom field ID (custom_23_-1) I found in the Smarty Debug Console and following the existing Smarty code in the template as a pattern.

When I include the following in the Individual.tpl:

{$form.custom_23_-1.label}
{$form.custom_23_-1.html}

I get an error message that says:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/mysite/.../templates_c/en_US/en_US/...Individual.tpl.php on line 124

And when I use:
{$form.custom_23.label}
{$form.custom_23.html}

there is no error message but the field does not display on the form.

I have cleaned the templates_c directory several times and it doesn't change the result.

So how should a custom field be referenced in a custom template?

Thanks.

Michał Mach

  • Ask me questions
  • ****
  • Posts: 748
  • Karma: 59
    • CiviCRM site
  • CiviCRM version: latest
  • CMS version: Drupal and Joomla latest
  • MySQL version: numerous
  • PHP version: 5.3 and 5.2
Re: How to reference a custom field in a custom template
November 30, 2010, 04:17:48 am
Hey,

Not sure what's the deal with Smarty error yet, but a quick workaround that I just verified working is:

{assign var=blah value=custom_1_-1}
{$form.$blah.html} {$form.$blah.label}

Thx,
m
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

My absolute favourite: Wordpress Integration!.

Donate Now!

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: How to reference a custom field in a custom template
November 30, 2010, 05:04:29 am
I have used custom fields in a template, but used the $viewCustomData array. That was in summary.tpl though, I am not sure what you are getting in the Individual.tpl. But you could try to retrieve them with an API from the template? (read http://en.flossmanuals.net/CiviCRM/DevelopAPI)
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

mdavid

  • I post occasionally
  • **
  • Posts: 31
  • Karma: 0
  • CiviCRM version: 4.2
  • CMS version: Drupal 7.15
  • MySQL version: 5.1.63
  • PHP version: 5.2.17
Re: How to reference a custom field in a custom template
December 01, 2010, 12:28:58 am
Thanks for the suggestions!

Michal - I was able to get the custom field to display using your suggestion. The code I'm using is:
  {assign var="pnletters" value="custom_23_-1"}
  {$form.$pnletters.label}<br />
  {$form.$pnletters.html}

However, it only displays if the field is empty. Once the custom field contains data, it no longer displays. I don't know why. If you have a thought about this, I'd be glad to hear it!

Erik - Thanks for suggesting using the API. I'm reading the page you referenced and working on figuring out how to apply it to my project.

Thank you both!

Michał Mach

  • Ask me questions
  • ****
  • Posts: 748
  • Karma: 59
    • CiviCRM site
  • CiviCRM version: latest
  • CMS version: Drupal and Joomla latest
  • MySQL version: numerous
  • PHP version: 5.3 and 5.2
Re: How to reference a custom field in a custom template
December 01, 2010, 05:13:38 am
Quote from: mdavid on December 01, 2010, 12:28:58 am
However, it only displays if the field is empty. Once the custom field contains data, it no longer displays. I don't know why. If you have a thought about this, I'd be glad to hear it!

That's why this field is named a bit differently in create and update modes. In create mode, the name of the field is custom_23_-1, in update mode it will be called something like custom_23_XX, where XX is the id of custom_value_sth_sth_23 table record. First thing, you need to introduce condition - if action is create, display custom_23_-1, if update, display something starting with custom_23. Not sure how to do the latter in smarty.

Thx,
m
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

My absolute favourite: Wordpress Integration!.

Donate Now!

mdavid

  • I post occasionally
  • **
  • Posts: 31
  • Karma: 0
  • CiviCRM version: 4.2
  • CMS version: Drupal 7.15
  • MySQL version: 5.1.63
  • PHP version: 5.2.17
Re: How to reference a custom field in a custom template
December 03, 2010, 11:04:29 pm
Thanks, Michal. After several days and many hours of reading, testing, trial and error, I still haven't figured this out.

Your explanation of the difference between create and update modes is very helpful. I appreciate it! I just haven't figured out the smarty code yet.

Using the API as a Smarty function appears to be for display purposes only, if I understand the documentation correctly. It says: "method is get, search, search_count (it shouldn't be a method that modify the entity, only to fetch data)."

I'm trying to get a custom field to display and be editable in the Contact Details area, right after the last name and suffix, instead of in a separate block.

Anyway, I wanted to say thanks for your help; you've pointed me in good directions. If I figure it out, I'll post it here.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: How to reference a custom field in a custom template
December 04, 2010, 02:15:42 am
For the Indivdual, there is a contact_get API, but also a contact_create and a contact_update. You should be able to use the contact_update to update a custom field? Check this page : http://wiki.civicrm.org/confluence/display/CRMDOC33/Contact+APIs
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Michał Mach

  • Ask me questions
  • ****
  • Posts: 748
  • Karma: 59
    • CiviCRM site
  • CiviCRM version: latest
  • CMS version: Drupal and Joomla latest
  • MySQL version: numerous
  • PHP version: 5.3 and 5.2
Re: How to reference a custom field in a custom template
December 06, 2010, 05:08:38 am
I kind of agree with Erik that API might be the best way to go. We are in a little swing those days about how custom fields should be used in custom templates - we'll probably look into it within next 1-2 releases.

Thx,
m
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

My absolute favourite: Wordpress Integration!.

Donate Now!

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: How to reference a custom field in a custom template
December 06, 2010, 07:31:30 am
Hi,

Quote from: mdavid on December 03, 2010, 11:04:29 pm
I'm trying to get a custom field to display and be editable in the Contact Details area, right after the last name and suffix, instead of in a separate block.

Yeap, the way the custom data fieds change names between the update and create (and that's a bit more complex than that, at least 3 or 4 different names).

Have you tried to use jquery to move the fields you want out of the separate box ? Bit of a hack, but if it's only a cosmetic issue, might be faster to code.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • How to reference a custom field in a custom template

This forum was archived on 2017-11-26.