In component.php, I need to add a href link to a css file with a generated name eg: custom-123abc456.css Topic is solved

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
User avatar
Chacapamac
Joomla! Guru
Joomla! Guru
Posts: 963
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

In component.php, I need to add a href link to a css file with a generated name eg: custom-123abc456.css

Post by Chacapamac » Sat Jun 06, 2020 10:34 pm

In the active template component.php I need to add a href link to a css file with a name that have a generated suffix (coming from an scss processor)

I can hardcode the file exactly this way:

Code: Select all

<!DOCTYPE html>
<html lang="<?php echo $this->language; ?>" dir="<?php echo $this->direction; ?>">
   <head>
   <jdoc:include type="head" />
   <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/custom-06990a99f87dea2049782d30f991833f.css" type="text/css" />
</head>
<body class="contentpane component">
<jdoc:include type="message" />
<jdoc:include type="component" />
</body>
</html>
The file is always custom-ABUNCHOFGENERATEDNUMBERSLETTERSHERE.css

I’m not sure how to replace the “ABUNCHOFGENERATEDNUMBERSLETTERSHERE” by some kind of REGEX that will get the file even if that generated string change.

Possible, I don’t know...?

Also do you know a way to override the component.php of an active template?
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

 
User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 22576
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: In component.php, I need to add a href link to a css file with a generated name eg: custom-123abc456.css

Post by pe7er » Sun Jun 07, 2020 12:19 pm

Chacapamac wrote:
Sat Jun 06, 2020 10:34 pm
In the active template component.php I need to add a href link to a css file with a name that have a generated suffix (coming from an scss processor)
[..]
The file is always custom-ABUNCHOFGENERATEDNUMBERSLETTERSHERE.css
You can use Joomla's HTML Helper to include a CSS (or JavaScript) file to the head of your html document.

Code: Select all

use Joomla\CMS\HTML\HTMLHelper;
HTMLHelper::_('stylesheet', 'templates/your-template/css/css-file.css', ['version' => 'auto', 'relative' => false]);
With 'version'=>'auto' Joomla will automatically add a MD5 hash in the url/filename.
Kind Regards,
Peter Martin, Global Moderator
https://db8.nl - Joomla specialist, Nijmegen, Nederland
Co-developer of d2 Content https://data2site.com/joomla-extensions/d2-content

User avatar
Chacapamac
Joomla! Guru
Joomla! Guru
Posts: 963
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: In component.php, I need to add a href link to a css file with a generated name eg: custom-123abc456.css

Post by Chacapamac » Sun Jun 07, 2020 1:57 pm

Thank you Pe7er,

Ok for the use of HTMLHelper::_

Now, the css file that I need to load always start as (custom-) follow by a bunch of alphanumeric generated characters and end with (.css) and always in the same folder

The alphanumeric parts is changing each time the original “custom.scss” is modified and compile (leafo) to a css file

My problem is to load that css file base only on it’s prefix (custom-) and it’s ending file type (.css)

I was thinking at some regex style declaration, use of preg_replace but I’m far from a coder, I’m not sure how to implement.


Will be something like ?

Code: Select all

HTMLHelper::_('stylesheet', 'templates/your-template/css/custom-[SOME REGEX HERE].css', ['version' => 'auto', 'relative' => false]);
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1740
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: In component.php, I need to add a href link to a css file with a generated name eg: custom-123abc456.css

Post by SharkyKZ » Sun Jun 07, 2020 2:24 pm

First, do you actually need the random suffix? If not, maybe your CSS compiler script can be changed use a specific file name.

If that's not possible, and the suffix is not available anywhere in Joomla, you can use JFolder::files() to get a list of files matching a regex in your CSS folder.

Code: Select all

use Joomla\CMS\Filesystem\Folder;

$files = Folder::files(JPATH_THEMES . '/' . $this->template . '/css', 'custom-.*\.css');

User avatar
Chacapamac
Joomla! Guru
Joomla! Guru
Posts: 963
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: In component.php, I need to add a href link to a css file with a generated name eg: custom-123abc456.css

Post by Chacapamac » Sun Jun 07, 2020 4:09 pm

Sorry for be a Newbie here, but, with your help, Im on the way

