Keyword

item -> extra_fields empty in onBeforeK2Save

More
12 years 2 months ago #100843 by Jerry
Hello,

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.

More
12 years 2 months ago #100844 by Lefteris
You can take a look at /administrator/components/com_k2/models/item.php save function to see how to get the extra fields values. However i do not think that you need it. K2 will save the extra fields. You do not need to add code. The code in the plugin needs to be added in case you have defined fields in the plugin XML file.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
12 years 2 months ago #100845 by Jerry
I need to read the extra fields in onBeforeK2Save because I have to set the $item->title. The $item->extra_fields is NULL. So far, I can only read the extra fields from the $_POST and there the names are K2ExtraField_1, K2ExtraField_2, etc., which is not useful in trying to identify them.

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.

More
12 years 2 months ago #100846 by Jerry
And a big problem is this... When an extra field is a select box, the $_POST of course contains the ID, not the VALUE of the selected item.

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.

More
12 years 2 months ago #100847 by Jerry
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.

More
12 years 1 month ago #100848 by Mihail Semerdzhiev
Replied by Mihail Semerdzhiev on topic Re: item -> extra_fields empty in onBeforeK2Save
Thank you, Jerry!

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.


Powered by Kunena Forum