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

heimrichhannot / contao-utils-bundle / 21213682826

21 Jan 2026 02:40PM UTC coverage: 78.552% (-0.2%) from 78.761%
21213682826

Pull #106

github

koertho
update tests and fix workflow
Pull Request #106: Allow customize alias title field

11 of 15 new or added lines in 2 files covered. (73.33%)

1 existing line in 1 file now uncovered.

1161 of 1478 relevant lines covered (78.55%)

3.41 hits per line

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

77.78
/src/EventListener/DcaField/AliasDcaFieldListener.php
1
<?php
2

3
namespace HeimrichHannot\UtilsBundle\EventListener\DcaField;
4

5
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
6
use Contao\CoreBundle\Framework\ContaoFramework;
7
use Contao\CoreBundle\Slug\Slug;
8
use Contao\Database;
9
use Contao\DataContainer;
10
use HeimrichHannot\UtilsBundle\Dca\AliasField;
11
use HeimrichHannot\UtilsBundle\Dca\AliasFieldConfiguration;
12

13
class AliasDcaFieldListener extends AbstractDcaFieldListener
14
{
15
    #[AsHook('loadDataContainer')]
16
    public function onLoadDataContainer(string $table): void
17
    {
18
        if (!isset(AliasField::getRegistrations()[$table])) {
1 ✔
19
            return;
1 ✔
20
        }
21

22
        /** @var AliasFieldConfiguration $registration */
23
        $registration = AliasField::getRegistrations()[$table];
1 ✔
24

25
        $field = AliasField::getField();
1 ✔
26
        if (is_array($registration->generateAliasCallback)) {
1 ✔
27
            $field['save_callback'][] = $registration->generateAliasCallback;
1 ✔
28
        }
29

30
        $this->applyDefaultFieldAdjustments($field, $registration);
1 ✔
31

32
        $GLOBALS['TL_DCA'][$table]['fields'][$registration->fieldName] = $field;
1 ✔
33
    }
34

35
    public function onFieldsAliasSaveCallback($value, DataContainer $dc)
36
    {
37
        $framework = $this->container->get('contao.framework');
3 ✔
38
        $aliasExists = (static fn(string $alias): bool => $framework->createInstance(Database::class)
3 ✔
39
                ->prepare("SELECT id FROM $dc->table WHERE alias=? AND id!=?")
3 ✔
40
                ->execute($alias, $dc->id)
3 ✔
41
                ->numRows > 0);
1 ✔
42

43
        if (method_exists($dc, 'getCurrentRecord')) {
3 ✔
NEW
44
            $row = $dc->getCurrentRecord();
×
45
        } else {
46
            /**
47
             * Contao 4 fallback
48
             * @todo Remove when contao 5 only
49
             */
50
            $row = $dc->activeRecord->row();
3 ✔
51
        }
52

53
        // Generate an alias if there is none
54
        if (!$value) {
3 ✔
55
            /** @var ?AliasFieldConfiguration $fieldConfiguration */
56
            $fieldConfiguration = AliasField::getRegistrations()[$dc->table] ?? null;
1 ✔
57
            $titleField = $fieldConfiguration?->titleField ?? 'title';
1 ✔
58

59
            $value = $this->container->get('contao.slug')->generate(
1 ✔
60
                (string)$row[$titleField],
1 ✔
61
                (int)$row['pid'],
1 ✔
62
                $aliasExists
1 ✔
63
            );
1 ✔
64
        } elseif (preg_match('/^[1-9]\d*$/', (string)$value)) {
2 ✔
65
            throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasNumeric'], $value));
1 ✔
66
        } elseif ($aliasExists($value)) {
1 ✔
67
            throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $value));
1 ✔
68
        }
69

70
        return $value;
1 ✔
71
    }
72

73
    public static function getSubscribedServices(): array
74
    {
75
        return array_merge(
×
76
            [
×
77
                'contao.slug' => Slug::class,
×
78
                'contao.framework' => ContaoFramework::class,
×
79
            ],
×
80
            parent::getSubscribedServices()
×
81
        );
×
82
    }#
83

84

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

© 2026 Coveralls, Inc