• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 23251086155

18 Mar 2026 02:55PM UTC coverage: 78.692% (-0.3%) from 78.958%
23251086155

push

github

koertho
DateAddedField - catch exception when model is preventSave

1 of 6 new or added lines in 1 file covered. (16.67%)

1167 of 1483 relevant lines covered (78.69%)

3.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/EventListener/DcaField/DateAddedFieldListener.php
1
<?php
2

3
namespace HeimrichHannot\UtilsBundle\EventListener\DcaField;
4

5
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
6
use Contao\DataContainer;
7
use Doctrine\DBAL\Connection;
8
use HeimrichHannot\UtilsBundle\Dca\DateAddedField;
9

10
class DateAddedFieldListener extends AbstractDcaFieldListener
11
{
12
    #[AsHook("loadDataContainer")]
13
    public function onLoadDataContainer(string $table): void
14
    {
15
        if (!isset(DateAddedField::getRegistrations()[$table])) {
6✔
16
            return;
1✔
17
        }
18

19
        $GLOBALS['TL_DCA'][$table]['config']['onload_callback'][] = [self::class, 'onLoadCallback'];
6✔
20
        $GLOBALS['TL_DCA'][$table]['config']['oncopy_callback'][] = [self::class, 'onCopyCallback'];
6✔
21

22
        $field = [
6✔
23
            'label' => &$GLOBALS['TL_LANG']['MSC']['dateAdded'],
6✔
24
            'eval' => ['rgxp' => 'datim', 'doNotCopy' => true],
6✔
25
            'sql' => "int(10) unsigned NOT NULL default '0'",
6✔
26
        ];
6✔
27

28
        $this->applyDefaultFieldAdjustments($field, DateAddedField::getRegistrations()[$table]);
6✔
29

30
        $GLOBALS['TL_DCA'][$table]['fields']['dateAdded'] = $field;
6✔
31
    }
32

33
    public function onLoadCallback(?DataContainer $dc = null): void
34
    {
35
        if (!$dc || !$dc->id) {
1✔
36
            return;
1✔
37
        }
38

39
        $model = $this->getModelInstance($dc->table, (int)$dc->id);
1✔
40
        if (!$model || $model->dateAdded > 0) {
1✔
41
            return;
1✔
42
        }
43

44
        $model->dateAdded = time();
1✔
45
        try {
46
            $model->save();
1✔
NEW
47
        } catch (\RuntimeException) {
×
NEW
48
            $this->container->get(Connection::class)->update($model::getTable(), ['dateAdded' => $model->dateAdded], ['id' => $model->id]);
×
49
        }
50
    }
51

52
    public function onCopyCallback(int $insertId, DataContainer $dc): void
53
    {
54
        if (!$dc->id) {
1✔
55
            return;
1✔
56
        }
57

58
        $model = $this->getModelInstance($dc->table, $insertId);
1✔
59
        if (!$model || $model->dateAdded > 0) {
1✔
60
            return;
1✔
61
        }
62

63
        $model->dateAdded = time();
1✔
64
        $model->save();
1✔
65
    }
66

67
    public static function getSubscribedServices(): array
68
    {
NEW
69
        $services = parent::getSubscribedServices();
×
NEW
70
        $services[] = Connection::class;
×
NEW
71
        return $services;
×
72
    }
73

74

75
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc