Keyword

Extrafields in K2ViewItem for Show vs Hide

  • dtronvig
  • dtronvig's Avatar Topic Author
  • Offline
  • New Member
More
9 years 11 months ago - 9 years 11 months ago #128485 by dtronvig
I might easily be confused, but it seems that when a K2 Item is set to Show Extra Fields, then at least in item.php the extra_fields show up in the K2ViewItem Object in a form like:
[extra_fields] => Array
(
  [1] => stdClass Object
  (
     [id] => 2
     [name] => Image URL
     [value] => images/Music/BBKingMontreaux360x540.jpg
     [type] => textfield
     [group] => 1
     [published] => 1
     [ordering] => 2
     [alias] => ImageURL
  )
  .
  .
  .
but when a K2 Item is set to Hide Extra Fields, the extra_fields show in th K2ViewItem Object in a form like:
[extra_fields] => [{"id":"1","value":""},{"id":"2","value":"images\/Music\/BBKingMontreaux360x540.jpg"} . . .
I really didn't see that difference coming, and it took me a while to figure that out.

Now, this is in a Gavick template, so if it's something peculiar to the Gavern Framework I suppose I could talk to them, but this group is likely to have a better handle on this in any case.

I'm using this generic code to load extra_fields values into an array:
<?php  
  $extrafields = array();
  foreach($item->extra_fields as $field)
  {    
     $extrafields[$field->id] = $field->value;
  }   
?>
In [template]/html/mod_k2_content/Default/default.php this works just fine, whether the Item is set to Show or Hide Extra Fields, and I haven't really looked at the Object to see if the structure changes in that context.

In [template]/html/com_k2/templates/default/item.php this code only works when the Item is set to Show Extra Fields, but -- not surprisingly -- that code doesn't find anything when the Item is set to Hide -- when of course the Extra Fields are delivered in a completely different form.

So regardless of whether this is standard K2 behavior or something peculiar to the Gavern Framework I still have to work around it. So:

Is there some code that would work on both forms of extra_fields?

To bypass this entirely, could I ask for a quick bit of code to extract and parse the extra_fields directly from the database, given the Item ID?

Many thanks,
Drew

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

More
9 years 11 months ago #128486 by Lefteris
Replied by Lefteris on topic Re: Extrafields in K2ViewItem for Show vs Hide
Hi. What you get when extra fields are set to "Hide" is the plain database value. In that case you will need to call the K2 functions to render them before you can use them as an array in your loop. Try this:
<?php
$model = K2Model::getInstance('Item', 'K2Model');
$item->extra_fields = $model->getItemExtraFields($item->extra_fields, $item);
?>

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

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

  • dtronvig
  • dtronvig's Avatar Topic Author
  • Offline
  • New Member
More
9 years 11 months ago - 9 years 11 months ago #128487 by dtronvig
Replied by dtronvig on topic Re: Extrafields in K2ViewItem for Show vs Hide
Thanks Lefteris.

Well, if in some cases I need to parse the database value, then it seems like I might as well just go straight to the database all the time. Could I hit you up for a quick example of the code to get the extra_fields from the database to a PHP array? ('cause it would take me a few iterations to get it all working right.)

Thanks again,
Drew

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

More
9 years 11 months ago #128488 by Lefteris
Replied by Lefteris on topic Re: Extrafields in K2ViewItem for Show vs Hide
If you have the item, extra fields should be there most of the times, either as an array or as JSON string. In the second case you just need to add the code i provided before applying yours. In case you need it, the database field is named "extra_fields".

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

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


Powered by Kunena Forum