$tip = new TipsAndTricks(2, 'New SugarFields In Studio');

bsoremsugar —  March 16, 2009 — 4 Comments

In the last tip we showed how to add a new SugarField to the application. This time we’ll show you how to add that SugarField to Studio so administrators can add it as a custom field to layouts. It should be noted that we will be patching some core files. These same patches will be available in the next release of SugarCRM. As a side note empty() is getting rendered as emptyempty in this post. We are looking into the issue, but just make a mental note that if you see !emptyempty it should read just !empty.

Step 1:Patching Dynamic Fields

The first file we need to patch is modules/DynamicFields/FieldCases.php. This file is essentially a switch statement for Module Builder and Studio to retrieve the appropriate Sugar Field class. We’ll be changing to allow for supporting custom SugarFields.

Change the code inside the function get_widget from

default:
    $local_temp = new TemplateText(); break;

to

default:
	$file = false;
	//CHECK IF THERE IS A CUSTOM TEMPLATE
	if(file_exists('custom/modules/DynamicFields/templates/Fields/Template'. $type . '.php')){
		$file  =	'custom/modules/DynamicFields/templates/Fields/Template'. $type . '.php';
	//CHECK IF THERE IS A STANDARD TEMPLATE
	}else if(file_exists('modules/DynamicFields/templates/Fields/Template'. $type . '.php')){
		$file  =	'modules/DynamicFields/templates/Fields/Template'. $type . '.php';
	}
	//IF THERE WAS A CUSTOM OR STANDARD TEMPLATE USE IT
	if(!empty($file)){
		require_once($file);
		$class  = 'Template' . $type ;
		$customClass = 'Custom' . $class;
		//CHECK FOR A CUSTOM CLASS
		if(class_exists($customClass)){
			$local_temp = new $customClass();
		}else{
			//DEFAULT TO THE STANDARD CLASS NAME Template{$type}
			$local_temp = new $class();
		}
		break;
	}else{
		$local_temp = new TemplateText(); break;
	}

Step 2:Creating a YouTube Template Field

Now our code can load custom templates, so let’s create our custom YouTube template. We want it to behave like a normal text field, but when it’s creating it’s vardef it should have the type set to YouTube. To do this we create a new template in custom/modules/DynamicFields/templates/Fields/TemplateYouTube.php and we’ll create a TemplateYouTube class that extends TemplateField. Our code should look something like this

default) ? $this->default : $this->default_value;

		//LET'S SET THE MAX LENGTH
		$def['len'] = 50;

		//STILL HAVE THE DB THINK OF THE FIELD AS A varchar
		$def['dbType'] = 'varchar';

		return $def;
	}
}

Step 3:Adding YouTube to the custom field Drop Down

Now that we have a template class for our YouTube field we need it to actually show up in Studio and Module Builder. To do this we simply create a file custom/modules/ModuleBuilder/language/en_us.lang.php and put the following code in it

<?php
	$mod_strings['fieldTypes']['YouTube'] = 'YouTube';

You will need to delete the cached language file for Module Builder and Studio which is located in cache/modules/ModuleBuilder/langague/en_us.lang.php
Once you have done this you should be able to go to studio and add a custom field of type YouTube. Now your administrators can take advantage of all the new SugarFields you create.

4 responses to $tip = new TipsAndTricks(2, 'New SugarFields In Studio');

  1. 
    Jason Eggers (eggsurplus) March 17, 2009 at 3:29 pm

    I’ve requested this exact functionality (#25780) but it was rejected: http://www.sugarcrm.com/crm/index.php?option=com_sugarbugs&task=view&caseID=87d6d3fe-9757-9a4f-a042-4908a0d338d6

    This would be very, very helpful!

    Thanks for posting this solution Majed

  2. 
    Jason Eggers (eggsurplus) March 17, 2009 at 7:29 am

    I’ve requested this exact functionality (#25780) but it was rejected: http://www.sugarcrm.com/crm/index.php?option=com_sugarbugs&task=view&caseID=87d6d3fe-9757-9a4f-a042-4908a0d338d6

    This would be very, very helpful!

    Thanks for posting this solution Majed

  3. 

    Hey Jason,

    Not sure why they rejected that feature. Wish I had seen it sooner would have added it before. I’ll look into what happened. If you have any other ideas you would like to see implemented for easier customizations, just let me know.

    Thanks,
    Majed

  4. 

    Hey Jason,

    Not sure why they rejected that feature. Wish I had seen it sooner would have added it before. I’ll look into what happened. If you have any other ideas you would like to see implemented for easier customizations, just let me know.

    Thanks,
    Majed

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s