Keyword

extra fields in related items by tag

More
12 years 5 months ago #65293 by enano
hi all I have the following problem.
I want to show the extra fields of the items related by tag.
$ this-> relatedItems is the array with related items and each item related have a data such as title, category, author, etc..(you can see in com_k2/item.php around line 461) I want to add the extra fields in these item data...
I can not find where are the parameters relatedItems to add the extra fields. I search
componets/com_k2/model/item.php
componets/com_k2/controller/item.php
thank you very much.

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

More
12 years 5 months ago - 12 years 5 months ago #65294 by matthew turner
Replied by matthew turner on topic Re: extra fields in related items by tag
Hi,
I recently needed to do this for a clients website.
They needed the related items to be from another single category only, and needed a way to display the related items in a specific order.... (and not in the category list order, date order, alphabetical or item id or the reverse order of any of these!)
I only went as far as retrieving this specific extrafield from the array but the following might help you out, as it is all done in a template over ride - (so updates to K2 do not wipe out your changes) :


<?php if($this->item->params->get('itemRelated') && isset($this->relatedItems)): ?>

<?php //var_dump($this->relatedItems);
// Sort the data with volume descending, edition ascending["ordering"]
// Add $data as the last parameter, to sort by the common key
//$mynewOrder = array_multisort($item->id, SORT_ASC, $this->relatedItems);
//$mynewOrder = asort($this->relatedItems->item->id);
//$mynewOrder = array_multisort($item->id, SORT_ASC, $this->relatedItems);
//array_multisort($this->relatedItems,SORT_DESC );

?>
<?php
foreach ($this->relatedItems as $val)
{
$sortArrayRank[] = $val->ordering;
$MYvalobj[] = $val->extra_fields;
}

array_multisort(array_values($MYvalobj), $this->relatedItems);
?>

<!-- Related items by tag -->
<div class="itemRelated">
<h2>Testimonials</h2>
<ul>
<?php foreach($this->relatedItems as $key=>$item): ?>
<?php // if item not from category 4 check routine start ?>
<?php if($item->catid == 4): ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?>">


<?php
$MYobj = $item->extra_fields;
$obj = json_decode($MYobj);

echo "<h1> ";
echo $obj[0]->value;
echo "</h1><br />";

?>
</li>
<li> ....</li>
etc....
<?php endif;// if item not from category 4 check end ?>
</ul>

Hope this helps point you in the right direction - I will be doing further work with this method and will post other changes.....
Regards
Mat

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

More
12 years 5 months ago #65295 by enano
Replied by enano on topic Re: extra fields in related items by tag
hi all ... mat thanks for reply .... i don't knew about json_decode now I have the extrafields of related item in an array but I still have a problem ...
the data in extra fields are $ extraField-> id, $ extraField-> value the problem is I need the extra field name not the id .... I was thinking to adding a new function in ITEMLIST model which can bring the names of extra fields but I can not display the data in the view does not really understand much that implements the mVC in k2
sorry for my english isn't my native language
thanks for the help! : D

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

More
12 years 5 months ago - 12 years 5 months ago #65296 by matthew turner
Replied by matthew turner on topic Re: extra fields in related items by tag
Hi,
I just thought I would try this for you and it works!
Add the next bit of code inside the foreach($this->relatedItems as $key=>$item): part of the item.php template override file...


$MYobj = $item->extra_fields;
$obj = json_decode($MYobj);


$ItemExtrafieldID = $obj[0]->id;

$db = &JFactory::getDBO();
$query = "SELECT *".
" FROM #__k2_extra_fields";

$query .= " WHERE published = 1"
. " AND id = $ItemExtrafieldID";

$db->setQuery($query);
$data = $db->loadObjectlist();



echo "<h1>data->name = ";
echo $data[0]->name;
echo "</h1><br />";

Hope this helps....
Regards
Mat

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

More
12 years 5 months ago #65297 by enano
Replied by enano on topic Re: extra fields in related items by tag
​​thanks mat I'll try the code: D

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

More
12 years 5 months ago - 12 years 5 months ago #65298 by enano
Replied by enano on topic Re: extra fields in related items by tag
thank you very much mat ... works but I think it isn't very advisable to add sql in the view and not respecting the MVC pattern that is why I had thought of adding sql queries in the corresponding model but my level of knowledge does not allow me. .. I will continue looking for ways to integrate in the mvc pattern ... here is the full code
Log in  or Create an account to join the conversation.

More
12 years 5 months ago #65299 by matthew turner
Replied by matthew turner on topic Re: extra fields in related items by tag
Hi,
Yes I agree not adding SQL to the template would be better, but it is not practical to modify core files as the updates will replace changed files. There must always be another way...

I was thinking of another way to do this using the modelItemList - I will get looking again in the morning my current project requires the plugins of the related items to work too!
The related item model definately needs more options in the template!
Regards
Mat

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

More
12 years 4 months ago - 12 years 4 months ago #65300 by Robs
Replied by Robs on topic Re: extra fields in related items by tag
Hi enano,

can you please help me, i have the same problem like you, i need to put in some extrafield in the items, that appear as "related items by tag" underneath an item. Like in your post, i replaced everything between
enano wrote: thank you very much mat ... works but I think it isn't very advisable to add sql in the view and not respecting the MVC pattern that is why I had thought of adding sql queries in the corresponding model but my level of knowledge does not allow me. .. I will continue looking for ways to integrate in the mvc pattern ... here is the full code
Log in  or Create an account to join the conversation.

More
11 years 11 months ago - 11 years 11 months ago #65301 by Annett Richter
Replied by Annett Richter on topic Re: extra fields in related items by tag
Hey,

really thank you for that code-snippets!

I had the "easy" problem to just put out the extrafields in related articles - but not behind each other like normal, different positions.

Started the script after
<?php foreach($this->relatedItems as $key=>$item): ?>

->
<?php 
$extra_fields = json_decode($item->extra_fields); 
$aprextrafields = array();
foreach ($extra_fields as $key => $extraField) {
$aprextrafields[$extraField->id] = $extraField->value;
} ?>

then you easily can call your extrafields by ID where you want:
 <?php echo $aprextrafields[1]; ?>
 <?php echo $aprextrafields[2]; ?>
 <?php echo $aprextrafields[3]; ?>
 <?php echo $aprextrafields[4]; ?>

I gues my missing line was
$extra_fields = json_decode($item->extra_fields);

Thanks,

alpha

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