feat(main): Add custom CSS classes to HTML blocks

This feature allows administrators to define custom CSS classes for individual HTML blocks.
It enhances styling flexibility and integrates with common grid systems (e.g., Bootstrap `col-md-6`)
by applying user-defined classes to the block's wrapper element.

Includes:
- A new `custom_class` field in the block model, form, and data handlers.
- Database schema updates with an upgrade path to handle potential renames from `column_class`.
- Frontend rendering to apply the classes.
- Registration of `actionFrontControllerSetMedia` hook to load front-end CSS.
- Basic responsive image styling for blocks.
This commit is contained in:
2026-08-13 11:33:24 +02:00
parent d0da05845a
commit fb641fc236
14 changed files with 101 additions and 4 deletions
+1
View File
@@ -7,6 +7,7 @@ class Config
const HOOKS = [
'actionAdminControllerSetMedia',
'actionCustomhtmlblockGridDataModifier',
'actionFrontControllerSetMedia',
];
const BLOCK_TYPES = [
@@ -19,6 +19,7 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
$block->title = (string) $formData['title'];
$block->type = (string) $formData['type'];
$block->show_title = (int) $formData['show_title'];
$block->custom_class = (string) ($formData['custom_class'] ?? '');
$block->content = (string) $formData['content'];
$block->active = (int) $formData['active'];
$block->ordering = $this->getNextOrdering($idGroup);
@@ -44,6 +45,7 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
$block->title = (string) $formData['title'];
$block->type = (string) $formData['type'];
$block->show_title = (int) $formData['show_title'];
$block->custom_class = (string) ($formData['custom_class'] ?? '');
$block->content = (string) $formData['content'];
$block->active = (int) $formData['active'];
@@ -22,6 +22,7 @@ final class CustomHtmlBlockDataProvider implements FormDataProviderInterface
'title' => $block->title,
'type' => $block->type,
'show_title' => (bool) $block->show_title,
'custom_class' => $block->custom_class,
'content' => $block->content,
'faqs' => $this->loadFaqs((int) $id),
'active' => (bool) $block->active,
@@ -35,6 +36,7 @@ final class CustomHtmlBlockDataProvider implements FormDataProviderInterface
'title' => '',
'type' => 'html',
'show_title' => false,
'custom_class' => '',
'content' => '',
'faqs' => [],
'active' => true,
+5
View File
@@ -48,6 +48,11 @@ class CustomHtmlBlockType extends AbstractType
'label' => $this->translator->trans('Show title', [], 'Modules.Wscustomhtml.Admin'),
'required' => false,
])
->add('custom_class', TextType::class, [
'label' => $this->translator->trans('Custom classes', [], 'Modules.Wscustomhtml.Admin'),
'required' => false,
'attr' => ['placeholder' => 'col-md-6'],
])
->add('content', TextareaType::class, [
'label' => $this->translator->trans('Content', [], 'Modules.Wscustomhtml.Admin'),
'required' => false,
+5 -1
View File
@@ -22,6 +22,9 @@ class CustomHtmlBlock extends ObjectModel
/** @var string */
public $content = '';
/** @var string */
public $custom_class = '';
/** @var int */
public $active = 1;
@@ -45,6 +48,7 @@ class CustomHtmlBlock extends ObjectModel
'title' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 256],
'type' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 32],
'content' => ['type' => self::TYPE_HTML, 'validate' => 'isCleanHtml'],
'custom_class' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 128],
'active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'show_title' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'ordering' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
@@ -62,7 +66,7 @@ class CustomHtmlBlock extends ObjectModel
}
$result = Db::getInstance()->executeS(
'SELECT `id_ws_customhtml`, `id_ws_customhtml_group`, `title`, `type`, `content`, `active`, `show_title`, `ordering`
'SELECT `id_ws_customhtml`, `id_ws_customhtml_group`, `title`, `type`, `content`, `custom_class`, `active`, `show_title`, `ordering`
FROM `' . _DB_PREFIX_ . 'ws_customhtml`
WHERE `id_ws_customhtml_group` IN (' . implode(',', $groupIds) . ')
AND `active` = 1
+8
View File
@@ -9,6 +9,14 @@ use Context;
trait DisplayHooks
{
public function hookActionFrontControllerSetMedia(): void
{
$this->context->controller->registerStylesheet(
'ws_customhtml_front',
'/modules/' . $this->name . '/views/css/front/customhtml.css'
);
}
public function renderWidget($hookName = null, array $configuration = [])
{
if (empty($hookName)) {
+1
View File
@@ -64,6 +64,7 @@ trait Install
`title` VARCHAR(256) NOT NULL DEFAULT \'\',
`type` VARCHAR(32) NOT NULL DEFAULT \'html\',
`content` LONGTEXT,
`custom_class` VARCHAR(128) NOT NULL DEFAULT \'\',
`active` TINYINT(1) NOT NULL DEFAULT 1,
`show_title` TINYINT(1) NOT NULL DEFAULT 0,
`ordering` INT(11) NOT NULL DEFAULT 0,