DigitalMediaGuy
DigitalMediaGuy

Reputation: 421

How can I pull k2 items into my custom components view?

I am working on a custom joomla component and have a single view setup. I am trying to figure out how to pull a list of k2 items into the view based on a few custom filters...

In weird english it would something like this:

Get all k2 items that_match_some_requirements into my custom components view where user_id = this user

It would be nice to be able to reference the item data normally like so:

$this->item->info

I am really just trying to understand / figure out the best way to import k2 item's and their object into my components view. If that makes sense?

Maybe something with this? Although I feel the name indicates otherwise :-/

JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_component/models'); 
$whateverModel = JModelLegacy::getInstance('something', 'something'); //? not sure  

or maybe from this module code?

require_once (JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'helpers'.DS.'route.php');
require_once (JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'helpers'.DS.'utilities.php');

class modK2ContentHelper
{

public static function getItems(&$params, $format = 'html')
{

    jimport('joomla.filesystem.file');
    $mainframe = JFactory::getApplication();
    $limit = $params->get('itemCount', 5);
    $cid = $params->get('category_id', NULL);
    $ordering = $params->get('itemsOrdering', '');
    $componentParams = JComponentHelper::getParams('com_k2');
    $limitstart = JRequest::getInt('limitstart');

Thank you!

Upvotes: 2

Views: 2357

Answers (1)

Shaz
Shaz

Reputation: 2647

I think it's easier your second option. Actually the whole process of getting K2 items with specific conditions is specified in the helper.php file.

In line 261:

            $items = $db->loadObjectList();

The objects are loaded after the query has been created (the code is too long to be pasted here).

In the next lines you can see how a lot of item properties are created, modified or generated (300 lines of code).

At the end you get an array of items (php objects):

                ...
               $rows[] = $item;
        }

        return $rows;
        ...

You could just take this file and reuse the code, or even call it from your component assuming always that the module is installed.

Upvotes: 1

Related Questions