Keyword

SOLVED: How to access extra fields in tag template

  • ljk
  • ljk's Avatar Topic Author
  • Offline
  • Junior Member
More
10 years 9 months ago - 10 years 8 months ago #122056 by ljk
Hi,

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
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
10 years 9 months ago #122057 by Krikor Boghossian
Replied by Krikor Boghossian on topic Re: How to access extra fields in tag template
You can directly render specific extrafields.

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
  • ljk's Avatar Topic Author
  • Offline
  • Junior Member
More
10 years 8 months ago #122058 by ljk
Hi,

Using
$this->item->extraFields->Price->value
works great in item.php but it does not work on tag.php.

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
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
10 years 8 months ago #122059 by Krikor Boghossian
Replied by Krikor Boghossian on topic Re: How to access extra fields in tag template
Be careful where $this->item and $item are being used.

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

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

  • ljk
  • ljk's Avatar Topic Author
  • Offline
  • Junior Member
More
10 years 8 months ago #122060 by ljk
Hi,

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
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
10 years 8 months ago #122061 by Krikor Boghossian
Replied by Krikor Boghossian on topic Re: How to access extra fields in tag template
<?php foreach($this->items as $item): ?> is not correct.
<?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
  • ljk's Avatar Topic Author
  • Offline
  • Junior Member
More
10 years 8 months ago #122062 by ljk
Hi,

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">
You get this error:
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
  • Yiota's Avatar
  • Visitor
10 years 8 months ago #122063 by Yiota
Hello,

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
  • ljk's Avatar Topic Author
  • Offline
  • Junior Member
More
10 years 8 months ago #122064 by ljk
Hi,

$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
  • Yiota's Avatar
  • Visitor
10 years 8 months ago #122065 by Yiota
The settings for which elements are going to show in your tag pages are located in the K2 Parameters. You will have to check them first and see that the extra fields are enabled and then see if the the extra field is showing.

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


Powered by Kunena Forum