- Posts: 15
COMMUNITY FORUM
item -> extra_fields empty in onBeforeK2Save
- Jerry
- Topic Author
- Offline
- New Member
I've been trying and trying but cannot get it to work. I hope someone can give some useful clues here.
I want people to enter calendar events: date, hour, minute, description. Nothing more. So I hide title, alias, publish up, publish down, category.
I have a template override (/templates/MYTEMPLATE/html/com_k2/MYSUBTEMPLATE/itemform.php) to hide all but the few extra fields. As far as I can tell, I have to use this:
$efs = array();
foreach ( $this->extraFields as $ef ) {
switch ( $ef->id ) {
case 1:
$efs[ "datum" ] = $ef;
break;
// for all extra fields, I check for the ID to know which field it is
// the name/label might change afterwards so I cannot use that
// pity that when I reinstall this website to production environment,
// the IDs will change
}
And when I have all fields in the $efs array, I do stuff like this:
<tr>
<td align="right" class="key">Datum:</td>
<td class="inputbox">
<span id="datum"><?php print $efs[ "datum" ]->element; ?></span>
</td>
</tr>
Then, in /plugins/k2/MYPLUGIN/myplugin.php, function onBeforeK2Save, I only get &$item and $isNew. The $_POST has K2ExtraField_1 etc. How do I get my definition of extra fields here? Or is the order the only way? How can I use something like K2ExtraField_Datum?
If I do that myself, I have to generate the inputs myself completely, making the extra field type useless. And I bet the extra fields will not be filled correctly.
Thanks!
Jerry
Please Log in or Create an account to join the conversation.
- Lefteris
- Offline
- Moderator
- Posts: 8743
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Jerry
- Topic Author
- Offline
- New Member
- Posts: 15
What I basically want to do is something like this:
function onBeforeK2Save(&$item,$isNew) {
// ...
$item->title = $item->extra_fields[ "EventDate" ] . ", " . $item->extra_fields[ "EventTime" ];
// ...
}
That's it.
I just found out the X in K2ExtraFields_X is the ID. For some reason the extra fields, though entered in a completely different order, were now ordered in ID order, so I thought they were order numbers and not IDs. Although a key would still be better than an ID (DATE, HOUR, and MINUTE instead of 3, 1, and 2), I can now identify the extra fields.
Still, I don't know why it's called $item->extraFields in the template and ->extra_fields in the onBeforeK2Save, and why the latter is NULL.
Please Log in or Create an account to join the conversation.
- Jerry
- Topic Author
- Offline
- New Member
- Posts: 15
So, in the onBeforeK2Save, I need to have access to the definitions of the extra fields. Is that possible?
Thanks!!
Please Log in or Create an account to join the conversation.
- Jerry
- Topic Author
- Offline
- New Member
- Posts: 15
Recap of the problem: I need to access the extra fields to set the title of an item.
For a readable title, I need the name of the selected select box item and not the value. So getting the ID value from the $_POST, the code below gets the defined values from the Extra Fields definition.
$ef_hour = $_POST[ "K2ExtraField_2" ]; // ExtraField "Hour" has ID = 2
$ef_hour_val = "";
$query = "SELECT value FROM #__k2_extra_fields WHERE id=2";
$db->setQuery($query);
$arr = json_decode( $db->loadResult() );
foreach ( $arr as $elm ) {
if ( $elm->value == $ef_hour ) {
$ef_hour_val = $elm->name; //
break;
}
}
Please Log in or Create an account to join the conversation.
- Mihail Semerdzhiev
- Offline
- New Member
- Posts: 12
You save my day!
Jerry wrote: Probably solved?! This is for other people looking for the same thing. That is, if someone can please confirm whether this is the right way to do it...
Recap of the problem: I need to access the extra fields to set the title of an item.
For a readable title, I need the name of the selected select box item and not the value. So getting the ID value from the $_POST, the code below gets the defined values from the Extra Fields definition.
$ef_hour = $_POST[ "K2ExtraField_2" ]; // ExtraField "Hour" has ID = 2 $ef_hour_val = ""; $query = "SELECT value FROM #__k2_extra_fields WHERE id=2"; $db->setQuery($query); $arr = json_decode( $db->loadResult() ); foreach ( $arr as $elm ) { if ( $elm->value == $ef_hour ) { $ef_hour_val = $elm->name; // break; } }
Please Log in or Create an account to join the conversation.