Joomla - php - javascript - file display

43 views
Skip to first unread message

Philippe Levi

unread,
Apr 19, 2024, 11:35:13 AMApr 19
to Joomla! General Development
Hi everyone,
First off, let me apologize for not being really fluent in javascript/php.

I am trying to develop a Joomla 4/5 module in which I want to open an external local text file and display its content in the module...
Everything is set and running ok on the module side. Fields are defined ok.
The only thing I can't really achieve (beginner's issue I believe) is pass the file name (gotten from $params) and somehow pass it to javascript to open the file to display its contents in the module.
1. I cannot seem to retrieve the full local path but just the filename (for security reasons I understand)
2. If my field definition is like:
<field name="textfile" type="file" label="Select a file" description="Select a file to display" size="10" accept="text/*" />, and if the result returned is, say, text.txt, how can I grab this 'text.txt' string, make it part of a file object which I could open?

Not sure whether it's possible or not.
Thx for helping the helpless

Steven Berkson

unread,
Apr 19, 2024, 2:48:14 PMApr 19
to joomla-de...@googlegroups.com

You can make the value available to Javascript a number of ways. You can manually add a script declaration and echo the value.

 

Here is what I have been doing. This is not in a module, but just figure out how to access WebAssetManager and…

 

$wa = $this->document->getWebAssetManager();

$wa->addInlineScript('let myTextFile = ' . $myTextFile . ';')

 

And then myTextFile should be available to your JS code.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/joomla-dev-general/ca0aac59-f28e-4984-881d-3f1ccdda3ac5n%40googlegroups.com.

Philippe Levi

unread,
Apr 19, 2024, 3:10:44 PMApr 19
to Joomla! General Development
Thank you indeed for this suggestion which I'll investigate tomorrow but have one question though:
since the file name is presented without its full path, will this "let myTextFile = ' . $myTextFile" statement be working to then let me open the file since the selected file could be located anywhere locally in the pc disk?
Not too familiar with WebAssetManager since being more of a .NET guy...

Thx

Steven Berkson

unread,
Apr 19, 2024, 3:34:47 PMApr 19
to joomla-de...@googlegroups.com
You'll have to provide a full path to the file. Whether you put that information in the paper variable or in the JS code is up to you.

The path could be http://mydomain.com/myTestFile.txt, or a relative path like ../myTestFile.txt

If you want to keep the path confidential or if the information is static when the page is loaded, consider putting the entire contents of the file into layout in a hidden element and then just using Javascript to display it, or use Ajax to retrieve the file contents when called for.

Steven Berkson


From: joomla-de...@googlegroups.com <joomla-de...@googlegroups.com> on behalf of Philippe Levi <rosc...@gmail.com>
Sent: Friday, April 19, 2024 12:10:44 PM

To: Joomla! General Development <joomla-de...@googlegroups.com>
Subject: Re: [jgen] Joomla - php - javascript - file display
 

Steven Berkson

unread,
Apr 19, 2024, 5:55:47 PMApr 19
to joomla-de...@googlegroups.com

Correction “…Whether you put that information in the PHP variable or in the JS code is up to you…”

Reply all
Reply to author
Forward
0 new messages