At first glance, the reasons why someone would want to do this are not always readily apparent. However, consider the scenario where one uses a text area field (e.g. description) on a module and one wishes to display that field on the subpanel.
The process of including the field on the subpanel is easily accomplished via Studio, but over time, an issue not initially apparent usually starts to surface.
Because a text area field can hold large amounts of text, it is possible for a user to populate the field with a number of pages worth of data. That on its own is not a problem. However, displaying that on a ListView, of which a subpanel is a good example, usually causes undesired display problems, as demonstrated by the image that follows (click to enlarge):
The issue at hand is that the Description field with the longer text value makes the subpanel appear a bit odd, to the point that the additional columns that are part of the view appear to the extreme right of the screen.
So how do we solve this? Easily, via a logic hook. Let us take a look at how we do so.
First of all, lets talk a little bit more about our example from the previous image.
The image illustrates the default Member Organizations subpanel one finds on the DetailView of an Account record. Thus, the description field in question in this example belongs to the child Account records associated with the current Account.
In addition, this bit of knowledge also tells us that the logic hook we need to build will be for the Accounts module, thus, the related files will reside in the folder <sugar>/custom/modules/Accounts.
Let us build the files.
The first file is the logic hook definition file (logic_hooks.php):
<?php
$hook_version = 1;
$hook_array = Array();
$hook_array['process_record'] = Array();
$hook_array['process_record'][] = Array(1, 'DescTruncate', 'custom/modules/Accounts/DisplayUtils.php','DisplayUtils', 'truncateDesc');
?>
<?php
/*************************************
Project: Logic Hooks for Truncating Displayed Value
Original Dev: Angel Magaña, July 2011
@2009-2011 Angel Magaña
cheleguanaco[at]cheleguanaco.com
Desc: Logic Hooks for Truncating Displayed Value
The contents of this file are governed by the GNU General Public License (GPL).
A copy of said license is available here: http://www.gnu.org/copyleft/gpl.html
This code is provided AS IS and WITHOUT WARRANTY OF ANY KIND.
*************************************/
class DisplayUtils {
function truncateDesc (&$bean, $event, $arguments)
{
$bean->description = substr($bean->description, 0, 100);
}
}
?>
Assuming everything is fine, the subpanel should now be nicer and easier to read, as demonstrated in the image (click to enlarge):
Via this technique, you are at liberty to manipulate the displayed data in a number of different ways.
Hi, I am using your code without success
ReplyDeleteHi Angel, thank you for this code, it works fine, I was missed in a wrong module but I change the path and now it is all rigth.
ReplyDeleteThanks,
Wilson
This is exactly what I was looking for. Works great. Muchas gracias Angel.
ReplyDelete