Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
name: CI

on: [ push ]
on:
push:
branches:
- main
- v2
pull_request:
types: [ opened, reopened, synchronize, ready_for_review ]
branches:
- '**'

jobs:
tests:
name: PHP ${{ matrix.php }}
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
strategy:
fail-fast: false
matrix:
php: [ 8.1, 8.2, 8.3, 8.4 ]
contao: [ 4.13.*, 5.3.*, 5.4.* ]
contao: [ 4.13.*, 5.3.*]

steps:
- name: Setup PHP
Expand All @@ -34,6 +43,7 @@ jobs:

coverage:
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down Expand Up @@ -61,6 +71,7 @@ jobs:

phpstan:
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -79,6 +90,7 @@ jobs:
ecs:
name: ECS
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ composer.json~
composer.phar
.ddev/
.phpunit.result.cache
phpunit.xml.dist.bak
phpunit.xml.dist.bak
.codex
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file.

## [2.2.0] - 2026-04-20
- Added: support for modern twig layouts of contao 5.7 ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
- Added: EntrypointsBuilder concept for retriving current page entrypoints ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
- Changed: add constant for default field name ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
- Fixed: removed dead or unnecessary code and checks ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
- Deprecated: `src/Asset/PageEntrypoints.php`, `src/Asset/TemplateAsset.php` and `src/Asset/TemplateAssetGenerator.php` ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
- Deprecated: `ConfigurationHelper::isEnabledOnPage()` ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
- Deprecated: getter-Methods in `EncoreEnabledEvent` ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))

## [2.1.1] - 2026-03-30
- Fixed: compatibility issue with symfony 7

Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"symfony/config": "^5.4 || ^6.0 || ^7.0",
"symfony/console": "^5.4 || ^6.0 || ^7.0",
"symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0",
"symfony/deprecation-contracts": "^1.0 || ^2.0 || ^3.0",
"symfony/filesystem": "^5.4 || ^6.0 || ^7.0",
"symfony/http-foundation": "^5.4 || ^6.0 || ^7.0",
"symfony/http-kernel": "^5.4 || ^6.0 || ^7.0",
Expand All @@ -36,15 +37,16 @@
"twig/twig": "^1.38.3 || ^2.7 || ^3.0"
},
"require-dev": {
"contao/test-case": "^4.0 || ^5.0",
"contao/core-bundle": "^4.13",
"contao/test-case": "^4.0",
"contao/manager-plugin": "^2.13",
"phpunit/phpunit": "^8.0 || ^9.0",
"php-coveralls/php-coveralls": "^2.0",
"symfony/phpunit-bridge": "^3.2 || ^4.0 || ^5.0 || ^6.0",
"heimrichhannot/contao-test-utilities-bundle": "^0.1.4",
"phpstan/phpstan": "^1.12",
"phpstan/phpstan-symfony": "^1.4",
"rector/rector": "^1.2",
"phpstan/phpstan": "^1.12 || ^2.0",
"phpstan/phpstan-symfony": "^1.4 || ^2.0",
"rector/rector": "^1.2 || ^2.0",
"contao/contao-rector": "dev-main",
"symplify/easy-coding-standard": "^12.5"
},
Expand Down
53 changes: 53 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* Copyright (c) 2022 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

declare(strict_types=1);

use HeimrichHannot\EncoreBundle\Asset\FrontendAsset;
use HeimrichHannot\EncoreBundle\Asset\TemplateAsset;
use HeimrichHannot\EncoreBundle\EntryPoint\EntryPointBuilderFactory;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container): void {
$services = $container->services();

$services
->defaults()
->autowire()
->bind('$bundleConfig', '%huh_encore%')
->bind('$webDir', '%contao.web_dir%')
->bind('$encoreCache', service('webpack_encore.cache'))
->bind(CacheItemPoolInterface::class, service('webpack_encore.cache'))
->bind(TagRenderer::class, service('webpack_encore.tag_renderer'))
;

$services
->load('HeimrichHannot\\EncoreBundle\\', '../src/{Asset,Collection,Command,DataContainer,EventListener,Helper}/*')
->exclude('../src/Asset/{EntrypointCollection.php}')
->public()
->autoconfigure()
;

$services
->set(EntryPointBuilderFactory::class)
;

$services
->alias('huh.encore.asset.frontend', FrontendAsset::class)
->public()
;

$services
->alias('huh.encore.asset.template', TemplateAsset::class)
->public()
;
};
22 changes: 0 additions & 22 deletions config/services.yml