First If I use pe7er code and input the EXACT name of the css file, it work;

Code: Select all

//Styles
JHtml::_('stylesheet', 'custom-06990a99f87dea2049782d30f991833f.css', array('version' => 'auto', 'relative' => true));
You can see the result login in lightbox getting some css from that file:
logincss.jpg
Same thing with:

Code: Select all

<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/custom-06990a99f87dea2049782d30f991833f.css" type="text/css" />
The login get is css


Now, (I probably do it wrong)
If I use SharkyKZ JPATH
that way in the code:
pathscode.jpg
The lightbox login bring the 404 error page:
path-themes,jpg.jpg

• With the JHtml::_ system I just need to replace the alphanumeric with some regular expression, I guess?
You do not have the required permissions to view the files attached to this post.
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1740
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: In component.php, I need to add a href link to a css file with a generated name eg: custom-123abc456.css

Post by SharkyKZ » Sun Jun 07, 2020 7:26 pm

My snippet is just for finding files that match an expression. You have to pass them to JHtml::_('stylesheet'). How exactly to do this depends on your use case. Are there multiple files? Are you sure a file always exists?

If you're sure the file exists and there's only one, you can use it like this:

Code: Select all

JHtml::_('stylesheet', $files[0], ['version' => 'auto', 'relative' => true]);

User avatar
Chacapamac
Joomla! Guru
Joomla! Guru
Posts: 963
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: In component.php, I need to add a href link to a css file with a generated name eg: custom-123abc456.css

Post by Chacapamac » Mon Jun 08, 2020 11:46 am

This is the content of the template css folder.
files.jpg
The custom-000generatedalphanmeric000.css file is my most important target.

The ability to add astroid-.....css and style-....css can maybe be a cool opion, but definitivly color-....csss
• All the generated alphanumeric parts a changing each time a new settings is modiied in the template manager or a change is made to the custom.scss file (the one that generated The custom-000generatedalphanmeric000.css file)
editor_content.css is NOT a target
You do not have the required permissions to view the files attached to this post.
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1740
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: In component.php, I need to add a href link to a css file with a generated name eg: custom-123abc456.css

Post by SharkyKZ » Tue Jun 09, 2020 7:47 am

If you have multiple files, adjust the regex accordingly and the run a loop to add each stylesheet. The complete code:

Code: Select all

use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\HTML\HTMLHelper;

$files = Folder::files(JPATH_THEMES . '/' . $this->template . '/css', '(custom|style|astroid|color)-.+\.css');

foreach ($files as $file)
{
	HTMLHelper::_('stylesheet', $file, ['version' => 'auto', 'relative' => true]);
}

User avatar
Chacapamac
Joomla! Guru
Joomla! Guru
Posts: 963
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: In component.php, I need to add a href link to a css file with a generated name eg: custom-123abc456.css

Post by Chacapamac » Wed Jun 10, 2020 2:17 am

SharkyKZ, you are some kind of Joomla God, It absolutely work. :eek:

This is what I did;
In component.php on top of the file I insert:

Code: Select all

use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\HTML\HTMLHelper;
top-of-file.jpg
Just before the DOCTYPE, in the PHP I add your code for 2 files:
custom-whateverxyz123.css & astroid-whateberabc098.css
error.jpg
and, like magic both css where apply to my component... :D

Just a note:
As you can see, my old Dreamweaver CS6 pick errors in both HTMLHelper::_ line ?
— I suspect, this is not a problem as I don’t see any error in the output
You do not have the required permissions to view the files attached to this post.
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1740
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: In component.php, I need to add a href link to a css file with a generated name eg: custom-123abc456.css

Post by SharkyKZ » Wed Jun 10, 2020 6:41 am

No idea about the error.

But you don't need to repeat the code multiple times if you use the provided regex.

User avatar
Chacapamac
Joomla! Guru
Joomla! Guru
Posts: 963
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: In component.php, I need to add a href link to a css file with a generated name eg: custom-123abc456.css

Post by Chacapamac » Wed Jun 10, 2020 12:06 pm

Again Thanks SharkyKZ.

Yes your exact code work, no need to repeat.

I love Joomla and the great people around it - Great Community
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

 

Post Reply

Return to “Joomla! 3.x Coding”