CiviCRM Community Forums (archive)
Old sections (read-only, deprecated) => Developer Discussion => Topic started by: lcdweb on July 25, 2009, 05:38:23 pm
-
I've got a profile search form that I'm putting together. The detail view needs to be custom laid out, which means I'm referencing each variable individually, instead of through the foreach iteration.
However, the $row array assigns the label value to the key -- and because many of the labels have spaces, the key/value can't be referenced (smarty won't let you reference a key containing spaces). Which basically means I can't reference fields individually.
I'm playing with reassigning the values so they can be referenced. But I think this should be changed in future releases, as it significantly impacts customizeability.
-B
-
Brian - If you can recommend a "simple" change to the tpl for 3.0, let me know and I'll look at putting it in.
-
Well, you know I'm a bit of a hack. So this is pretty ungraceful --
templates/CRM/Profile/Page/View.tpl
at the top I've added:
{foreach from=$row item=value key=rowName name=profile}
{assign var=field value='f'}
{assign var=label value='l'}
{assign var=fieldname value=$rowName|replace:' ':'_'}
{assign var=$label|cat:$fieldname value=$rowName}
{assign var=$field|cat:$fieldname value=$value}
{/foreach}
That gives me a label/field pair for each field, with the format f[name of field with spaces replaced by underscore] and l[same]. So I can reference the labels and fields for each field individually now.
I'm not sure you'd want to add to the tpl file for the release though. Because the variable assignment happens in the tpl file, the list of variables doesn't show up in the smarty debug window. So people won't know they're available unless they look at the tpl file. But I guess those looking to reference variables individually will be looking at the tpl file anyway. For now, it's probably sufficient to just have a solution available on the forums.
Here's my draft modified directory layout, in case you're interested. Still in progress. :)
http://www.nytransit.org/index.php?option=com_civicrm&task=civicrm/profile/view&reset=1&id=704&gid=3
(this site was my model for the book case study)
-
here is what i did last nite when i wanted to do something similar. I knew the fields in the profile which i wanted to layout
<table class="form-layout-compressed">
{assign var=fName value="First Name"}
{assign var=lName value="Last Name"}
<tr id="contact_name"><td class="label">Name</td><td class="view-value">{$row.$fName} {$row.$lName}</td></tr>
<tr id="contact_email"><td class="label">Email</td><td class="view-value">{$row.Email}</td></tr>
<tr id="contact_phone"><td class="label">Phone</td><td class="view-value">{$row.Phone}</td></tr>
</table>
lobo
-
Brian - this seemed like an easy "win" for site designers, so I filed and implemented an issue for 3.0 to make this easy:
http://issues.civicrm.org/jira/browse/CRM-4805
With this change... if you're creating want a custom profile view template, you can access field labels and values in $profileFields_N array - where N is profile ID.
EXAMPLE - to access last_name field for profile 1:
{$profileFields_1.last_name.label}
{$profileFields_1.last_name.value}
Would be great if you could try this out (if you have a trunk svn running) OR backport the change in Dynamic.php - should work fine.
To test a backport (caveat - i didn't do this).... insert these lines after line 146 in CRM/Profile/Page/Dynamic.php
// $profileFields_$gid array can be used for customized display of field labels and values in Profile/View.tpl
$profileFields = array( );
$labels = array( );
foreach ( $fields as $name => $field ) {
$labels[$field['title']] = $name;
}
foreach ( $values as $title => $value ) {
$profileFields[$labels[$title]] = array( 'label' => $title,
'value' => $value );
}
$template->assign_by_ref( 'profileFields_' . $this->_gid, $profileFields );
Then assuming it works as you'd hoped, would be awesome if you could add some doc for this in the 3.0 customizing profiles section:
http://wiki.civicrm.org/confluence/display/CRMUPCOMING/Customize+Built-in%2C+Profile%2C+Contribution+and+Event+Registration+Screens
-
Dave,
I backported to v2.2 and it works great -- and it's much easier to reference the available fields through smarty debug with this method. I'll modify the wiki. thanks!
-B
-
Dave,
I'm not sure why, but Smarty didn't like dashes in the array key. This affected the phone and address fields (they contain -Primary). So I altered to replace the dash with an underscore:
// $profileFields_$gid array can be used for customized display of field labels and values in Profile/View.tpl
$profileFields = array( );
$labels = array( );
foreach ( $fields as $name => $field ) {
$labels[$field['title']] = str_replace("-", "_", $name);
}
foreach ( $values as $title => $value ) {
$profileFields[$labels[$title]] = array( 'label' => $title,
'value' => $value );
}
$template->assign_by_ref( 'profileFields_' . $this->_gid, $profileFields );
-
just to be safe, u might want to change that to:
$labels[$field['title']] = preg_replace('/\s+|\W+/', '_', $name);
this replaces all non alpha-numeric characters with a _
lobo
-
Great -- thx.
(aren't you on a plane right now?)
-
plane delayed a bit. wonders of internet connectivity everywhere :)
lobo
-
I've committed lobo's recommended fix to trunk for next alpha release.