This file was deleted.

2 changes: 1 addition & 1 deletion contao/dca/tl_layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/*
* Subpalettes
*/
$dca['subpalettes']['addEncore'] = 'encoreEntries,encoreStylesheetsImportsTemplate,encoreScriptsImportsTemplate';
$dca['subpalettes']['addEncore'] = EncoreEntriesSelectField::NAME_DEFAULT . ',encoreStylesheetsImportsTemplate,encoreScriptsImportsTemplate';

/**
* Fields.
Expand Down
96 changes: 48 additions & 48 deletions docs/developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ This document contains additional information for developers working with encore

## Add entries from your code (frontend module, content element,...)

Since version 1.3 it is possible to add encore entries from your code. So for example the slider assets are automatically included, if the slider module is added to the page.

The most simple method is to use the `PageAssetsTrait` of [Contao Encore Contracts](https://github.com/heimrichhannot/contao-encore-contracts).
Use this trait in your class in combination with `ServiceSubscriberInterface` and make sure your class is registered as service with autoconfigure activated.
Now you have a new method `addPageEntrypoint()` available.
Expand Down Expand Up @@ -64,65 +62,67 @@ PaletteManipulator::create()

## Add encore entries to custom template

If you don't want to render assets on page basis, it is possible to generate a custom set of encore entries.

1. Create an `EntrypointCollection` with the `EntrypointCollectionFactory` service
1. Get your assets with `TemplateAssetGenerator` service.
1. Optional: If you want an input field in the contao backend to select entries, you can use the `DcaGenerator` service to generate an input like on layout or page settings.
To collect or render assets in custom templates or abstinent from the normal page rendering, use the `EntryPointsBuilder`.

```php
use Contao\FrontendTemplate;
use HeimrichHannot\EncoreBundle\Asset\EntrypointCollectionFactory;
use HeimrichHannot\EncoreBundle\Asset\TemplateAssetGenerator;
<?php

function renderTemplateWithEncore(array $entrypoints, EntrypointCollectionFactory $entrypointCollectionFactory, TemplateAssetGenerator $templateAssetGenerator)
{
$template = new FrontendTemplate();
$collection = $entrypointCollectionFactory->createCollection($entrypoints);
$template->stylesheets = $templateAssetGenerator->linkTags($collection);
$template->headJavaScript = $templateAssetGenerator->headScriptTags($collection);
$template->javaScript = $templateAssetGenerator->scriptTags($collection);
return $template->getResponse();
}
```

It is also possible to get the stylesheets inline:
namespace App\CustomController;

```php
use Contao\FrontendTemplate;
use HeimrichHannot\EncoreBundle\Asset\EntrypointCollectionFactory;
use HeimrichHannot\EncoreBundle\Asset\TemplateAssetGenerator;
use HeimrichHannot\EncoreBundle\Asset\FrontendAsset;
use HeimrichHannot\EncoreBundle\EntryPoint\EntryPointBuilderFactory;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;use Twig\Environment;

function renderTemplateWithEncore(array $entrypoints, EntrypointCollectionFactory $entrypointCollectionFactory, TemplateAssetGenerator $templateAssetGenerator)
class CustomController
{
$template = new FrontendTemplate();
$collection = $entrypointCollectionFactory->createCollection($entrypoints);
$template->inlineCss = $templateAssetGenerator->inlineCssLinkTag($collection);
return $template->getResponse();
private readonly TagRenderer $tagRenderer;
private readonly EntryPointBuilderFactory $entrypointBuilderFactory;
private readonly Environment $twig;
private readonly FrontendAsset $frontendAsset;

public function __invoke(): Response
{
// collect entry points from the different sources
$entryPoints = $this->entrypointBuilderFactory->create()
// add the sources you want:
->setPage($event->getPage())
->setLayout($event->getLayout())
->setFrontendAsset($this->frontendAsset)
// build the collection:
->build();

// render the tags, for example with the tag renderer of webpack encore bundle
$this->tagRenderer->reset();
$css = $head = $body = [];
foreach ($entryPoints->allActive() as $entrypoint) {
if ($entrypoint->requiresCss) {
$css[] = $this->tagRenderer->renderWebpackLinkTags($entrypoint->name);
}
if ($entrypoint->head) {
$head[] = $this->tagRenderer->renderWebpackScriptTags($entrypoint->name);
} else {
$body[] = $this->tagRenderer->renderWebpackScriptTags($entrypoint->name);
}
}

// render the template
return new Response($this->twig->render('custom_template.html.twig', [
'css' => $css,
'head' => $head,
'body' => $body,
]));
}
}
```

## ConfigurationHelper

The `ConfigurationHelper` service can be used to obtain some configuration information. Following methods are available:

`isEnabledOnCurrentPage(?PageModel $pageModel = null): bool` - Return if encore is enabled for the current frontend page. You can pass a page object to check for a custom page, otherweise `global $objPage` is used.
`isEnabledOnPage(PageModel $page, ?LayoutModel $layout = null): bool` - Return if encore is enabled for the current frontend page.

`getRelativeOutputPath(): string` - Return the relative output path configured by webpack encore bundle. Typical this is `build`.

`getAbsoluteOutputPath(): string` - Return the absolute output path configured by webpack encore bundle. For example `/var/www/html/project/web/build`

## Custom import templates

If you need custom templates for the import of javascript and stylesheet assets files, Encore Bundle provide support for this.
Create a twig template (see `src/Resources/views` for examples) and register them in your (project) bundle config.

Example:

```yaml
huh_encore:
templates:
imports:
- { name: default_css, template: "@HeimrichHannotEncore/encore_css_imports.html.twig" }
- { name: default_js, template: "@HeimrichHannotEncore/encore_js_imports.html.twig" }
```
`getAbsoluteOutputPath(): string` - Return the absolute output path configured by webpack encore bundle. For example `/var/www/html/project/public/build`
17 changes: 15 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
parameters:
ignoreErrors:
-
message: "#^Offset 'fields' on array\\{\\} in isset\\(\\) does not exist\\.$#"
message: '#^Call to method getLayout\(\) on an unknown class Contao\\CoreBundle\\Event\\LayoutEvent\.$#'
identifier: class.notFound
count: 2
path: tests/EventListener/DcaField/EncoreEntriesSelectFieldListenerTest.php
path: src/EventListener/InjectPageEntriesListener.php

Comment thread
koertho marked this conversation as resolved.
-
message: '#^Call to method getPage\(\) on an unknown class Contao\\CoreBundle\\Event\\LayoutEvent\.$#'
identifier: class.notFound
count: 2
path: src/EventListener/InjectPageEntriesListener.php

-
message: '#^Parameter \$event of method HeimrichHannot\\EncoreBundle\\EventListener\\InjectPageEntriesListener\:\:onLayoutEvent\(\) has invalid type Contao\\CoreBundle\\Event\\LayoutEvent\.$#'
identifier: class.notFound
count: 1
path: src/EventListener/InjectPageEntriesListener.php
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
parameters:
level: 4
level: 5
paths:
- src
- tests
- contao
universalObjectCratesClasses:
- Contao\LayoutModel
Expand Down
Loading
Loading