2

Joomla 3.9.14

I have an external application that uses the script below, allowing users to register as a user on my Joomla site. It all appears to be working apart from error displaying. My code is below;

register.php

<?php 
// database connection
include('conn.php');

require_once JPATH_BASE .'/components/com_users/models/registration.php';

$model = new UsersModelRegistration();

jimport('joomla.mail.helper');
jimport('joomla.user.helper');

$language = JFactory::getLanguage();
$language->load('com_users', JPATH_SITE);

// hard coded for now, in live site these will be $_POST values
$username = 'adam';
$name = 'adam24';
$email = '[email protected]';
$password = 'password';

$data = array( 
    'username' => $username,
    'name' => $name,
    'email1' => $email,
    'password1' => $password, // First password field
    'password2' => $password, // Confirm password field
    'groups'    =>array('10'),
    'block' => 0 );

echo $model->register($data);
?>

When I visit this page, the user is created in my Joomla site with the correct details. When I visit the page for a second time I get an error about the username already being is use, this is expected. However when I dump the error it is displayed as;

$err = $model->get('_errors');

// result of var_dump($err)
array (
  0 => 'Joomla\\CMS\\Form\\Form::getInstance() could not load file',
  1 => 'Registration failed: JLIB_DATABASE_ERROR_USERNAME_INUSE',
) 

The JLIB_DATABASE_ERROR_USERNAME_INUSE constant isn't loading - why would this be?

It's defined at administrator\language\en-GB\en-GB.lib_joomla.ini

1
  • JText::_() should be translating that when setting the error array entry, so start looking at libraries/src/Language/Text.php for the issue. The string alone is passed in by User.php to set it, so my first suspect would be an issue with language detection, but you should probably set your debugger to single-step through it (or possible just the Language class's (Language.php) getInstance() method to verify what's happening there.
    – Arlen
    Commented Jan 16, 2020 at 15:12

2 Answers 2

2

Hard to say without seeing the complete code but my guess is you don't initialize the application and the language file in question does not get loaded. You can load it manually:

$language->load('lib_joomla', JPATH_ADMINISTRATOR);
2
  • Well, you already knew that the string was in lib_joomla, not in com_users language file.
    – Sharky
    Commented Jan 17, 2020 at 9:18
  • The error means the XML form file used by the model was not found. You might need to add lookup paths with Joomla\CMS\Form\FormHelper::addFormPath(). More similar errors may occur due to incomplete dependencies.
    – Sharky
    Commented Jan 17, 2020 at 9:20
0

I believe this arises because the you're running an external php application when you visit that page.

When you visit a page which is part of the Joomla website then core Joomla runs and it performs a lot of application initialisation, which includes determining the language which should be used, and then loading the language strings from the JLIB ini file which you mention.This loading is done within the load() method in the file libraries/src/language/Language.php. Then also when you output an error message the code will have to call the Text::_() method in libraries/src/language/Text.php to translate the language constant using the language strings stored in the Joomla Language class instance. I reckon that at least some of this functionality is absent in your external application.

To get round this you could try and mirror how Joomla initialises the Language classes etc, but personally I think that will be very challenging.

If you're not happy to leave things the way they are and want instead to be able to handle possible error messages, then the easiest solution would be to interpret the language constant yourself by reading the JLIB ini file using PHP parse_ini_file and then looking for matching strings in the error message, and doing a string replace.

I also seem to remember hearing sometime that one of the aims of Joomla 4 was to provide a basic Joomla framework that external PHP applications could use, and if that's true then it might provide a solution, but I don't know enough about Joomla 4 to help there I'm afraid. I also could be wrong about that aim!

2
  • Thanks @Robbie Jackson you were right about some functionality being absent. I had to load $language->load('lib_joomla', JPATH_ADMINISTRATOR);
    – jonboy
    Commented Jan 17, 2020 at 9:12
  • Wow, a lot easier to fix than I expected! Commented Jan 17, 2020 at 11:00

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.