How to add options for backend users when creating/editing content
How to add options for backend users when creating/editing content
Hi all, I'm real sold on CMSms, lovely to work with and such beautiful code. I got a hang on how to write modules, but there's still more that I probably should learn before I get all "set in my ways" lol.
Is it possible for my module to add extra fields to the create content and edit content forms? I want to have a drop menu that allows staff to select which user groups are allowed to view the page. Of course, I would prefer if this could be done with a hook or some similar technique rather than go hacking at the core files.
Any comments and links to relevant documentation is greatly appreciated, cheers!
Is it possible for my module to add extra fields to the create content and edit content forms? I want to have a drop menu that allows staff to select which user groups are allowed to view the page. Of course, I would prefer if this could be done with a hook or some similar technique rather than go hacking at the core files.
Any comments and links to relevant documentation is greatly appreciated, cheers!
Re: How to add options for backend users when creating/editing content
Cheers for the link.
Re: How to add options for backend users when creating/editing content
Can't see anything in there that does it, and the form appears to be hard-coded into admin/editcontent.php...
I tracked down the origin of the looped form elements, defined in Content.inc.php line 209 :
... I'm thinking of adding something similar that will allow modules to insert form fields by just returning an array from a method like :
... see any problems or room for improvement?
I tracked down the origin of the looped form elements, defined in Content.inc.php line 209 :
Code: Select all
<?php
$additionalcall = '';
foreach($gCms->modules as $key=>$value)
{
if (get_preference(get_userid(), 'wysiwyg')!="" &&
$gCms->modules[$key]['installed'] == true &&
$gCms->modules[$key]['active'] == true &&
$gCms->modules[$key]['object']->IsWYSIWYG() &&
$gCms->modules[$key]['object']->GetName()==get_preference(get_userid(), 'wysiwyg'))
)
{
$additionalcall = $gCms->modules[$key]['object']->WYSIWYGPageFormSubmit();
}
}
Code: Select all
<?php
class MyModule
{
function GetCustomContentFields(){ return array(lang('label'), '<input name="whatever" />', 'MethodToCallOnSubmit'); }
}
Re: How to add options for backend users when creating/editing content
Returns an array of arrays, so can do multiple fields. Then the 3rd parameter 'MethodToCallOnSubmit' is not required for each field, it could be just a set method in the plugin class:
Will have a crack at it today.
Code: Select all
<?php
class MyModule
{
function PostContentSubmit(){
if(count($_POST)>0){
// do something with the submitted data
}
}
}
Last edited by oi_antz on Fri Jul 18, 2008 10:45 pm, edited 1 time in total.
Re: How to add options for backend users when creating/editing content
Part one of the solution:
Open lib/classes/contenttypes/Content.inc.php
Find the point where $additionalcall gets defined (around line 209).
Replace the existing code:
With the following code, noting the additional statements that "get custom form fields" :
... so far, this will allow any module to add form fields to the "Add new page" and "Edit existing page" pages in the admin section, under content > pages
Your module will need to provide a method called "GetContentFields", something like this:
Now onto step two - processing the data...
Open lib/classes/contenttypes/Content.inc.php
Find the point where $additionalcall gets defined (around line 209).
Replace the existing code:
Code: Select all
<?php
$additionalcall = '';
foreach($gCms->modules as $key=>$value)
{
if (get_preference(get_userid(), 'wysiwyg')!="" &&
$gCms->modules[$key]['installed'] == true &&
$gCms->modules[$key]['active'] == true &&
$gCms->modules[$key]['object']->IsWYSIWYG() &&
$gCms->modules[$key]['object']->GetName()==get_preference(get_userid(), 'wysiwyg'))
{
$additionalcall = $gCms->modules[$key]['object']->WYSIWYGPageFormSubmit();
}
}
Code: Select all
<?php
$additionalcall = '';
foreach($gCms->modules as $key=>$value)
{
if (get_preference(get_userid(), 'wysiwyg')!="" &&
$gCms->modules[$key]['installed'] == true &&
$gCms->modules[$key]['active'] == true &&
$gCms->modules[$key]['object']->IsWYSIWYG() &&
$gCms->modules[$key]['object']->GetName()==get_preference(get_userid(), 'wysiwyg'))
{
$additionalcall = $gCms->modules[$key]['object']->WYSIWYGPageFormSubmit();
}
// get custom form fields
if(method_exists($gCms->modules[$key]['object'], 'GetContentFields')){
$extraFields = $gCms->modules[$key]['object']->getContentFields();
if(!is_array($extraFields)) continue;
foreach($extraFields as $extraField){
if(!is_array($extraField) || count($extraField)!=2) continue;
$ret[] = $extraField;
};
};
}
Your module will need to provide a method called "GetContentFields", something like this:
Code: Select all
<?php
class FEUsers extends CMSModule
{
function GetContentFields(){
return array(
array('Access Group', '<select name="access_groups"><option value="public">Public</option></select>')
);
}
}
Re: How to add options for backend users when creating/editing content
Well, as it turns out, there is already a call that can be utilized for this:
$pluginObj->ContentEditPre($contentObj);
or
$pluginObj->ContentEditPost($contentObj);
That's nice, reduces the number of hacks I have to keep track of.
Hope this is useful to someone wanting to add custom fields to the content forms from within their module.
$pluginObj->ContentEditPre($contentObj);
or
$pluginObj->ContentEditPost($contentObj);
That's nice, reduces the number of hacks I have to keep track of.
Hope this is useful to someone wanting to add custom fields to the content forms from within their module.
Re: How to add options for backend users when creating/editing content
It is worthwhile pointing out that when creating new pages, the page id $contentObj->Id() is not set until ContentEditPost(). So if your module depends on knowing the page id, you will need to use ContentEditPost() for your processing logic.
Re: How to add options for backend users when creating/editing content
Here is the resulting file. Make sure you backup the existing file before overwriting it!
download the file attached to the next reply, it has been updated
download the file attached to the next reply, it has been updated
- Attachments
-
[The extension txt has been deactivated and can no longer be displayed.]
Last edited by oi_antz on Mon Jul 21, 2008 9:51 pm, edited 1 time in total.
Re: How to add options for backend users when creating/editing content
Updated the file, the call to $ModuleName->GetContentFields() now passes the content object so you can get the page id from it.
eg:
eg:
Code: Select all
<?php
function GetContentFields($contentObj){
$DB = Antz::getDb('cmsms');
//$DB->debug(true);
//$DB->showErrors(true);
$pageid = (int) $contentObj->Id();
if($pageid > -1){
// page exists, get the groups
$currentGroups = $DB->fetchAll("SELECT * FROM ".cms_db_prefix()."fe_pages_to_groups WHERE page_id = '{$pageid}'");
$cg = array();
foreach($currentGroups as $k=>$v){
$cg[$k] = $v['group_key'];
};
}else{
$cg = array('public');
};
$accessGroups = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}fe_user_groups");
$opts = array();
foreach($accessGroups as $k=>$v){
$selected = (in_array($v['keyname'], $cg)) ? 'selected="selected"' : '';
$opts[] = '<option value="'.$v['keyname'].'" '.$selected.'>'.$v['title'].'</option>';
};
return array(
array('Access Groups:', '<select name="access_groups[]" multiple="multiple">'.implode("\n", $opts).'</select>')
);
}
- Attachments
-
[The extension txt has been deactivated and can no longer be displayed.]
-
[The extension txt has been deactivated and can no longer be displayed.]