Additional tab
This mechanism allows you to add your own tabs or interface modifications to exisiting Navigate CMS functions. For example, the Akismet antispam extension adds a tab when viewing a comment. The new tab has two functions: check if a comment is spam and report the correctness of the detection to the Akismet service.
To add the new tab we have to tell Navigate CMS to do something when the user requests viewing a comment. That is accomplished by binding to the "edit" event of the "comments" function in the extension file definition:
{
"title": "Akismet antispam",
"(more_properties)": "(more_values)"
"bindings": [
{
"module": "comments",
"event": "edit",
"function": "nvweb_akismet_antispam_edit_tab"
}
]
}
The bindings section in the definition declares when and what Navigate CMS must execute on certain events. You can find a full list of possible module/event combinations in the Events section of this manual. nvweb_akismet_antispam_edit_tab is a function that we need to write in our extension main PHP file (akismet_antispam.php).
function nvweb_akismet_antispam_edit_tab($params)
{
global $layout;
global $user;
$extension = new Extension();
$extension->load('akismet_antispam');
$comment = $params['comment'];
$navibars = $params['navibars'];
$naviforms = $params['naviforms'];
$navibars->add_tab('Akismet Antispam');
$navibars->add_tab_content_row(
array(
'<label>Akismet</label>',
'<button id="akismet_check">'.$extension->t('test_comment').'</button>'
)
);
// [...]
$layout->add_script('
$("#akismet_check").on("click", function()
{
// [...]
});
// [...]
');
}
Let's analyze the function code step by step. We named our function "nvweb_akismet_antispam_edit_tab" to avoid any kind of duplicated names problems. The rule is simple "nvweb" because it is a Navigate CMS extension, "akismet_antispam" as the unique extension name and "edit_tab" because it describes where the function will be executed.
Notice it recieves only one parameter, but later in the code we see that parameter is an array of objects: the comment, the navibars layout object and the naviforms instance that is generating the form fields. navibars and naviforms are not explained in this section of the manual as they belong to the development side of Navigate CMS. However, what they do is clear enough to understand this example. Depending on the module/event combination other elements will be sent in the array. You will have to check the Events section to know what is sent on each case.
$extension = new Extension();
$extension->load('akismet_antispam');
This code loads the extension, its options and the extension dictionary. Later, when we do:
$extension->t('test_comment')
we are asking for the extension translation of the string "test_comment" in the active language. Refer to the extension definition section of this manual to know how to write a dictionary for your extension.
Once initialized, we include the HTML contents of our tab with $navibars->add_tab_content_row and the associated javascript code with $layout->add_script.
You are free to do anything in your extension function: read the $_POST values, calculate something, save values in the database, generate reports...
Summary
To add a new functionality in Navigate CMS you have to:
- Identify the module/event combination you need and the values you will get from Navigate CMS.
- Write the bindings section in your extension definition.
- Create the function that will be executed on the event.
- Add HTML/JS code that will be inserted in your tab and/or write the functionality you want to offer.
0 Comments