- Posts: 34
COMMUNITY FORUM
SOLVED: How to access extra fields in tag template
- ljk
-
Topic Author
- Offline
- Junior Member
I have been using K2 on a Joomla 1.5 site and now am upgrading to Joomla 3.x. When I needed to access the extra field values in one of the template pages I had a foreach loop that walked thru the extra fields until I found the one I needed and got its value. This worked from categories, items, and tag templates. Now with the new Joomla 3 version of K2 the categories and items templates have the same structure as they did in Joomla 1.5 and I can get the values that I need. But for some reason the tag structure is totally different for the extra fields.
For categories and items the extra fields structure looks like this:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Street Address
[value] => 602-1888 York Avenue
[type] => textfield
[group] => 2
[published] => 1
[ordering] => 1
[alias] => StreetAddress
)
[1] => stdClass Object
(
[id] => 2
[name] => Price
[value] => 389000
[type] => textfield
[group] => 2
[published] => 1
[ordering] => 4
[alias] => Price
) ...
But on the tags page the structure looks like this:
[{"id":"1","value":"602-1888 York Avenue"},{"id":"2","value":"389000"},...
I need to get the 'Price' from the extra fields which is: 389000. So id 2's value. How to I code this so I can get out the value of id 2?
I tried:
[code
]foreach ($fields as $key=>$extraField):
if ($extraField->id == '2') {
$price = $extraField->value;
endforeach;
[/code]
But that isn't working, it gets an error:
Warning: Invalid argument supplied for foreach()
Any help would be appreciated.
Thank you.
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
You can use this snippet to update your template's code: getk2.org/community/New-to-K2-Ask-here-first/171284-Where-to-edit-K2-after-Item-Fields
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- ljk
-
Topic Author
- Offline
- Junior Member
- Posts: 34
Using
$this->item->extraFields->Price->value
I need to be able to get at the extra fields in tag.php.
Any other suggestions?
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- ljk
-
Topic Author
- Offline
- Junior Member
- Posts: 34
I think there is a bug in tags in Joomla 3 version as $items doesn't seem to have the same structure as any of the other template files.
Here is the code I have tried in tags.php:
<?php if(count($this->items)): ?>
<div class="tagItemList">
<?php foreach($this->items as $item): ?>
<?php echo 'price= '.$this->item->extraFields->Price->value;?>
<?php echo 'price= '.$item->extraFields->Price->value;?>
...
In both cases price comes out as blank.
Can any of the developers of this extension confirm that tags is working properly or not on a Joomla 3 site?
Thank you.
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
<?php foreach ($item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value): ?>
...
<?php endif; ?>
<?php endforeach; ?>
Should be used instead
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- ljk
-
Topic Author
- Offline
- Junior Member
- Posts: 34
Please look at tag.php the foreach loop that I pasted is what is in the default tag template starting at line 35 it has:
<?php if(count($this->items)): ?>
<div class="tagItemList">
<?php foreach($this->items as $item): ?>
<!-- Start K2 Item Layout -->
<div class="tagItemView">
If you add a foreach loop inside the items one like so:
<?php if(count($this->items)): ?>
<div class="tagItemList">
<?php foreach($this->items as $item): ?>
<?php foreach ($item->extra_fields as $key=>$extraField): ?>
<?php echo $extraField->value;
<?php endforeach;?>
<!-- Start K2 Item Layout -->
<div class="tagItemView">
Warning: Invalid argument supplied for foreach() on the line that has the second foreach loop to loop through the extrafields.
tag.php just doesn't work like all the other template files. Could a developer look at this problem, try to access the extrafields in tag.php?
Thanks.
Please Log in or Create an account to join the conversation.
- Yiota
-
- Visitor
You cannot just have <?php echo $extraField->value; ?> You need to define the extrafield itself in order to bring the value.
Try with this code
<?php if($item->params->get('tagItemExtraFields',0) && count($item->extra_fields)): ?>
<?php echo $item->extraFields->Price->value; ?>
<?php endif; ?>
Please Log in or Create an account to join the conversation.
- ljk
-
Topic Author
- Offline
- Junior Member
- Posts: 34
$item->params->get('tagItemExtraFields' is zero. Which I gather means that somewhere I don't have it set to show extra fields on tag pages?
The way that I am getting to my tag page is by clicking a tag on the category items pageor on an item details page by clicking a tag. On all my categories I have it set to show extra fields for the category and item layouts, but I don't see anything about tag layout.
On Joomla 1.5 when you clicked a tag in either the category item page or item details page, the extra fields were available in tag.php just like there were in item.php or category-item.php. But with Joomla 3.x the tag.php is not working the same.
This site uses hundreds of different tags, so I can't go setup a menu item for each different tag. if that is how you get the extra fields to display.
Ideas?
Thank you for your help.
Please Log in or Create an account to join the conversation.
- Yiota
-
- Visitor
Please Log in or Create an account to join the conversation.
- ljk
-
Topic Author
- Offline
- Junior Member
- Posts: 34
I have made sure that tags now have Extra Fields enabled in the K2 Parameters.
But
$price = $this->item->extraFields->Price->value;
still is not returning the Price extra field. Price is the alias of the Price field. Still getting out a blank value for Price.
Other ideas?
Thank you for your help.
Please Log in or Create an account to join the conversation.
- Yiota
-
- Visitor
$price = $item->extraFields->Price->value; and not $price = $this->item->extraFields->Price->value;
Please Log in or Create an account to join the conversation.
- ljk
-
Topic Author
- Offline
- Junior Member
- Posts: 34
Thank you so much, that gave me the price field.
Please Log in or Create an account to join the conversation.