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

heimrichhannot / contao-utils-bundle / 15155552365

21 May 2025 06:55AM UTC coverage: 25.946% (+0.5%) from 25.433%
15155552365

push

github

web-flow
Add alias field (#101)

* add alias field

* add tests

* fix php 8 only code

* remove label

45 of 54 new or added lines in 4 files covered. (83.33%)

1542 of 5943 relevant lines covered (25.95%)

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 HeimrichHannot\UtilsBundle\Dca\AliasField;
11
use HeimrichHannot\UtilsBundle\Dca\AliasFieldConfiguration;
12

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

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

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

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

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

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

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

60
        return $value;
1✔
61
    }
62

63
    public static function getSubscribedServices(): array
64
    {
NEW
65
        return array_merge(
×
NEW
66
            [
×
NEW
67
                'contao.slug' => Slug::class,
×
NEW
68
                'contao.framework' => ContaoFramework::class,
×
NEW
69
            ],
×
NEW
70
            parent::getSubscribedServices()
×
NEW
71
        );
×
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

© 2025 Coveralls, Inc