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

heimrichhannot / contao-utils-bundle / 15155773938

21 May 2025 07:07AM UTC coverage: 79.64% (+0.03%) from 79.614%
15155773938

push

github

web-flow
Forwardport alias field (#102)

* forwardport alias field

* fix ci

37 of 46 new or added lines in 3 files covered. (80.43%)

1150 of 1444 relevant lines covered (79.64%)

3.48 hits per line

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

77.42
/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->aliasExistCallback)) {
1✔
27
            $field['save_callback'][] = $registration->aliasExistCallback;
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
        // Generate an alias if there is none
44
        if (!$value) {
3✔
45
            $value = $this->container->get('contao.slug')->generate(
1✔
46
                /** @phpstan-ignore property.notFound */
47
                (string)$dc->activeRecord->title,
1✔
48
                /** @phpstan-ignore property.notFound */
49
                (int)$dc->activeRecord->pid,
1✔
50
                $aliasExists
1✔
51
            );
1✔
52
        } elseif (preg_match('/^[1-9]\d*$/', (string) $value)) {
2✔
53
            throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasNumeric'], $value));
1✔
54
        } elseif ($aliasExists($value)) {
1✔
55
            throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $value));
1✔
56
        }
57

58
        return $value;
1✔
59
    }
60

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

72

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