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

heimrichhannot / contao-utils-bundle / 15155347519

21 May 2025 06:42AM UTC coverage: 25.959% (+0.5%) from 25.433%
15155347519

Pull #101

github

koertho
add tests
Pull Request #101: Add alias field

46 of 55 new or added lines in 4 files covered. (83.64%)

1543 of 5944 relevant lines covered (25.96%)

1.56 hits per line

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

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

3
namespace HeimrichHannot\UtilsBundle\EventListener\DcaField;
4

5
use Contao\CoreBundle\Framework\ContaoFramework;
6
use Contao\CoreBundle\ServiceAnnotation\Hook;
7
use Contao\CoreBundle\Slug\Slug;
8
use Contao\Database;
9
use Contao\DataContainer;
10
use Contao\Validator;
11
use HeimrichHannot\UtilsBundle\Dca\AliasField;
12
use HeimrichHannot\UtilsBundle\Dca\AliasFieldConfiguration;
13

14
class AliasDcaFieldListener extends AbstractDcaFieldListener
15
{
16
    /**
17
     * @Hook("loadDataContainer")
18
     */
19
    public function onLoadDataContainer(string $table): void
20
    {
21
        if (!isset(AliasField::getRegistrations()[$table])) {
1✔
22
            return;
1✔
23
        }
24

25
        /** @var AliasFieldConfiguration $registration */
26
        $registration = AliasField::getRegistrations()[$table];
1✔
27

28
        $field = AliasField::getField();
1✔
29
        if (is_array($registration->aliasExistCallback)) {
1✔
30
            $field['save_callback'][] = $registration->aliasExistCallback;
1✔
31
        }
32

33
        $this->applyDefaultFieldAdjustments($field, $registration);
1✔
34

35
        $GLOBALS['TL_DCA'][$table]['fields'][$registration->fieldName] = $field;
1✔
36
    }
37

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

48
        // Generate an alias if there is none
49
        if (!$value) {
3✔
50
            $value = $this->container->get('contao.slug')->generate(
1✔
51
                (string)$dc->activeRecord->title,
1✔
52
                (int)$dc->activeRecord->pid,
1✔
53
                $aliasExists
1✔
54
            );
1✔
55
        } elseif (preg_match('/^[1-9]\d*$/', $value)) {
2✔
56
            throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasNumeric'], $value));
1✔
57
        } elseif ($aliasExists($value)) {
1✔
58
            throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $value));
1✔
59
        }
60

61
        return $value;
1✔
62
    }
63

64
    public static function getSubscribedServices(): array
65
    {
NEW
66
        return array_merge(
×
NEW
67
            [
×
NEW
68
                'contao.slug' => Slug::class,
×
NEW
69
                'contao.framework' => ContaoFramework::class,
×
NEW
70
            ],
×
NEW
71
            parent::getSubscribedServices()
×
NEW
72
        );
×
73
    }#
74

75

76
}
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