Chapter 06

## 6. Metadata ## ### Intro ### Module metadata are used to describe how various views behave in the module. The main use of this is providing field and layout information but this can also be used to filter subpanels and to describe what fields are used in the search. ### Location ### Module metadata can be found in:
Example 6.1: Module metadata location -----
modules/<TheModule>/metadata/
-----
### Customising ### Usually studio is the best way of customising metadata. Even when you do wish to make customisations that are not possible through studio it can be simpler to set everything up in studio first. This is particularly true for layout based metadata. However if you are customising metadata it is as simple as placing, or editing, the file in the custom directory. For example to override the Accounts detailviewdefs (found in modules/Accounts/metadata/detailviewdefs.php) we would place (or edit) the file in custom/modules/Accounts/metadata/detailviewdefs.php. One exception to this rule is the studio.php file. The modules metadata folder is the only location checked - any version in custom/<TheModule>/metadata/studio.php is ignored. ### Different metadata ### #### detailviewdefs.php #### detailviewdefs.php provides information on the layout and fields of the detail view for this module. This file uses the same structure as editviewdefs.php. Let’s look at an example for a fictional module ABC_Vehicles:
Example 6.2: DetailView metadata definition -----
 1 <?php
 2 $viewdefs ['ABC_Vehicles'] ['DetailView'] = array (
 3 	'templateMeta' => array (
 4 		'form' => array (
 5 			'buttons' => array (
 6 				'EDIT',
 7 				'DUPLICATE',
 8 				'DELETE',
 9 				'FIND_DUPLICATES'
10 			)
11 		),
12 		'maxColumns' => '2',
13 		'widths' => array (
14 			array (
15 				'label' => '10',
16 				'field' => '30'
17 			),
18 			array (
19 				'label' => '10',
20 				'field' => '30'
21 			)
22 		),
23 		'includes' => array (
24 			array (
25 				'file' => 'modules/ABC_Vehicles/ABC_VehiclesDetail.js'
26 			)
27 		)
28 	),
29 	'panels' => array (
30 		'LBL_ABC_VEHICLES_INFO' => array (
31 			array (
32 				array (
33 					'name' => 'name',
34 					'comment' => 'The Name of the Vehicle',
35 					'label' => 'LBL_NAME',
36 				),
37 				'reg_number'
38 			),
39 			array (
40 				array (
41 					'name' => 'type',
42 					'label' => 'LBL_TYPE',
43 				),
44 				array (
45 					'name' => 'phone_fax',
46 					'comment' => 'The fax phone number of this company',
47 					'label' => 'LBL_FAX'
48 				)
49 			),
50 			array (
51 				array (
52 					'name' => 'registered_address_street',
53 					'label' => 'LBL_REGISTERED_ADDRESS',
54 					'type' => 'address',
55 					'displayParams' => array (
56 						'key' => 'registered'
57 					)
58 				),
59 			),
60 		),
61 		'LBL_PANEL_ADVANCED' => array (
62       array (
63 				array (
64 					'name' => 'assigned_user_name',
65 					'label' => 'LBL_ASSIGNED_TO'
66 				),
67 				array (
68 					'name' => 'date_modified',
69 					'label' => 'LBL_DATE_MODIFIED',
70 					'customCode' => '{$fields.date_modified.value} '
71 							+ '{$APP.LBL_BY} '
72 							+ '{$fields.modified_by_name.value}',
73 				)
74 			),
75 		),
76 	)
77 );
78 ?>
-----
We see that line 2 defines an array $viewdefs['ABC_Vehicles']['DetailView'] which places a DetailView entry for the module ABC_Vehicles into $viewdefs (DetailView will be EditView or QuickCreateView as appropriate). This array has two main keys defined here: ####= templateMeta ####= The templateMeta key provides information about the view in general. The ['form']['buttons'] entries define the buttons that should appear in this view. ; maxColumns : Defines the number of columns to use for this view. It is unusual for this to be more than 2. ; widths : An array defining the width of the label and field for each column. ; includes : An array of additional JavaScript files to include. This is useful for adding custom JavaScript behaviour to the page. ####= panels ####= The panels entry defines the actual layout of the Detail (or Edit) view. Each entry is a new panel in the view with the key being the label for that panel. We can see in our example that we have 2 panels. One uses the label defined by the language string LBL_ABC_VEHICLES_INFO, the other uses LBL_PANEL_ADVANCED. Each panel has an array entry for each row, with each array containing an entry for each column. For example we can see that the first row has the following definition:
Example 6.3: DetailView metadata row definition -----
31 array(
32 	array (
33 		'name' => 'name',
34 		'comment' => 'The Name of the Vehicle',
35 		'label' => 'LBL_NAME',
36 	),
37 	'reg_number',
38 ),
-----
This has an array definition for the first row, first column and a string definition for the first row, second column. The string definition is very straightforward and simply displays the detail (or edit, as appropriate) view for that field. It will use the default label, type, etc. In our example we are displaying the field named reg_number. The array definition for the first row, first column is a little more complex. Each array definition must have a name value. In our example we are displaying the name field. However we also supply some other values. Values most commonly used are: ; comment : Used to note the purpose of the field. ; label : The language key for this label. If the language key is not recognised then this value will be used instead (see the [[#chap08.xhtml#language-chapter|chapter on language]]). ; displayParams : An array used to pass extra arguments for the field display. For the options and how they are used you can have a look into the appropriate field type in include/SugarFields/Fields or custom/include/SugarFields/Fields. An example is setting the size of a textarea:
Example 6.4: DetailView metadata displayParams -----
1 'displayParams' => array(
2     'rows' => 2,
3     'cols' => 30,
4 ),
-----
; customCode : Allows supplying custom smarty code to be used for the display. The code here can include any valid smarty code and this will also have access to the current fields in this view via $fields. An example of outputing the ID field would be {$fields.id.value}. Additionally the module labels and app labels can be accessed via $MOD and $APP respectively. Finally you can use @@FIELD@@ to output the value of the field that would have been used. For example {if $someCondition}@@FIELD@@{/if} will conditionally show the field. #### editviewdefs.php #### editviewdefs.php provides information on the layout and fields of the edit view for this module. This file uses the same structure as detailviewdefs.php. Please see the information on detailviewdefs.php. #### listviewdefs.php #### The listviewdefs.php file for a module defines what fields the list view for that module will display. Let’s take a look at an example:
Example 6.5: ListView metadata definition -----
 1 $listViewDefs ['AOR_Reports'] =
 2 array (
 3   'NAME' =>
 4   array (
 5     'width' => '15%',
 6     'label' => 'LBL_NAME',
 7     'default' => true,
 8     'link' => true,
 9   ),
10   'REPORT_MODULE' =>
11   array (
12     'type' => 'enum',
13     'default' => true,
14     'studio' => 'visible',
15     'label' => 'LBL_REPORT_MODULE',
16     'width' => '15%',
17   ),
18   'ASSIGNED_USER_NAME' =>
19   array (
20     'width' => '15%',
21     'label' => 'LBL_ASSIGNED_TO_NAME',
22     'module' => 'Employees',
23     'id' => 'ASSIGNED_USER_ID',
24     'default' => true,
25   ),
26   'DATE_ENTERED' =>
27   array (
28     'type' => 'datetime',
29     'label' => 'LBL_DATE_ENTERED',
30     'width' => '15%',
31     'default' => true,
32   ),
33   'DATE_MODIFIED' =>
34   array (
35     'type' => 'datetime',
36     'label' => 'LBL_DATE_MODIFIED',
37     'width' => '15%',
38     'default' => true,
39   ),
40 );
-----
To define the list view defs we simply add a key to the $listViewDefs array. In this case we add an entry for AOR_Reports This array contains an entry for each field that we wish to show in the list view and is keyed by the upper case name of the field. For example, the REPORT_MODULE key refers to the report_module field of AOR_Reports. ; type : The type of the field. This can be used to override how a field is displayed. ; default : Whether this field should be shown in the list view by default. If false then the field will appear in the available columns list in studio. ; studio : Whether or not this field should be displayed in studio. This can be useful to ensure that a critical field is not removed. ; label : The label to be used for this field. If this is not supplied then the default label for that field will be used. ; width : The width of the field in the list view. Note that, although this is usually given as a percentage it is treated as a proportion. The example above has five columns with a width of 15% but these will actually be 20% since this is a ratio. #### popupdefs.php #### popupdefs.php provides information on the layout, fields and search options of the module popup that is usually used when selecting a related record. Let’s look at the default popupdefs.php for the Accounts module:
Example 6.6: PopupView metadata definition -----
 1 $popupMeta = array(
 2 	'moduleMain' => 'Case',
 3 	'varName' => 'CASE',
 4 	'className' => 'aCase',
 5 	'orderBy' => 'name',
 6 	'whereClauses' =>
 7 		array('name' => 'cases.name',
 8 				'case_number' => 'cases.case_number',
 9 				'account_name' => 'accounts.name'),
10 	'listviewdefs' => array(
11 		'CASE_NUMBER' => array(
12 			'width' => '5',
13 			'label' => 'LBL_LIST_NUMBER',
14 	        'default' => true),
15 		'NAME' => array(
16 			'width' => '35',
17 			'label' => 'LBL_LIST_SUBJECT',
18 			'link' => true,
19 	        'default' => true),
20 		'ACCOUNT_NAME' => array(
21 			'width' => '25',
22 			'label' => 'LBL_LIST_ACCOUNT_NAME',
23 			'module' => 'Accounts',
24 			'id' => 'ACCOUNT_ID',
25 			'link' => true,
26 	        'default' => true,
27 	        'ACLTag' => 'ACCOUNT',
28 	        'related_fields' => array('account_id')),
29 		'PRIORITY' => array(
30 			'width' => '8',
31 			'label' => 'LBL_LIST_PRIORITY',
32 	        'default' => true),
33 		'STATUS' => array(
34 			'width' => '8',
35 			'label' => 'LBL_LIST_STATUS',
36 	        'default' => true),
37 	    'ASSIGNED_USER_NAME' => array(
38 	        'width' => '2',
39 	        'label' => 'LBL_LIST_ASSIGNED_USER',
40 	        'default' => true,
41 	       ),
42 		),
43 	'searchdefs'   => array(
44 	 	'case_number',
45 		'name',
46 		array(
47 			'name' => 'account_name',
48 			'displayParams' => array(
49 				'hideButtons'=>'true',
50 				'size'=>30,
51 				'class'=>'sqsEnabled sqsNoAutofill'
52 			)
53 		),
54 		'priority',
55 		'status',
56 		array(
57 			'name' => 'assigned_user_id',
58 			'type' => 'enum',
59 			'label' => 'LBL_ASSIGNED_TO',
60 			'function' => array(
61 				'name' => 'get_user_array',
62 				'params' => array(false))
63 			),
64 	  )
65 );
-----
The popupdefs.php specifies a $popupMeta array with the following keys: ; moduleMain : The module that will be displayed by this popup. ; varName : The variable name used to store the search preferences etc. This will usually simply the upper case module name. ; className : The class name of the SugarBean for this module. If this is not supplied then moduleMain will be used. This is only really required for classes where the class name and module name differ (such as Cases). ; orderBy : The default field the list of records will be sorted by. ; whereClauses : Legacy option. This is only used as a fallback when there are no searchdefs. Defines the names of fields to allow searching for and their database representation. ; listviewdefs : The list of fields displayed in the popup list view. See listviewdefs.php. ; searchdefs : An array of the fields that should be available for searching in the popup. See the individual search defs in the searchdefs.php section (for example the basic_search array). #### quickcreatedefs.php #### quickcreatedefs.php provides information on the layout and fields of the quick create view for this module (this is the view that appears when creating a record from a subpanel). This file uses the same structure as detailviewdefs.php. Please see the information on detailviewdefs.php. #### searchdefs.php #### The search defs of a module define how searching in that module looks and behaves. Let’s look at an example.
Example 6.7: Search View metadata definition -----
  1 $searchdefs ['Accounts'] = array (
  2 	'templateMeta' => array (
  3 		'maxColumns' => '3',
  4 		'maxColumnsBasic' => '4',
  5 		'widths' => array (
  6 			'label' => '10',
  7 			'field' => '30'
  8 		)
  9 	),
 10 	'layout' => array (
 11 		'basic_search' => array (
 12 			'name' => array (
 13 				'name' => 'name',
 14 				'default' => true,
 15 				'width' => '10%'
 16 			),
 17 			'current_user_only' => array (
 18 				'name' => 'current_user_only',
 19 				'label' => 'LBL_CURRENT_USER_FILTER',
 20 				'type' => 'bool',
 21 				'default' => true,
 22 				'width' => '10%'
 23 			)
 24 		)
 25 		,
 26 		'advanced_search' => array (
 27 			'name' => array (
 28 				'name' => 'name',
 29 				'default' => true,
 30 				'width' => '10%'
 31 			),
 32 			'website' => array (
 33 				'name' => 'website',
 34 				'default' => true,
 35 				'width' => '10%'
 36 			),
 37 			'phone' => array (
 38 				'name' => 'phone',
 39 				'label' => 'LBL_ANY_PHONE',
 40 				'type' => 'name',
 41 				'default' => true,
 42 				'width' => '10%'
 43 			),
 44 			'email' => array (
 45 				'name' => 'email',
 46 				'label' => 'LBL_ANY_EMAIL',
 47 				'type' => 'name',
 48 				'default' => true,
 49 				'width' => '10%'
 50 			),
 51 			'address_street' => array (
 52 				'name' => 'address_street',
 53 				'label' => 'LBL_ANY_ADDRESS',
 54 				'type' => 'name',
 55 				'default' => true,
 56 				'width' => '10%'
 57 			),
 58 			'address_city' => array (
 59 				'name' => 'address_city',
 60 				'label' => 'LBL_CITY',
 61 				'type' => 'name',
 62 				'default' => true,
 63 				'width' => '10%'
 64 			),
 65 			'address_state' => array (
 66 				'name' => 'address_state',
 67 				'label' => 'LBL_STATE',
 68 				'type' => 'name',
 69 				'default' => true,
 70 				'width' => '10%'
 71 			),
 72 			'address_postalcode' => array (
 73 				'name' => 'address_postalcode',
 74 				'label' => 'LBL_POSTAL_CODE',
 75 				'type' => 'name',
 76 				'default' => true,
 77 				'width' => '10%'
 78 			),
 79 			'billing_address_country' => array (
 80 				'name' => 'billing_address_country',
 81 				'label' => 'LBL_COUNTRY',
 82 				'type' => 'name',
 83 				'options' => 'countries_dom',
 84 				'default' => true,
 85 				'width' => '10%'
 86 			),
 87 			'account_type' => array (
 88 				'name' => 'account_type',
 89 				'default' => true,
 90 				'width' => '10%'
 91 			),
 92 			'industry' => array (
 93 				'name' => 'industry',
 94 				'default' => true,
 95 				'width' => '10%'
 96 			),
 97 			'assigned_user_id' => array (
 98 				'name' => 'assigned_user_id',
 99 				'type' => 'enum',
100 				'label' => 'LBL_ASSIGNED_TO',
101 				'function' => array (
102 					'name' => 'get_user_array',
103 					'params' => array (
104 							0 => false
105 					)
106 				),
107 				'default' => true,
108 				'width' => '10%'
109 			)
110 		)
111 	)
112 );
-----
Here we setup a new array for Accounts in the $searchdefs array. This has two keys: ####= templateMeta ####= The templateMeta key controls the basic look of the search forms. Here we define some overall layout info such as the maximum columns (3) and the maximum number of columns for the basic search (4). Finally we set the widths for the search fields and their labels. ####= layout ####= The layout key contains the layout definitions for the basic search and advanced search. This is simply a list of array definition of the fields. See the section on listviewdefs.php for a description of some of the options. #### subpaneldefs.php #### The subpaneldefs.php file provides definitions for the subpanels that appear in the detail view of a module. Let’s look at an example:
Example 6.8: Subpanel metadata definition -----
 1 $layout_defs['AOS_Quotes'] = array (
 2 	'subpanel_setup' => array (
 3 		'aos_quotes_aos_contracts' => array (
 4 			'order' => 100,
 5 			'module' => 'AOS_Contracts',
 6 			'subpanel_name' => 'default',
 7 			'sort_order' => 'asc',
 8 			'sort_by' => 'id',
 9 			'title_key' => 'AOS_Contracts',
10 			'get_subpanel_data' => 'aos_quotes_aos_contracts',
11 			'top_buttons' => array (
12 				0 => array (
13 					'widget_class' => 'SubPanelTopCreateButton'
14 				),
15 				1 => array (
16 					'widget_class' => 'SubPanelTopSelectButton',
17 					'popup_module' => 'AOS_Contracts',
18 					'mode' => 'MultiSelect'
19 				)
20 			)
21 		),
22 		'aos_quotes_aos_invoices' => array (
23 			'order' => 100,
24 			'module' => 'AOS_Invoices',
25 			'subpanel_name' => 'default',
26 			'sort_order' => 'asc',
27 			'sort_by' => 'id',
28 			'title_key' => 'AOS_Invoices',
29 			'get_subpanel_data' => 'aos_quotes_aos_invoices',
30 			'top_buttons' => array (
31 				0 => array (
32 					'widget_class' => 'SubPanelTopCreateButton'
33 				),
34 				1 => array (
35 					'widget_class' => 'SubPanelTopSelectButton',
36 					'popup_module' => 'AOS_Invoices',
37 					'mode' => 'MultiSelect'
38 				)
39 			)
40 		),
41 		'aos_quotes_project' => array (
42 			'order' => 100,
43 			'module' => 'Project',
44 			'subpanel_name' => 'default',
45 			'sort_order' => 'asc',
46 			'sort_by' => 'id',
47 			'title_key' => 'Project',
48 			'get_subpanel_data' => 'aos_quotes_project',
49 			'top_buttons' => array (
50 				0 => array (
51 					'widget_class' => 'SubPanelTopCreateButton'
52 				),
53 				1 => array (
54 					'widget_class' => 'SubPanelTopSelectButton',
55 					'popup_module' => 'Accounts',
56 					'mode' => 'MultiSelect'
57 				)
58 			)
59 		)
60 	)
61 );
-----
In the example above we set up a definition for a module (in this case AOS_Quotes) in the $layout_defs array. This has a single key subpanel_setup which is an array of each of the subpanel definitions keyed by a name. This name should be something recognisable. In the case above it is the name of the link field displayed by the subpanel. The entry for each subpanel usually has the following defined: ; order : A number used for sorting the subpanels. The values themselves are arbitrary and are only used relative to other subpanels. ; module : The module which will be displayed by this subpanel. For example the aos_quotes_project def in the example above will display a list of Project records. ; subpanel_name : The subpanel from the displayed module which will be used. See the subpanels section of this chapter. ; sort_by : The field to sort the records on. ; sort_order : The order in which to sort the sort_by field. asc for ascending desc for descending. ; title_key : The language key to be used for the label of this subpanel. ; get_subpanel_data : Used to specify where to retrieve the subpanel records. Usually this is just a link name for the current module. In this case the related records will be displayed in the subpanel. However, for more complex links, it is possible to specify a function to call. When specifying a function you should ensure that the get_subpanel_data entry is in the form function:theFunctionName. Additionally you can specify the location of the function and any additional parameters that are needed by using the function_parameters key. An example of a subpanel which uses a function can be found in [[#chap19.xhtml#appendix-a|Appendix A]]. ; function_parameters : Specifies the parameters for a subpanel which gets it’s information from a function (see
get_subpanel_data). This is an array which allows specifying where the function is by using the import_function_file key (if this is absent but get_subpanel_data defines a function then the function will be called on the bean for the parent of the subpanel). Additionally this array will be passed as an argument to the function defined in get_subpanel_data which allows passing in arguments to the function. ; generate_select : For function subpanels (see get_subpanel_data) whether or not the function will return an array representing the query to be used (for generate_select = true) or whether it will simply return the query to be used as a string. ; get_distinct_data : Whether or not to only return distinct rows. Relationships do not allow linking two records more than once therefore this only really applies if the subpanel source is a function. See
get_subpanel_data for information on function subpanel sources. ; top_buttons : Allows defining the buttons to appear on the subpanel. This is simply an array of the button definitions. These definitions have, at least, the widget_class defined which decides the button class to use in include/generic/SugarWidgets. Depending on the button this array may also be used to pass in extra arguments to the widget class. #### subpanels #### Inside the metadata folder is the subpanels folder. This allows creating different subpanel layouts for different parent modules. For example, the Contacts module will display differently in the subpanel on an account than it will in the subpanel of a case. The files inside the subpanels folder can be named anything. All that matters is that it can be referenced in the subpanel_name of the subpaneldefs.php of the parent module. The usual subpanel file is simply called default.php. Let’s look at the modules/Accounts/metadata/subpanels/default.php file:
Example 6.8: Module Subpanels definition -----
 1 $subpanel_layout = array(
 2 	'top_buttons' => array(
 3 		array(
 4 			'widget_class' => 'SubPanelTopCreateButton'
 5 		),
 6 		array(
 7 			'widget_class' => 'SubPanelTopSelectButton', 
 8 			'popup_module' => 'Accounts'
 9 		),
10 	),
11 	'where' => '',
12 	'list_fields' => array (
13 	  'name' =>
14 	  array (
15 	   'vname' => 'LBL_LIST_ACCOUNT_NAME',
16 	   'widget_class' => 'SubPanelDetailViewLink',
17 	   'width' => '45%',
18 	   'default' => true,
19 	  ),
20 	  'billing_address_city' =>
21 	  array (
22     	'vname' => 'LBL_LIST_CITY',
23     	'width' => '20%',
24     	'default' => true,
25 	  ),
26 	  'billing_address_country' =>
27 	  array (
28 	  	'type' => 'varchar',
29     	'vname' => 'LBL_BILLING_ADDRESS_COUNTRY',
30     	'width' => '7%',
31     	'default' => true,
32 	  ),
33 	  'phone_office' =>
34 	  array (
35 	  	'vname' => 'LBL_LIST_PHONE',
36 	  	'width' => '20%',
37 	  	'default' => true,
38 	  ),
39 	  'edit_button' =>
40 	  array (
41 	  	'vname' => 'LBL_EDIT_BUTTON',
42 	  	'widget_class' => 'SubPanelEditButton',
43 	  	'width' => '4%',
44 	  	'default' => true,
45 	  ),
46 	  'remove_button' =>
47 	  array (
48 	    'vname' => 'LBL_REMOVE',
49 	    'widget_class' => 'SubPanelRemoveButtonAccount',
50 	    'width' => '4%',
51 	    'default' => true,
52 	  ),
53    )
54 );
-----
There are three keys in the $subpanel_layout variable for this subpanel. These are: ; top_buttons : Defines the buttons that will appear at the top of the subpanel. See the top_buttons key in subpaneldefs.php. ; where : Allows the addition of conditions to the where clause. For example this could be used to exclude Cases that are closed (cases.state != "Closed") or only include Accounts of a specific industry (accounts.industry = "Media"). Note that in these examples we specify the table to remove any ambiguity in the query. ; list_fields : Defines the list of fields to be displayed in this subpanel. See the section on listviewdefs.php for more information. #### studio.php #### studio.php is the simplest file in metadata and it’s existence is simply used to confirm if a module should be shown in studio for user tweaking. Note that, unlike other metadata files, the file in modules/<TheModule>/metadata/studio.php will be the only one checked. A file in custom/modules/<TheModule>/metadata/studio.php will have no effect.