5

currently I've got Jreviews installed and I'd like to replace it by K2 to list specialized shops with addresses, phones, maps, opening hours ...

With K2 I guess I'll need to define extra custom fields to hold those specific information. No problem.

But, how may I configure things to have those fields displayed in the detailed article/items for a specific shop ?

Many thanks,

Tibi.

ktharsis
  • 3,160
  • 1
  • 19
  • 30
Tibi
  • 435
  • 1
  • 3
  • 16

5 Answers5

6
// In the item template you can skip this first line...
$this->item->extra_fields = K2ModelItem::getItemExtraFields($this->item->extra_fields);

$extraFlds = array();
if ( $this->item->extra_fields ){
  foreach ( $this->item->extra_fields as $key=>$extraField ){
     $extraFlds[ $extraField->name ] = $extraField->value;
  }
}

Then you can access your extra fields in the associate array like $extraFlds['my field']

Hari Honor
  • 8,677
  • 8
  • 51
  • 54
  • This code was super useful, thanks! For anyone planning to copy+paste like I did, notice that in the last line of code $extarFlds should be $extraFlds. – Victoria Ruiz Oct 12 '12 at 01:37
  • 2
    k2 seems to have updated their class - the object needs to be instantiated now: `$k2obj = new K2ModelItem(); $fields = $k2obj->getItemExtraFields($this->item->extra_fields, $this->item);` – murraybiscuit Apr 24 '13 at 06:38
  • Its not working for me now. I am getting `WARNING: CREATING DEFAULT OBJECT FROM EMPTY VALUE` error. Is this not valid for Joomla 3.3 and K2 v2.6.7? – Neel Jun 05 '14 at 17:36
  • btw, I am trying this in category.php – Neel Jun 05 '14 at 17:38
  • Sorry.. my bad. I need to do the edits in category_item.php and not category.php – Neel Jun 05 '14 at 18:00
4

After a lot of tries here what i used and worked for me

<?php 
// if form is empty show default form
$k2obj = new K2ModelItem(); 
$fields = $k2obj->getItemExtraFields($this->item->extra_fields, $this->item); 
//echo $this->item->extraFields->State->name; 
echo $this->item->extraFields->FIELD_ALIAS->value;

?>

This is working and noted its all pegged to instantiating the class.

Note: I am using this in the k2 item i version 2.6.7 Joomla 2.5.14

madth3
  • 7,275
  • 12
  • 50
  • 74
J Mathenge
  • 101
  • 1
  • 3
1

The problem is that $this->item->extra_fields is actually a JSON string retrieved from the database, so you have to decode it first. It's structure is rather complicated (and unfortunately each field is labelled by it's id, it's name doesn't appear at all), you'll see it if you execute:

print_r($this->item->extra_fields);`

If you want to call field values by it's field name I'd do it like this:

if ($this->item->params->get('itemExtraFields')) {

$item_extra_fields = json_decode($this->item->extra_fields);

$put_your_extra_field1_name_here = $item_extra_fields[1]->value;
$put_your_extra_field2_name_here = $item_extra_fields[2]->value;
$put_your_extra_field3_name_here = $item_extra_fields[3]->value;
$put_your_extra_field4_name_here = $item_extra_fields[4]->value;
}

Notice that this is useful if the extra field you need is text, but it can be an array or whatever so you might have to code a little bit more. Hope this is useful!

1

if you want show custum field in k2 table list go to:

components\com_k2\templates\default\category_item.php

and edit file near line 136 like this:

<?php foreach ($this->item->extra_fields as $key=>$extraField):
            **if(strpos($extraField->name,"/")){**
            ?>
            <li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
                <span class="catItemExtraFieldsLabel"><?php echo $extraField->name; ?></span>
                <span class="catItemExtraFieldsValue"><?php echo $extraField->value; ?></span>
            </li>
            <?php **}** endforeach; ?>

i do that in my site: www.joomir.com

Sotiris
  • 38,986
  • 11
  • 53
  • 85
0

In K2 you set the parameters for how an item displays at the category level. There is an option to display the extra fields in both Item view options in category listings as well as the Item view options.

By default, the built in K2 template will display the extra fields under a heading "Additional Information" with an unordered list of field name and values. You can override that template and make the extra fields display any way you like.

Brent Friar
  • 10,588
  • 2
  • 20
  • 31