画布
画布是替换屏幕显示的开发功能。
以新窗体替换老窗体来创建、更新或查看记录(产品,第三方,联系人,…)。
例如,您可以替换掉系统自带的“新建第三方”的窗体,或者“修改第三方”的窗体,或者替换掉查看模板。
条件
画布功能于 Dolibarr 3.2 以后可用。
它只适用于:
- products, contacts 和 third parties 对象的新建 窗体
- products, contacts and third parties对象的编辑 窗体
- products, contacts and third parties对象的显示 选项卡。 多数情况下,是打开对象时的第一个选项卡。您可以查看新的模块创建文档: Module_development#The_tab_management 了解如何管理选项卡。
用自己的模板替换创建表单
首先要找到要替换的标准创建表单的URL并添加参数: canvas=mycanvas@mymodule. 要找到URL,你可以在浏览器顶部找到URL。
例如,你想替换掉新建第三方的表单。URL有点像
http://mydomain/societe/soc.php?action=create&leftmenu=customers
添加了画布参数后,URL看起来会像:
http://mydomain/societe/soc.php?action=create&leftmenu=customers&canvas=mycanvas@mymodule
当你在“新建”页面增加了参数 canvas=mycanvas@mymodule , Dolibarr 将使用模板文件
/mymodule/canvas/mycanvas/tpl/tabname_create.tpl
在这里:
- mymodule 目录能找到画布参数的mymodule模块名(您选择此值并创建具有相同值的目录。使用小写值).
- mycanvas is value mycanvas found into canvas parameter (you choose this value and create directory with same value. use lower case value).
- tabname 存储初始表单的选项卡的名称。在大多数情况下,它是价值的“card”。
For example, we will use value "company@societe" for canvas parameter. This is a value of an already coded demo canvas template for third parties, not used in a standard usage of Dolibarr but provided as "demo". So URL to create a third party using this alternative template is now:
http://mydomain/societe/soc.php?action=create&leftmenu=customers&canvas=societe@company
This means the form to create the third party will be found into template file:
/societe/canvas/company/tpl/card_create.tpl
So to use you own template, just choose a value for mymodule, mycanvas, copy and paste a demo template found into Dolibarr directories with new directory and new file name and then edit the template like you want. Template contains all the part under the top menu and at right of left menu. You can change look, field order, add your javascript controls, remove or add your own fields. The only requirement is to keep mandatory fields with same name to avoid errors when you will submit the page. Fields removed will be filled with null or 0 into database. New fields for the moment are ignored. We will see later how we can add code to save them.
Once this is done, you must now add the new create URL into Dolibarr menus or replace the existing one with new one. For this take a look at page Menus_system.
Note that when a record is saved using a canvas, the canvas name mycanvas@mymodule is also stored into database. This is important because this means, Dolibarr will be able to know that this record is a canvas and not the other so it will be able to them suggest a view or edit form dedicated for this record only and keep using standard forms for other records. See next chapters.
Replace an edit form with your own template
Using your own edit form for a record saved using your own create form is as simple as adding a file called
/mymodule/canvas/mycanvas/tpl/tabname_edit.tpl
where
- mymodule directory is value mymodule you used as canvas parameter to create record (see previous chapter).
- mycanvas is value mycanvas you used as canvas parameter to create record (see previous chapter).
- tabname is name of tab or page to edit record. In most cases it is value "card".
With our canvas demo societe@company, edit template will be into file
/societe/canvas/company/tpl/card_edit.tpl
Note that for edit, changing URL is not necessarly required. Dolibarr can detect your record is a personalized canvas record and automatically scan for this template file to show it if found. If not, standard form will be used. So all you have to do to use your own edit template is copy and paste an existing demo edit template file and rename it with correct value of mymodule and mycanvas and edit it to match your need. Like for creation, all mandatory fields must be kept to avoid errors when submitting the form to save change.
Replace a view form with your own template
Using your own view form for a record saved using your own create form is as simple as adding a file called
/mymodule/canvas/mycanvas/tpl/tabname_view.tpl
where
- mymodule directory is value mymodule you used as canvas parameter to create record (see previous chapter).
- mycanvas is value mycanvas you used as canvas parameter to create record (see previous chapter).
- tabname is name of tab or page to edit record. In most cases it is value "card".
With our canvas demo societe@company, view template will be into file
/societe/canvas/company/tpl/card_view.tpl
Note that for view, changing URL is not necessarly required. Dolibarr can detect your record is a personalized canvas record and automatically scan for this template file to show it if found. If not, standard form will be used. So all you have to do to use your own view template is copy and paste an existing demo view template file and rename it with correct value of mymodule and mycanvas and edit it to match your need.
Add code to manage new fields
Example into external module cabinetmed.