问题
In SugarCRM installable changes in detailview the question was asked about how to add a panel, using the Module Installer's manifest file to add to the the editview/detailview of an existing module without wiping out customizations previously made in the custom directory.
The answer was provided how to add fields, but not panels.
I know you could use a post_execute function called from the manifest file to edit the editviewdefs.php and detailviewdefs.php files in the /custom/modules//metadata/ directory, but that involves making some guesses about what already exists in those files.
Does anyone know how to add panels via the manifest file (or post_execute) that incrementally adds the panel, without using php code to manually edit the editviewdefs.php and detailviewdefs.php files?
回答1:
You're after the ParserFactory class, which can be used from a post_install or similar script executed as your module/package is installed. My understanding is that ParserFactory will call custom files if they're there, or stock files and adjust them appropriately and safely if not.
In your module's directory, create a subdirectory called 'scripts' and create 'post_install.php' which contains a single function post_install(). Your package dir will look something like this:
~$ find .
/LICENSE.txt
/manifest.php
/scripts/post_install.php
You use the ParserFactory like this:
<?php
function post_install(){
// $parser becomes an associative array for the metadata, similar to editviewdefs.php
require_once('modules/ModuleBuilder/parsers/ParserFactory.php');
$parser = ParserFactory::getParser('detailview', 'Accounts');
// finding the panels (which contain their fields), you can
// now add fields or additional arrays to this array, creating new panels
// (you could manipulate $parser->_viewdefs directly, but I find this easier)
$panels = array_keys ( $parser->_viewdefs [ 'panels' ] );
// place your panels back into the $parser and save
$parser->_viewdefs['panels'] = $panels;
$parser->handleSave(false);
};
回答2:
Bingo - and Thank you to Matthew
I did not know where to find this and on the Sugar forum, no-one seemed to know so thank you Matthew.
So yes, it is very easy to add panels to an existing module in SugarCRM using the code that Matthew pointed out
To add a Panel called Events, in
/custom/modules/Accounts/language/en_us.lang.php
(add to this or create new file if you prefer)
add
$mod_strings = array ('LBL_DETAILVIEW_PANEL1' => 'Events',);
and then in the post_install.php file in the /scripts directory of the install package put
<?php
function post_install()
{
// Debug point - checking to see if get to post_install script
echo "Made it to the post_install script.<br />";
// Use the ParserFactory to edit the view arrays
// Fetch the existing view into an array called $view_array
require_once('modules/ModuleBuilder/parsers/ParserFactory.php');
$view_array = ParserFactory::getParser('detailview','Accounts');
// Declare the additional content
$new_content = array
(
0 => array
(
0 => array
(
'name' => 'created_by_name',
'label' => 'LBL_CREATED',
),
1 => array
(
'name' => 'modified_by_name',
'label' => 'LBL_MODIFIED_NAME',
),
),
);
// Add the new content to the desired section of the view array
$view_array->_viewdefs['panels']['lbl_detailview_panel1'] = $new_content;
//Save the layout
$view_array->handleSave(false);
return;
}
?>
(I have just put two existing fields in the new panel but you can just as easily place newly created fields (from the manifest file) into the new panel as well
来源:https://stackoverflow.com/questions/21444143/adding-panels-to-editviewdefs-php-via-manifest-file