2

In my last question I tried to modify mod_latest_articles module, and added myCustomFunction to ArticlesLatestHelper.php like this :

/**
 * @package     Joomla.Site
 * @subpackage  mod_articles_latest
 *
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\Module\ArticlesLatest\Site\Helper;

defined('_JEXEC') or die;

use Joomla\CMS\Access\Access;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
use Joomla\Component\Content\Site\Model\ArticlesModel;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;

\JLoader::register('ContentHelperRoute', JPATH_SITE . '/components/com_content/helpers/route.php');

/**
 * Helper for mod_articles_latest
 *
 * @since  1.6
 */
abstract class ArticlesLatestHelper
{
    /**
     * Retrieve a list of article
     *
     * @param   Registry       $params  The module parameters.
     * @param   ArticlesModel  $model   The model.
     *
     * @return  mixed
     *
     * @since   1.6
     */
    public static function getList(Registry $params, ArticlesModel $model)
    { // this is the built-in function ... }


    public static function myCustomFunction($created_date)
    {
        $date = new JDate($created_date);
        echo $date->format('l, d F Y H:i', false, false);
    }   

I'm able to call the function from the default.php by using @Zollie's solution, but now I got this error message when using JDate inside my function

Error: Class 'Joomla\Module\ArticlesLatest\Site\Helper\JDate' not found: Class 'Joomla\Module\ArticlesLatest\Site\Helper\JDate' not found

Can anybody tell me, what am I doing wrong in here now? Thanks!

| improve this question | | | | |
  • @mickmackusa I think we should. Early adopters help spreading the enthusiasm. There's not too much information about Joomla 4 can be found on Google Search right now. – webchun May 6 '19 at 23:41
  • Ah yes, I agree with that. Anyway, is what I asked in my question considered as a bug? – webchun May 6 '19 at 23:50
3

I’m answering the question because it is obviously not a Joomla 4 core bug but a mistake in the developer’s coding and the problem would be the same in Joomla 3 too.

If you try to use a Joomla core class in your code you have to include that class on the top of the file otherwise Joomla does not know what it needs to use, so:

use Joomla\CMS\Date\Date;

Then you can use in your code:

$date = new Date($created_date);

These classes are not automatically available everywhere across Joomla unless you point to them. So include the classes you use on the top of your files.

| improve this answer | | | | |
  • Ah ok thanks @Zollie . Anyway, where I can find list joomla core classes? I got the same error when trying to use Jhtml – webchun May 7 '19 at 6:34
  • @webchun - yes, I’ve forgotten to include that in my answer: you can check all the useful classes here: api.joomla.org/cms-3/namespaces/Joomla.html – Zollie May 7 '19 at 6:37
  • Many thanks @Zollie! – webchun May 7 '19 at 6:39
  • @webchun - instead of JHtml you can use use Joomla\CMS\HTML\HTMLHelper; as you can see here: api.joomla.org/cms-3/classes/Joomla.CMS.HTML.HTMLHelper.html. - at the bottom of the pages you also find the path to include to the class. – Zollie May 7 '19 at 6:42
  • ok thanks @Zollie! – webchun May 7 '19 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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