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

heimrichhannot / contao-utils-bundle / 15140811854

20 May 2025 02:51PM UTC coverage: 25.295% (-0.1%) from 25.433%
15140811854

Pull #101

github

koertho
add alias field
Pull Request #101: Add alias field

6 of 53 new or added lines in 4 files covered. (11.32%)

1503 of 5942 relevant lines covered (25.29%)

1.55 hits per line

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

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

3
namespace HeimrichHannot\UtilsBundle\EventListener\DcaField;
4

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

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

23
        /** @var AliasFieldConfiguration $registration */
NEW
24
        $registration = AliasField::getRegistrations()[$table];
×
25

NEW
26
        $field = AliasField::getField();
×
NEW
27
        if (is_array($registration->aliasExistCallback)) {
×
NEW
28
            $field['save_callback'][] = $registration->aliasExistCallback;
×
29
        }
30

NEW
31
        $this->applyDefaultFieldAdjustments($field, $registration);
×
32

NEW
33
        $GLOBALS['TL_DCA'][$table]['fields'][$registration->fieldName] = $field;
×
34
    }
35

36
    public function onFieldsAliasSaveCallback(mixed $value, DataContainer $dc): mixed
37
    {
NEW
38
        $aliasExists = static function (string $alias) use ($dc): bool {
×
NEW
39
            return Database::getInstance()
×
NEW
40
                    ->prepare("SELECT $dc->table FROM tl_article WHERE alias=? AND id!=?")
×
NEW
41
                    ->execute($alias, $dc->id)
×
NEW
42
                    ->numRows > 0;
×
NEW
43
        };
×
44

45
        // Generate an alias if there is none
NEW
46
        if (!$value) {
×
NEW
47
            $value = $this->container->get(Slug::class)->generate(
×
NEW
48
                (string)$dc->activeRecord->title,
×
NEW
49
                (int)$dc->activeRecord->pid,
×
NEW
50
                $aliasExists
×
NEW
51
            );
×
NEW
52
        } elseif (preg_match('/^[1-9]\d*$/', $value)) {
×
NEW
53
            throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasNumeric'], $value));
×
NEW
54
        } elseif ($aliasExists($value)) {
×
NEW
55
            throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $value));
×
56
        }
57

NEW
58
        return $value;
×
59
    }
60

61
    public static function getSubscribedServices(): array
62
    {
NEW
63
        return array_merge(
×
NEW
64
            [
×
NEW
65
                'contao.slug' => Slug::class,
×
NEW
66
            ],
×
NEW
67
            parent::getSubscribedServices()
×
NEW
68
        );
×
69
    }#
70

71

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