Joomla plugin : how to get article title and article id - joomla

I have developed on simple plugin in Joomla 1.6
I stuck at : How to get article title and article url.
I am getting no output if tried to print below statement:
echo $article->title;
echo $article->id;
I have written this in php file, not used MVC architecture.
Is there any other settings need to do in Joomla admin side ?
Please suggest your pointers for extracting article title and article url.
Thanks in advance!
Pravin

In order to get the article ID you have to write the following:
echo JRequest::getVar('id');
For the title, you just take the id you got, load the article object
$blabla = $article->load(ID);
echo $blabla->get('title');

It seems JRequest is deprecated in 2.5 & 3.x as indicated in the Deprecated Elements list.
I would rather use the following:
$article_id = JFactory::getApplication()->input->get('id');

i tried :
public function onContentPrepare($context,&$article, &$params, $limitstart) {
echo JRequest::getVar('id');
}
Still I am not getting the id. Is this right?
The article is loaded in your second argument ($article). Being on this event (onContentPrepare), the only property you can access is $article->text.
For suiting your purpose (getting the article id and title) you will want to use another event, called "onContentBeforeDisplay".
public function onContentBeforeDisplay($context, &$article, &$params, $limitstart)
Here you have (again) the article passed through the second argument, but now you have access to properties like $article->id, $article->title and many others.
For future references on content events, take a look at the file "plugins\content\example\example.php"

You can use for getting active article title like this
$menu =& Jsite::getMenu(); echo $menu->getActive()->title;
may this help.

To get Joomla article id use this...
<?php echo JRequest::getVar('Itemid'); ?>
In the previous answer someone used id instead of Itemid. Hope this helps!

Related

Joomla 1.5 to 2.5 - how to get group names of component settings

I am upgrading a Joomla Website from 1.5 to 2.5 and I have a problem with a custom component which gets the setting params from the JEvents component. The old source tries to get the group names of these settings like this:
$groups = $this->params->getGroups();
But this does not work with Joomla 2.5.
I didn't find useful information on Google, so maybe you can help me out with this.
Thank you very much!
If it's simply the parameters from JEvents that you want to retrieve, then you can use the following:
$app = JFactory::getApplication('site');
$params = $app->getParams('com_events');
$var1 = $params->get('paramName1');
$var2 = $params->get('paramName2');
$var3 = $params->get('paramName3');
Hope this helps
Solved it by myself.
I changed the component input to a Form instead of Parameters and bound the Parameter Data in a JRegistry to the form.
The view works with fieldsets as groups.

Echo Joomla K2 Item Title in Main Template Using PHP

I'm trying to simply echo the title of the current k2 item I'm viewing, but the echo will not occur within the K2 template, it should show in my MAIN site template.
I tried this:
<?php echo $this->title; ?>
But that displays the FULL site title including my company name because I have it set that way in the main Joomla configuration.
I don't want the full site title that is generated for the 'title' tag in the head of the website; I just want to generate the name of the specific item I am currently viewing. This is probably pretty easy, but my PHP knowledge is limited.
I realise this is pretty old. However if you are still looking for a solution. This is not very elegant but will do what you want.
Firstly, just in case you weren't aware, you can get the article id from anywhere in by doing a JRequest:getVar('id') which will use a GET request to get the id from the URL.
$id = JRequest::getVar('id');
$id = explode(':',$id);
echo $id[0];
The reason I am exploding it is because in my site I am using aliases which if you are and you echo $id without you will see its in the form id:alias.
Using this method, you could query the database to get the name associated with that id in the k2_items table. You could create a function to do that which is else where in the templates folder then just assign it to a variable which you echo in the template. That keeps your template clean but gives you what you want.
Like I say, not a quick elegant solution but will work.

Magento Tables and how they work with the database

I been trying to understand Magento and I read many things on the wiki, but I couldn't figure out How does Magento works with database tables? because I didn't see any SQL
I would reccomend read over this blog post from Alan Storm:
http://alanstorm.com/magento_models_orm
He explains quite abit about the Magento ORM system, and in my opinion that entire site is a great resource for any Magneto developer.
If you watch your MySQl log, the calls made by magento can sometimes be 500 lines long or longer ... These calls are dynamically constructed using XML files. The best way to manipulate Magento data manually is to use MAGE:: calls or use a direct database connection by using:
$read = $resource->getConnection('core_read');
$sql = "select * from [YOUR_TABLE] where 1 limit 1";
$result = $read->query($sql);
It's either that or calls that look like:
$value = 'some value';
$item->setData('some_key', $value);
$item->save();
Magento is object oriented, so those are the most commonly accepted and used ways to retrieve/set data in Magento. I hope that helps.
Read chapter 5 onwards from the knowledge base.
You are not really asking a question so no one can help on the specifics, I always find that you learn best by doing, I find the best way to mess around with magento is to create a test.php file in shell/ with the following: (for example)
<?php
require('abstract.php');
class Test extends Mage_Shell_Abstract
{
function run(){ //call your functions here
echo 'running ..';
$this->database();
}
function database() { //you can create as many functions as you like
$entityId = '4449'; //product id
$product=Mage::getModel("catalog/product")->load($entityId);
var_dump($product->getAttributeText('size'));
}
}
$test = new Test();
$test -> run();
Then you can run from console:
php test.php
and it returns in my example
running ..string(11) "Extra Large"
Hope this helps you, next time be more specific.

Request Variables in Joomla

I am new to Joomla. I am trying to write a module which would fetch and display the contents from database.
Now for fetching the contents from database, I need to get the current article id and category id of the current article.
I am able to get the id with article title append using JRequest::getVar('id');
ex : 16:abc-def-article-title.
I can sub-string the id out of it but I think there would be a better place of achieving it ?
And also I need to get the category id as well.
Also please let me know how can I see what all variables does JRequest::getVar(') holds.
Many Thanks !!
First, try extract article id by method getInt():
JRequest::getInt('id', 0);
It's more safely, then getVar().
Then, to get category id, you can use this code in module file:
jimport('joomla.application.component.model');
$articlesModel = JModel::getInstance('ContentModelArticle');
$articleId = JRequest::getInt('id', 0);
$article = $articlesModel->getItem($articleId);
$categoryId = $article->catid;
Using Joomla! built-in models instead direct SQL queries is best way to understanding Joomla architecture and writing safety applications that will be easily maintained in the future.
Note: code samples are actually only for Joomla 2.5 (probably and for 1.6, 1.7, but i don't shure).

Page-specific logic in Joomla

I am trying to enable JavaScript in a Joomla template to behave differently depending on the page. In particular, I have set the Key Reference as that appears to be the most appropriate value I could find for this purpose. Unfortunately, I can't seem to access it in my code. I tried:
$this->params->get("keyref")
and a few other variations, but they simply returned a blank. How can I retrieve this value or is there a better way of writing page specific logic.
Related Articles
Joomla load script in a specific page: This would work, but seems like overkill for what I want to do here.
Each page can be given an alias. We can retrieve the alias using code from the forum:
function getCurrentAlias()
{
$menu = &JSite::getMenu();
$active = $menu->getActive();
return $active->alias;
}
We can then inject this into the Javascript:
var alias= '<?php echo getCurrentAlias(); ?>';
I'm not aware of keyref, but I would solve it by using the class suffix parameter you can set for each menu entry.see I would use a space in front of this suffix. With javascript you can then try to read this classname (suffix without the space) on each page.
getElementsByClassName("mysuffix");
for example
If this returns multiple objects, you know on which page you are. Will that help you?

Resources