How to add new tab on publish form in ExpressionEngine 2.x
I’m writing an extension that called when an entry is submitted.
Now i want to add a tab with few fields in publish from?Can i do this from extension?
I know there are EE1.x hooks - publish_form_new_tabs, publish_for开发者_开发问答m_new_tabs_block
....
The 2.x module API allows this. Documentation here: http://expressionengine.com/user_guide/development/modules.html
You can combine a module and extension of the same type with a structure like this:
/system/expressionengine/third_party/addon_name/ ext.addon_name.php language/ english/ lang.addon_name.php mcp.addon_name.php mod.addon_name.php tab.addon_name.php # Add fields to tab here, per API spec upd.addon_name.php # Add/remove tab here, per API spec
When a user installs either the extension or module, they'll be asked automatically if they want to install both at the same time. Obviously if the purpose is just to add a tab/fields to the publisher, your mcp and mod classes may be just the minimum needed to successfully install a module.
Hope this is a good starting point.
To add to the first answer,
one thing that I constantly had problems with is not calling the add_layout_tabs
method anywhere in the update file of your module (i.e .the file that starts with upd.
)
So suppose your update file is called: upd.addon_name.php
. Then in the install
looks like this (note: the following functions are all part of the Addon_name_upd
class inside your update file:
function install () {
// ... create databases or any necessary code for your module
$this->EE->load->library('layout');
$this->EE->layout->add_layout_tabs($this->tabs(), 'addon_name');
return TRUE;
}
Notice the call to $this->tabs()
method: That method looks something like this:
function tabs() {
$tabs['addon_name'] = array(
'field_1_inside_publish_form' => array(
'visible' => 'true',
'collapse'=> 'false',
'htmlbuttons' => 'true',
'width' => '100%'
)
);
return $tabs;
}
Where field_1_inside_publish_form
is a field that would be defined in your corresponding tab file for the module (i.e. tab.addon_name.php
).
The install method will in effect save a new tab to the existing publish layout that includes your module's tab configuration.
However, you must add a method to remove your configuration in the uninstall method of the update file by calling the delete_layout_tabs
method like so:
function uninstall() {
// necessary code to drop your database tables or whatever
$this->EE->load->library('layout');
$this->EE->layout->delete_layout_tabs($this->tabs(), 'addon_name');
return TRUE;
}
One more thing, if you develop like I do: make a little change here, test your change, go back and code some more, then you'll find that if new fields added to the tab file tab.addon_name.php
after your module is enabled don't appear in your new tab on the publish page. The reason is because the method add_layout_tabs
that you call in the install method of your update file needs to be ran.
However, this method only gets executed when you enable your module. So that means you have to disable your module, which is a drag if your module adds database tables. Luckily you can make the update method of your update file load your new tab configuration (in addition to adding or dropping any new database tables as part of your module's updates.)
The idea is that you delete your previous configuration and you add the configuration, which, calls your tabs method which has the names of the the new fields for your module's tab section.
So, suppose your tabs method has a new field called 'field_2_inside_publish_form' like so:
function tabs() {
$tabs['addon_name'] = array(
'field_1_inside_publish_form' => array(
'visible' => 'true',
'collapse'=> 'false',
'htmlbuttons' => 'true',
'width' => '100%'
),
'field_2_inside_publish_form' => array(
'visible' => 'true',
'collapse'=> 'false',
'htmlbuttons' => 'true',
'width' => '100%'
)
);
}
then your update method can update the layout like so (assume that you've updated your $this->version
property in the udpate file from '1.0' to '1.5'.
function update($current='') {
// don't do anything if the version hasn't changed
if($current == $this->version) {
return FALSE;
}
// the version property has a version higher than current version in db
// this means the module is being updated.
if($current < $this->version) {
// update the tab layout
// delete old layout
$this->EE->load->library('layout');
$this->EE->layout->delete_layout_tabs($this->tabs(), 'addon_name');
// add new tab layout which calls tabs method with updated code
$this->EE->load->library('layout');
$this->EE->layout->add_layout_tabs($this->tabs(), 'addon_name');
}
return TRUE;
}
Remember that the update method of the update file of your module gets run everytime you are in your module's control panel page defined in in your modules control panel page file (i.e. mcp.addon_name.php
). Your module's main control panel page would probably correspond to the index method of the Addon_name_mcp
class of your control panel page file.
It would correspond to a link uri like this: admin.php?S=0&D=cp&C=addons_modules&M=show_module_cp&module=addon_name&method=index
Hope that helps. It's kind of long winded but I wrote it more for my own benefit than for anyone else (cause I wasted like 3 hours trying to get my module to work.)
精彩评论