Hi Alex,
I still have not solved my issue. I have gotten really far, so far i have set up two objects, as it seamed like it worked the best. When trying with one object i did get it to work however both actions were triggered when loading the same URL when infact the should be triggered at two different times on the URL.. with the items everything loads correct both url works and the right OG parameters are loaded in the header. But for some reason i get the object error. Below is the code of my plugin.
defined('_JEXEC') or die('Restricted access');
jimport('sourcecoast.articleContent');
jimport('sourcecoast.openGraphPlugin');
class plgOpenGraphDjclassifieds extends OpenGraphPlugin {
protected function init() {
$this->extensionName = "Djclassifieds";
$this->supportedComponents[] = 'com_djclassifieds';
$this->setsDefaultTags = true;
// Define all types of pages this component can create and would be 'objects'
$this->addSupportedObject("Item", "item");
$this->addSupportedObject("Swapitem", "swaps");
// Add actions that aren't passive (commenting, voting, etc).
// Things that trigger just by loading the page should not be defined here unless extra logic is required
// ie. Don't define reading an article
$this->addSupportedAction("Swap", "swap");
$this->addSupportedAction("Want", "want");
$this->addSupportedAction("View item", "View");
}
protected function findObjectType($queryVars) {
// Setup Object type for page
$view = array_key_exists('view', $queryVars) ? $queryVars : '';
$object = null;
$isswaps = 0;
$isswapid = JRequest::getVar('swap_id', '');
if($isswapid != '' && $view == 'item') {
$objectTypes = $this->getObjects('swaps');
$isswaps = 1;
} else {
$objectTypes = $this->getObjects($view);
}
if ($view == 'swaps' || $view == 'item') {
if(count($objectTypes) > 0) {
$object = $objectTypes[0];
return $object;
} else {
return NULL;
}
} else {
return NULL;
}
}
//TODO - clean up when we remove J1.5 support
protected function setOpenGraphTags() {
$desc = ''; //Note: meta is same as blank value, since system plugin attempts to generate from metadescription if no value is found
$image = '';
$view = JRequest::getVar('view');
$task = JRequest::getVar('swap_id', '');
if($this->object) {
$desc_type = $this->object->getParameter('custom_desc_type');
$desc_length = $this->object->getParameter('custom_desc_length');
$image_type = $this->object->getParameter('custom_image_type');
$image_path = $this->object->getParameter('custom_image_path');
}
else {
$desc_type = 'custom_desc_introwords';
$desc_length = '10';
$image_type = 'custom_image_item';
$image_path = '';
}
//Get Description
$itemModel = JModel::getInstance('Item', 'DjclassifiedsModel');
$item = $itemModel->getItem();
$desc = $item->intro_desc;
$this->addOpenGraphTag('description', $desc, false);
//Get image
$imageName = 'components/com_djclassifieds/images/'.str_replace(";","",$item->image_url).'.thb.jpg';
if(JFile::exists(JPATH_SITE.'/'. $imageName))
$image_path = JURI::base() . $imageName;
else
$image_path = '';
$image = $image_path;
$this->addOpenGraphTag('image', $image, false);
}
/************* DEFINED ACTIONS CALLS *******************/
protected function checkActionAfterRoute($action) {
if (JRequest::getVar('view') == 'item' && $action->system_name == 'item') {
//Get item
$theswapid = JRequest::getVar('id', 0, '', 'int');
if($theswapid != 0) {
$db = JFactory::getDBO();
$query = "SELECT i.* FROM #__djcf_items i WHERE i.id=".$theswapid;
$db->setQuery($query, 0, 1);
$theitem = $db->loadObject();
}
//Get Item URL
$url = $this->getItemURL($theitem);
$queryVars = $this->jfbcOgActionModel->getUrlVars($url);
$this->triggerAction($action, $url);
}
if ($action->system_name == 'swap') {
$theswapid = JRequest::getVar('swap_id', '');
$db = JFactory::getDBO();
$query = "SELECT i.* FROM #__djcf_items i, #__djcf_itemsask j WHERE j.swap_id = '".$theswapid."' AND i.id = j.item_id";
$db->setQuery($query, 0, 1);
$theswap = $db->loadObject();
//Get Item URL
$url = $this->getItemURL($theswap);
$url = $url.'&swap_id='.$theswapid;
$queryVars = $this->jfbcOgActionModel->getUrlVars($url);
$this->triggerAction($action, $url);
}
}
private function getItemURL($item) {
$menus = JSite::getMenu();
$menu_item = $menus->getItems('link','index.php?option=com_djclassifieds&view=items&cid=0',1);
$Itemid = $menu_item->id;
//$anch = '#dj-classifieds';
$url = '/index.php?option=com_djclassifieds&view=item&cid='.$item->cat_id.'&id='.$item->id.'&Itemid='.$Itemid.$anch;
$url = JRoute::_($url, true);
$jUri = JURI::getInstance();
$url = rtrim($jUri->toString(array('scheme', 'host', 'port')), '/') . $url;
return $url;
}
}