Skip to content

Commit

Permalink
load asset in addToTemplate method, fixed warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Feb 21, 2023
1 parent 96b88e5 commit 7bbc5b9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## [1.2.0] - 2022-03-04
- Changed: load assets only on pages where widget is used
- Fixed: deprecation warning

## [1.1.0] - 2022-03-04
- Changed: allow php 8
- Changed: allow symfony/kernel 5
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"ext-json": "*",
"contao/core-bundle": "^4.4",
"symfony/http-kernel": "^3.4 || ^4.0 || ^5.0",
"heimrichhannot/contao-request-bundle": "^1.1.1",
"heimrichhannot/contao-ajax-bundle": "~1.0",
"heimrichhannot/contao-request-bundle": "^1.1.1",
"heimrichhannot/contao-utils-bundle": "^2.225",
"heimrichhannot/datatables": "^1.10",
"heimrichhannot/datatables-additional": "^1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class Plugin implements BundlePluginInterface
{

public function getBundles(ParserInterface $parser)
public function getBundles(ParserInterface $parser): array
{
return [
BundleConfig::create(HeimrichHannotListWidgetBundle::class)->setLoadAfter([
Expand Down
23 changes: 3 additions & 20 deletions src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
<?php

/**
* Backend form fields
*/
$GLOBALS['BE_FFL']['listWidget'] = \HeimrichHannot\ListWidgetBundle\Widget\ListWidget::class;
use HeimrichHannot\ListWidgetBundle\Widget\ListWidget;

/**
* Assets
* Backend form fields
*/
if (TL_MODE == 'BE')
{
$GLOBALS['TL_JAVASCRIPT']['datatables-i18n'] =
'assets/datatables-additional/datatables-i18n/datatables-i18n.min.js';
$GLOBALS['TL_JAVASCRIPT']['datatables-core'] = 'assets/datatables/datatables/media/js/jquery.dataTables.min.js';
$GLOBALS['TL_JAVASCRIPT']['datatables-rowReorder'] =
'assets/datatables-additional/datatables-RowReorder/js/dataTables.rowReorder.min.js';

$GLOBALS['TL_JAVASCRIPT']['jquery.list_widget.js'] = 'bundles/heimrichhannotlistwidget/assets/js/jquery.list_widget.js';

$GLOBALS['TL_CSS']['datatables-core'] =
'assets/datatables-additional/datatables.net-dt/css/jquery.dataTables.min.css';
$GLOBALS['TL_CSS']['datatables-rowReorder'] =
'assets/datatables-additional/datatables-RowReorder/css/rowReorder.dataTables.min.css';
}
$GLOBALS['BE_FFL']['listWidget'] = ListWidget::class;
46 changes: 35 additions & 11 deletions src/Widget/ListWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
use Contao\BackendTemplate;
use Contao\Controller;
use Contao\Database;
use Contao\Input;
use Contao\Model;
use Contao\RequestToken;
use Contao\System;
use Contao\Template;
use Contao\Widget;
use HeimrichHannot\AjaxBundle\Response\ResponseData;
use HeimrichHannot\AjaxBundle\Response\ResponseSuccess;
use HeimrichHannot\UtilsBundle\Util\Utils;

class ListWidget extends Widget
{
Expand Down Expand Up @@ -151,20 +154,41 @@ public static function initAjaxLoading(array $arrConfig, $objContext = null, $ob
}
}

public static function addToTemplate($objTemplate, array $arrConfig)
/**
* Add widget setup to template
*
* Configuration:
* - loadAssets: (bool) Load assets (default: true)
*/
public static function addToTemplate(Template $objTemplate, array $configuration): void
{
$objTemplate->class = $arrConfig['class'];
$objTemplate->ajax = $arrConfig['ajax'];
$objTemplate->headerFields = $arrConfig['headerFields'];
$objTemplate->columnDefs = htmlentities(json_encode(static::getColumnDefsData($arrConfig['columns'])));
$objTemplate->language = htmlentities(json_encode($arrConfig['language']));

if ($arrConfig['ajax']) {
$objTemplate->processingAction = System::getContainer()->get('huh.utils.url')->addQueryString(
'key=' . static::LOAD_ACTION . '&scope=' . $arrConfig['identifier'] . '&rt=' . RequestToken::get()
$configuration = array_merge([
'loadAssets' => true,
], $configuration);

$objTemplate->class = $configuration['class'];
$objTemplate->ajax = $configuration['ajax'];
$objTemplate->headerFields = $configuration['headerFields'];
$objTemplate->columnDefs = htmlentities(json_encode(static::getColumnDefsData($configuration['columns'])));
$objTemplate->language = htmlentities(json_encode($configuration['language']));

if ($configuration['ajax']) {
$objTemplate->processingAction = System::getContainer()->get(Utils::class)->url()->addQueryStringParameterToUrl(
'key=' . static::LOAD_ACTION . '&scope=' . $configuration['identifier'] . '&rt=' . RequestToken::get()
);
} else {
$objTemplate->items = $arrConfig['items'];
$objTemplate->items = $configuration['items'];
}

if ($configuration['loadAssets']) {
$GLOBALS['TL_JAVASCRIPT']['datatables-i18n'] = 'assets/datatables-additional/datatables-i18n/datatables-i18n.min.js';
$GLOBALS['TL_JAVASCRIPT']['datatables-core'] = 'assets/datatables/datatables/media/js/jquery.dataTables.min.js';
$GLOBALS['TL_JAVASCRIPT']['datatables-rowReorder'] = 'assets/datatables-additional/datatables-RowReorder/js/dataTables.rowReorder.min.js';

$GLOBALS['TL_JAVASCRIPT']['jquery.list_widget.js'] = 'bundles/heimrichhannotlistwidget/assets/js/jquery.list_widget.js';

$GLOBALS['TL_CSS']['datatables-core'] = 'assets/datatables-additional/datatables.net-dt/css/jquery.dataTables.min.css';
$GLOBALS['TL_CSS']['datatables-rowReorder'] = 'assets/datatables-additional/datatables-RowReorder/css/rowReorder.dataTables.min.css';
}
}

Expand Down

0 comments on commit 7bbc5b9

Please sign in to comment.