Keyword

Display specific extra field in module position ?

  • Uldis
  • Uldis's Avatar Topic Author
  • Offline
  • Senior Member
More
12 years 7 months ago - 12 years 7 months ago #103145 by Uldis
Hello,

I've been looking around forum for a solution, but it seems that this question hasn't been answered yet.

I'm looking for ways to display content of specific extra field in a module position.

I found this in k2 tips - link

So I tried using this code
Log in  or Create an account to join the conversation.

More
12 years 7 months ago #103146 by william white
Replied by william white on topic Re: Display specific extra field in module position ?
this getk2.org/extend/extensions/date/item/632-k2-split-content
does something similar to what you want very nicely.
You may be able to override it to show only what you want, or get the code from it to link a module with the current item

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

  • Uldis
  • Uldis's Avatar Topic Author
  • Offline
  • Senior Member
More
12 years 7 months ago #103147 by Uldis
Thank you for a fast reply.

I was just editing: my first post.

I'm actualy already using k2 split content to display 1 extra field in module position, but I need to display another extra field in diferent module position, k2 split content can only display all extra fields or none.

I tried using code mentioned above in k2 split content override as well, but that didn't work out for me.

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

More
12 years 7 months ago #103148 by william white
Replied by william white on topic Re: Display specific extra field in module position ?
Although i havent tried it, there is a box in the split content module to select az subtemplate.
You should be able to create a subtemplate for it, and change the foreach loop that displays extra fields to do what you want, and select the subtemplate in the module instance

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

  • Uldis
  • Uldis's Avatar Topic Author
  • Offline
  • Senior Member
More
12 years 7 months ago #103149 by Uldis
I tried doing that, but for some reason using this code
Log in  or Create an account to join the conversation.

More
12 years 7 months ago #103150 by william white
Replied by william white on topic Re: Display specific extra field in module position ?
You could try just using the built in foreach loop that starts on line 83 of default.php in the split content override
<?php foreach ($item->extra_fields as $extraField): ?>
<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 endforeach; ?>

and insert a condition to only print out the extra field you want

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

More
12 years 7 months ago - 12 years 7 months ago #103151 by william white
Replied by william white on topic Re: Display specific extra field in module position ?
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; ?>

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

  • Uldis
  • Uldis's Avatar Topic Author
  • Offline
  • Senior Member
More
12 years 7 months ago #103152 by Uldis
William,

thank you very much this is exactly what I wanted to achieve.

I don't know php yet, but I want to learn it very much.

Thank you for providing code I needed !

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

More
12 years 6 months ago - 12 years 6 months ago #103153 by Steven Johnson
Replied by Steven Johnson on topic Re: Display specific extra field in module position ?
Thanks so much for posting the foreach loop. I think this gets me on the right track.

What does the php code look like to insert a condition to print out a specific field?

Thanks! -- Steven

I am on Joomla 2.5.7 and K2 2.5.7


EDIT
After reading this post, I do not think this is 100% my situation. I am trying to display the k2 content module on the home page. In this module I want to display a single custom field for each of the different items

When I and the code to prepare extra field
Log in  or Create an account to join the conversation.

More
12 years 6 months ago #103154 by william white
Replied by william white on topic Re: Display specific extra field in module position ?
If you are using mod_k2_content you should create a subtemplate for the module and edit the built in foreach loop in default.tmpl/default.php to either display the field you want or hide the ones you do not want

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

More
12 years 3 months ago - 12 years 3 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
12 years 3 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
12 years 3 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
12 years 2 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
12 years 2 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
12 years 2 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
12 years 22 hours ago - 12 years 22 hours 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 11 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 8 months ago #103163 by Tim
Log in  or Create an account to join the conversation.

More
11 years 7 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.