Monday, July 25, 2011

SugarCRM - Adding a Custom View Using the MVC Model

I had been trying to find a good example of adding a custom view to an existing module in SugarCRM using the MVC model, but was unsuccessful.  I did find some examples though that did tasks that were similar and i was able to put together this example of adding the custom view.

The tasks that I will do in order to accomplish this are the following:

1.  Add a new item to the menu bar for calling the new view
2.  Add a controller for the module that will define the new action
3.  Create the new view

For my example I will be adding a new view called see1 that has a label of "See Me" in the menu to the Accounts module.

1.  Add New Item to Menu Bar

In order to do this, you will need to make a new file called menu.ext.php in custom/Extension/modules/Accounts/Ext/Menus/.  If the directory does exist, go ahead and make it.  In menu.ext.php you will need the following code:

<?PHP
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

if(ACLController::checkAccess('Accounts', 'edit', true)) $module_menu[]=Array(
"index.php?module=Accounts&action=See1",
"See Me","Accounts");

?>

The label of the link is defined above as See Me.  Also, I have defined the action as See1.  This is important to keep track of for the controller.

This file will now Add the link See Me to the other links for the Accounts module.  So, for me, I now have 4 Actions listed: Create Account, View Accounts, Import Accounts, and See Me.

2.  Add a Controller

Now that we have a link calling a new action, we need to define what the new action will do.  In order to do this, we need to create a new file that maps the action.  The new file will be called controller.php. and it will reside in custom/modules/Accounts/.  The contents of the file will be the folloiwng:

<?PHP
require_once('include/MVC/Controller/SugarController.php');

class AccountsController extends SugarController {

        function action_See1() {
                $this->view = "lookatit";
        }
}

?>

Here are a couple of notes about the file above.  The naming convention is case sensitive.  Also, notice how the action is defined as a function with the prefix "action_".  Lastly, the name of the view that will be called is defined in this as lookatit.  This is important to remember for he next step.

3.  Create a New View File

The last step in getting the new view to work is creating the new view that you would like to see.  In order to do this, we will need to create a new file in custom/modules/Accounts/views/.  If the directory does not exist, go ahead and create it.  The new file to put in the directory will be called view.lookatit.php.  The naming convention is important in that you have view preface it and the view name, "lookatit" for me, from the controller as the name.  In the file you could put the following:

<?PHP
require_once('include/MVC/View/SugarView.php');

class AccountsViewLookAtIt extends SugarView {

        public function AccountsViewLookAtIt() {
                parent::SugarView();
        }

        public function display() {

                echo "Here!\n";
        }
}
?>

The contents of this file will contain whatever you would like your new view to have.  This example just shows the text "Here!", but it is in the SugarCRM frame.  

So, this gets us to the point of having a custom view inside of the MVC framework of SugarCRM.

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete