2

In a plugin I need to get the Title (from Article or Menu or Modules) from a given ID. I am using the following code and it's working but I would like to know if it is correct doing this way or is a better/more proper way of doing it. Thank you.

$id      // ID from menu, article or module
$context // could be menu, content or module

$apk = JTable::getInstance($context);
$apk->load($id);
$title = $apk->get('title');

1 Answer 1

4

You could try something like the following:

public function onYOURFUNCTION($context, &$article, &$params)
{
    $id   = JFactory::getApplication()->input->getInt('id');
    $load = $article->load($id);

    echo $load->get('title');
}

or if you want to go down to JTable route:

public function onYOURFUNCTION($context, &$article, &$params)
{
    $input = JFactory::getApplication()->input->getInt('id');
    $table = JTable::getInstance('content');
    $table->load($id);

    echo $table->get('title');
}

Hope this helps

3
  • Thank you! Is there any advantage from one method to another?
    – n.h.
    Dec 13, 2016 at 11:32
  • @n.h. - I believe in your case the first method would be best as the &$article object can be easily accessed as it already exists. JTable is an abstract class that allows selecting, updating and delete data from the database at ease, however you as mentioned above, you probably won't need it. I'm not a backend developer so I could be wrong about that. Perhaps someone else can elaborate.
    – Lodder
    Dec 13, 2016 at 11:40
  • @Lodder JTable is more suitable if you need to access article params outside the article view. For example to get article image within template index.php and use it as a hero. In this case the first function would return null.
    – Dan
    Feb 13, 2019 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.