Keyword

Display specific extra field in module position ?

More
11 years 10 months ago - 11 years 10 months ago #103155 by danny leary
Hey Guys,

I think i have the solution for using custom extrafields from k2 inside the k2 content module.

The instructions given here do work

jbeginner.com/tutorials/extensions/how-to-display-k2-extra-fields/

but with a slight adjustment to the code to start the call. this following code should work.

<?php
//convertArray to use ids as key
$extrafields = array();
foreach($item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
?>

Basically, just removed the $this-> in the code given at the above url. the code given at the above url works great for the content items and templates, but for some reason breaks the site with a white screen in sub-templates created for the k2 content module.

This code should work. tested in a joomla 2.5.7 installation. the echo code given in the above example should work as well after the modified call code to actually show extra field data via id number.

Best Regards,
Danny
shoutoutadvertising.com

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

More
11 years 10 months ago #103156 by Lefteris
@Danny

Since K2 2.6.2 you can render an extra field using it's alias. Read the release announcement for more information getk2.org/blog/item/1068-k2-v262-now-available

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

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

More
11 years 10 months ago #103157 by danny leary
thanks for the update :) That is nice. I'll have to acquiant myself with this method.

one question that i couldnt' ascertain from the article.... will this method work for module sub-templates too?

those using older versions of k2 with lots of hacks might still be able to make use of the method discussed earlier.

thanks,
danny

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

More
11 years 9 months ago #103158 by Earl
I've managed to get this working in the item.php page here: detroitrevival.org/links/latest

However, when trying to get it to work in a module using the same code, I get the following error:

Fatal error: Using $this when not in object context in /home/totalinf/public_html/detroitrevival.org/modules/mod_k2_content/tmpl/Quotes/default.php on line 85

(The first line in the code below is line 85)

Here is the code that I am using in the default.php file here lines 83-98: /modules/mod_k2_content/tmpl/Quotes (please note that I created a sub-template):
<?php if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)): ?>
          <!-- Item extra fields -->
                <div class="mb-wrap mb-style-1">
                <div class="mb-thumb"></div>
                
                        <blockquote class="type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
                                                <p><?php echo $this->item->extraFields->k_quote->value ?> </p>
                                        </blockquote>
                    <div class="mb-attribution">
                                                <p class="mb-author"><?php echo $this->item->extraFields->q_author->value ?> </p>
                        <cite><?php echo $this->item->extraFields->q_citation_source->value ?></cite>
                                        </div>                        
        
                </div>
            <div class="clr"></div>
          </div>
          <?php endif; ?>

What I am trying to do is display the latest content (1 item) from the 'Quote' K2 category, in a module (basically ONLY the extra fields display in a module). The content is entered in the Extra Fields in the K2 item.

I've tried the solutions mentioned above but I'm not having any luck.

Thanks in advance for your wisdom :-)

Respectfully,
EJ Lear

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

More
11 years 9 months ago #103159 by Earl
Danny,

Was your suggestion and the code on your site only valid pre 2.6.2?

I still can't get this to work, very frustrating :-(

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

More
11 years 9 months ago #103160 by Peter Grube

William White wrote: Code that works in the split content override below. You can also use the | (or) if you want to show more than one extra field and can specify it by name

<?php foreach ($item->extra_fields as $extraField): ?>
 <?php if($extraField->name == "ExtraFieldName"): ?>
 <li class="type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
 <span class="moduleItemExtraFieldsLabel"><b><?php echo $extraField->name; ?></b></span>
  <span class="moduleItemExtraFieldsValue"><?php echo $extraField->value; ?></span>
     <br class="clr" />
    </li>
 <?php endif;?>
 <?php endforeach; ?>


Thx heaps William this code works perfectly in the K2 Content mod joomla 2.5.9 & k2 2.6.5, I would like to ask 1 question as my PHP skills are very basic, I tried the OR method it seems to be working but could you let me know if I got the code right, it seemed to easy to me.
Log in  or Create an account to join the conversation.

More
11 years 7 months ago - 11 years 7 months ago #103161 by Theofanis Valmas
Replied by Theofanis Valmas on topic Re: Display specific extra field in module position ?
Hello since k2 2.6.2 you can use extra fields as alias but only in item.php in order to use it in MODULE k2 content you should add the following code after the:

<?php foreach ($items as $key=>$item): ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; if(count($items)==$key+1) echo ' lastItem'; ?>">

(around line 25 in the mod_k2_content/tmpl/default.php)

<?php
if (!isset($item->extraFields))
{
$item->extraFields = new stdClass;
}
foreach ($item->extra_fields as $extraField)
{
$tmpAlias = $extraField->alias;
$item->extraFields->$tmpAlias = $extraField;
}
?>

now you can type any extra field as this:

$item->extraFields->EXTRA_FIELD_ALIAS->value

or

$item->extraFields->EXTRA_FIELD_ALIAS->name



(the code is from components/com_k2/models/item.php not mine)


this gives you the object to use with alias as you did in item.php

hope it helps!

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

  • Uldis
  • Uldis's Avatar Topic Author
  • Offline
  • Senior Member
More
11 years 6 months ago #103162 by Uldis
Dekari,

Thank you very much, for suggesting this, it has helped me a lot !!!

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

More
11 years 3 months ago #103163 by Tim
Log in  or Create an account to join the conversation.

More
11 years 2 months ago #103164 by Jonathan Roza
Replied by Jonathan Roza on topic Re: Display specific extra field in module position ?
Hi everyone.

Using J.2.5.14 and K2 2.6.6.

This works for me on default.php on mod_k2_content on a template override.

=============================================

<?php
if (!isset($item->extraFields))
{
$item->extraFields = new stdClass;
}
foreach ($item->extra_fields as $extraField)
{
$tmpAlias = $extraField->alias;
$item->extraFields->$tmpAlias = $extraField;
}
?>

<div class="moduleItemExtraFields"><span class="moduleItemExtraFieldsValue"><?php echo $item->extraFields->EXTRAFIELD_ALIAS->value; ?></span>

=============================================

This is right after plugins/k2-plugins Before Display, and itemAuthorAvatar.
It was placed before title, image, everything else.

Dekari, Thank's for the help.

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