-
-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RTM] Use Symfony asset component for Contao assets #1165
Conversation
src/Asset/ContaoContext.php
Outdated
|
|
||
| use Symfony\Component\Asset\Context\ContextInterface; | ||
|
|
||
| class ContaoContext implements ContextInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing should rely on the constants anymore. If you need to access TL_* it's better to find a different way because the constants will not be available in 5 anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only other way is to read from $GLOBALS in the back end and from global $objPage in the front end, which does not seem less wrong than using the constants to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, or we find a new way because apparently we will still need that feature in the future :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added ToDo no. 3 for this. We should probably inject the PageModel and "duplicate" the code from Controller::setStaticUrls.
|
I've added a dependency to https://github.com/terminal42/asset-bundle. This will automatically add all public bundle files as asset packages, completing the new Contao setup. in Twig templates: <img src="{{ asset('img/icon.png', 'rock_solid_slider') }}">
<img src="{{ asset('folder.gif', 'terminal42_folderpage') }}">in Contao templates: <img src="<?= $this->asset('img/icon.png', 'rock_solid_slider') ?>">
<img src="<?= $this->asset('folder.gif', 'terminal42_folderpage') ?>">in Contao HTML module/element: <img src="{{asset::img/icon.png::rock_solid_slider}}">
<img src="{{asset::folder.gif::terminal42_folderpage}}"> |
495cb11
to
1358688
Compare
|
I think we should finish this PR with the current feature set. The only item from the todo list that needs to be done is "Unit Tests". The rest can be added later. |
|
@Toflar I have rewritten the contexts to use the page model, is that how you intended it? |
91c4372
to
8f4dda4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since there are only two contexts ("assets" and "files"), maybe we don't need the $field property. Why not have a ContaoAssetsContext and ContaoFilesContext?
src/Asset/ContaoContext.php
Outdated
| */ | ||
| private function getFieldValue(): string | ||
| { | ||
| if (null === $this->page) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (null !== $this->page) {
return (string) $this->page->{$this->field};
}
/** @var Config $config */
$config = $this->framework->createInstance(Config::class);
return (string) $config->get($this->field);
I don't think that would make anything better. It means we need three classes instead of one, including an abstract. Which will be exactly the same, a class that will be configured, just by extending instead of a constructor argument. |
17bab2e
to
d6495f9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides my two comments, I think there are a lot more places in the code where TL_ASSETS_URL is used. Are those not relevant?
src/Asset/ContaoContext.php
Outdated
| { | ||
| if (null === $page) { | ||
| /** @var Config $config */ | ||
| $config = $this->framework->createInstance(Config::class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you using createInstance() instead of getAdapter() here? And shouldn't we initialize the framework first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both must be fixed indeed.
| @@ -394,7 +394,7 @@ public function mergeRow(array $arrData) | |||
| */ | |||
| public function markModified($strKey) | |||
| { | |||
| if (!isset($this->arrModified[$strKey])) | |||
| if (!isset($this->arrModified[$strKey]) && isset($this->arrData[$strKey])) | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change related to the feature?
| @@ -1792,45 +1793,31 @@ public static function addEnclosuresToTemplate($objTemplate, $arrItem, $strKey=' | |||
| /** | |||
| * Set the static URL constants | |||
| * | |||
| * @param PageModel $objPage An optional page object | |||
| * @deprecated Deprecated in Contao 4.5, to be removed in Contao 5. Use the asset contexts instead. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we have deprecated the method, we should no longer use it. Otherwise, if we still need to use it, we should not deprecate it. 😉
f7216b6
to
cbbe3ad
Compare
cbbe3ad
to
4bb7d55
Compare
|
Thank you @aschempp. |
|
In your examples you are using <img src="<?= $this->asset('folder.gif', 'terminal42_folderpage') ?>">however, shouldn't it be <img src="<?= $this->asset('folder.gif', 'terminal42/contao-folderpage') ?>">Or what exactly is |
|
Yes it is, the example comment was before some more commits ;) |
|
@fritzmg The diffeference is Contao componentes (assets), where the package name is the composer package (e.g. |
I started using Symfony Encore for our application asset management and stumbled over the Symfony Asset service. I think there are a ton of useful feature for Contao.
TL_ASSETS_URLprefix if configured.The provided code is totally working, but there are a lot more features we can implement. Missing TODOs:
TL_JAVASCRIPTandTL_CSSContao\CombinerController::setStaticUrlsinstead of using constants in the context service.Resources/publicof all bundles as packages.