Include PHP code in my custom module Topic is solved

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Post Reply
Dada1991
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Thu Jan 20, 2022 9:09 am

Include PHP code in my custom module

Post by Dada1991 » Thu Mar 24, 2022 10:53 am

I'm using Joomla 4 and I want to insert php code in a custom module.

In the menu of the custom module, I go to "CMS Content" --> "Code" and I insert my php code.

Nevertheless, It doesn't show me the result of my php code but only the raw code.
Why my php code is not interpreted ?

The result:
You do not have the required permissions to view the files attached to this post.

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 15238
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Include PHP code in my custom module

Post by toivo » Thu Mar 24, 2022 12:27 pm

Did you install the third party extension Sourcerer? Did you enable it?
Toivo Talikka, Global Moderator

slhuilli
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Wed Feb 02, 2022 10:57 am

Re: Include PHP code in my custom module

Post by slhuilli » Thu Mar 24, 2022 5:31 pm

I am not fan of sourcerer. I have had some problem with this component. I prefer coding a complete module. But if youve have some little PHP code to execute, It can be a solution.
If the code is written, it is because sourcerer don't work. have you activate it ? (in plugin for example ?)

Dada1991
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Thu Jan 20, 2022 9:09 am

Re: Include PHP code in my custom module

Post by Dada1991 » Mon Mar 28, 2022 4:07 am

Thank you for your replies.

Yes I installed Sourcerer (v9.1.1) plugin but when I enable it, I have an error "Class 'Factory' not found".

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 15238
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Include PHP code in my custom module

Post by toivo » Mon Mar 28, 2022 5:47 am

Dada1991 wrote:
Mon Mar 28, 2022 4:07 am
when I enable it, I have an error "Class 'Factory' not found".
Remove the first line of the code because Sourcerer has already defined the variable $db. It is also possible to use $database.

The line setting the query string should not have round brackets but those two lines with $query will not work.

Here is an example of PHP code from a custom module that uses the free version of Sourcerer to list the usernames from the users table:

Code: Select all

{source}<?php
$query = $db->getQuery(true);
$query->select('* FROM #__users');
$db->setQuery($query);
$users = $db->loadObjectList();
echo 'Users: ';
foreach ($users AS $user)
{
    echo ' ' . $user->username . ' ';
}
?>{/source}
BTW, Sourcerer 9.2.0 is the latest version, released two weeks ago.
Toivo Talikka, Global Moderator

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 29484
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Include PHP code in my custom module

Post by Per Yngve Berg » Mon Mar 28, 2022 6:25 am

In a custom html module, the option Prepare Content must also be enabled.

User avatar
sozzled
Joomla! Master
Joomla! Master
Posts: 13080
Joined: Sun Jul 05, 2009 3:30 am
Location: Canberra, Australia

Re: Include PHP code in my custom module

Post by sozzled » Mon Mar 28, 2022 7:00 am

Per Yngve Berg wrote:
Mon Mar 28, 2022 6:25 am
In a custom html module, the option Prepare Content must also be enabled.
Not necessarily true. If people are using a content plugin with a module then, yes, you need to enable the Prepare Content option (if the option exists). Sourcerer is a system plugin.
“If you think I’m wrong then say, ‘I think you’re wrong.’ If you say ‘You’re wrong!’, how do you know?”
Walking the talk: https://j4xdemo.enduring.com.au
:)

Dada1991
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Thu Jan 20, 2022 9:09 am

Re: Include PHP code in my custom module

Post by Dada1991 » Tue Mar 29, 2022 4:03 am

toivo wrote:
Mon Mar 28, 2022 5:47 am
Dada1991 wrote:
Mon Mar 28, 2022 4:07 am
when I enable it, I have an error "Class 'Factory' not found".
Remove the first line of the code because Sourcerer has already defined the variable $db. It is also possible to use $database.

The line setting the query string should not have round brackets but those two lines with $query will not work.

Here is an example of PHP code from a custom module that uses the free version of Sourcerer to list the usernames from the users table:

Code: Select all

{source}<?php
$query = $db->getQuery(true);
$query->select('* FROM #__users');
$db->setQuery($query);
$users = $db->loadObjectList();
echo 'Users: ';
foreach ($users AS $user)
{
    echo ' ' . $user->username . ' ';
}
?>{/source}
BTW, Sourcerer 9.2.0 is the latest version, released two weeks ago.
It's working now, thank you for the help


Post Reply

Return to “Joomla! 4.x Coding”