getting article (by Id) intro text into a custom module

60 views Asked by At

I'm developing a custom module in joomla 4 which needs to access the introtext of a specific article whose Id is known.

I'm looking for a code snippet that follows the latest coding standards and is most likely to continue to work properly when joomla 5 appears rather than something jotted down on the back of an envelope.

have not tried anything yet so nothing has happened yet.

1

There are 1 answers

3
Kevin On

You can get the introtext from _content table with sql query

/**
 * Get the introtext of an article
 * @param int $articleID
 * @return string the introtext
 */
function getArticleIntrotext($articleID) {
    $db = Factory::getContainer()->get('DatabaseDriver');
    // basic sql query... SELECT introtext FROM #__content WHERE id = $articleID
    $query = $db->getQuery(true)
        ->select($db->quoteName('introtext'))
        ->from($db->quoteName('#__content'))
        ->where($db->quoteName('id') . ' = ' . $articleID);
    $db->setQuery($query);
    $introtext = $db->loadResult();

    return $introtext;

}