Keyword

Extra fields in main content area of k2

  • Winston
  • Winston's Avatar Topic Author
  • Offline
  • New Member
More
6 years 8 months ago #163207 by Winston
I'm trying to put the extra fields the content area of my k2 page.

I use sourcerer to put php code the page. So far I've tried the following and they all fail to do anything:

This doesn't do anything because it never goes into the for-loop
{source}
<?php
$extrafields = array();
foreach($item->extra_fields as $item)
{    
$extrafields[$item->id] = $item->value;
echo "<script>alert('$item->value');</script>"; 
} 
?>
{/source}

This doesn't do anything either:
{source}
<?php
$item_extra_fields = json_decode($item->extra_fields);
$put_your_extra_field1_name_here = $item_extra_fields[0]->value;
echo $put_your_extra_field1_name_here;
?>
{/source}

This creates an empty alert:
{source}
<?php
echo "<script>alert('$item->title');</script>";
?>
{/source}

This creates an alert that prints "0":
{source}
<?php
$something = count($item->extra_fields)
echo "<script>alert('$something');</script>";
?>
{/source}

If I user $this->item->whatever, the page fails to load altogether.

Any help?

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
6 years 8 months ago #163213 by Krikor Boghossian
Replied by Krikor Boghossian on topic Extra fields in main content area of k2

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

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

  • Winston
  • Winston's Avatar Topic Author
  • Offline
  • New Member
More
6 years 8 months ago #163226 by Winston
Replied by Winston on topic Extra fields in main content area of k2
I copied the code, replaced
$this->item->extraFields
with
$item->extraFields
.

And obviously I put in the alias of my extra field as well into the code.

The execution does not go into the if-statement.

One clue might be from my original post:
count($item->extra_fields)
yields a 0.

I tried accessing $item->title and I don't get anything either.

Maybe my problem is accessing $item? In which case, how do i do it from the content area using sourcerer?

Thanks for the suggestion.

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
6 years 8 months ago #163235 by Krikor Boghossian
Replied by Krikor Boghossian on topic Extra fields in main content area of k2
Unfortunately I am not really familiar with sourcerer.
Have you tried using overrides?
getk2.org/documentation/tutorials/174-templating-with-k2-and-the-concepts-of-sub-templates

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

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

  • Winston
  • Winston's Avatar Topic Author
  • Offline
  • New Member
More
6 years 8 months ago #163249 by Winston
Replied by Winston on topic Extra fields in main content area of k2
I haven't tried templating but that's the last resort. Is there any way to put code of any language in the main content area of k2 to access extra fields?

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
6 years 8 months ago #163278 by Krikor Boghossian
Replied by Krikor Boghossian on topic Extra fields in main content area of k2
Apart from templating which is the most efficient way, a custom module which displays the item's data would be a solution.

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

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

  • Winston
  • Winston's Avatar Topic Author
  • Offline
  • New Member
More
6 years 8 months ago #163393 by Winston
Replied by Winston on topic Extra fields in main content area of k2
So I tried custom modules and I can't access $this->item. The site seizes when I use that in the module's php code.

Tried using $item and just like when I added code into k2 content itself, count($item->extra_fields) is 0...

Any ideas before I have to go to template overrides?

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
6 years 8 months ago #163396 by Krikor Boghossian
Replied by Krikor Boghossian on topic Extra fields in main content area of k2
In order to access $this you would need to query the db, select the item based on the id and load the data.

At this point overrides seem to be way more simple.

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

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

More
6 years 8 months ago #163408 by william white
Replied by william white on topic Extra fields in main content area of k2
This module www.bnrbranding.com/component/ars/repository/k2-bnr-content.html
will show you how to place the extra fields inside the content area of a k2 item if you use {loadposition ...} to place the module into the content

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

  • Winston
  • Winston's Avatar Topic Author
  • Offline
  • New Member
More
6 years 8 months ago #163415 by Winston
Replied by Winston on topic Extra fields in main content area of k2
Thanks William and Krikor.

The sample code from bnr is very useful. And i see how template override could be much easier. I'll try both methods soon.

Thanks again for you guys' help!

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


Powered by Kunena Forum