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

heimrichhannot / contao-utils-bundle / 16367830923

18 Jul 2025 10:02AM UTC coverage: 79.752% (+0.03%) from 79.724%
16367830923

push

github

koertho
moved method in DcaAuthorListener

23 of 29 new or added lines in 1 file covered. (79.31%)

1158 of 1452 relevant lines covered (79.75%)

3.46 hits per line

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

75.86
/src/EventListener/DcaField/DcaAuthorListener.php
1
<?php
2

3
namespace HeimrichHannot\UtilsBundle\EventListener\DcaField;
4

5
use Contao\BackendUser;
6
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
7
use Contao\DataContainer;
8
use Contao\FrontendUser;
9
use HeimrichHannot\UtilsBundle\Dca\AuthorField;
10
use HeimrichHannot\UtilsBundle\Dca\AuthorFieldConfiguration;
11
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
12

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

22
        $options = AuthorField::getRegistrations()[$table];
1✔
23
        $authorFieldName = $this->getAuthorFieldName($options);
1✔
24
        $authorField = $this->createAuthorField($options);
1✔
25

26
        $GLOBALS['TL_DCA'][$table]['fields'][$authorFieldName] = $authorField;
1✔
27
        $GLOBALS['TL_DCA'][$table]['config']['oncopy_callback'][] = [self::class, 'onConfigCopyCallback'];
1✔
28
    }
29

30

31
    public function onConfigCopyCallback(int $insertId, DataContainer $dc): void
32
    {
33
        $options = AuthorField::getRegistrations()[$dc->table];
1✔
34
        $authorFieldName = $this->getAuthorFieldName($options);
1✔
35

36
        $model = $this->getModelInstance($dc->table, $insertId);
1✔
37
        if (!$model) {
1✔
38
            return;
×
39
        }
40

41
        /** @var TokenStorageInterface $tokenStorage */
42
        $tokenStorage = $this->container->get('token_storage');
1✔
43
        $user = $tokenStorage->getToken()?->getUser();
1✔
44

45
        $model->{$authorFieldName} = 0;
1✔
46

47
        if (AuthorField::TYPE_USER === $options->getType()) {
1✔
48
            if ($user instanceof BackendUser) {
1✔
49
                $model->{$authorFieldName} = $user->id;
×
50
            }
51
        } elseif (AuthorField::TYPE_MEMBER === $options->getType()) {
×
52
            if ($user instanceof FrontendUser) {
×
53
                $model->{$authorFieldName} = $user->id;
×
54
            }
55
        }
56
        $model->save();
1✔
57
    }
58

59
    /**
60
     * @param AuthorFieldConfiguration $options
61
     * @return string
62
     */
63
    protected function getAuthorFieldName(AuthorFieldConfiguration $options): string
64
    {
65
        if (!$options->hasFieldNamePrefix()) {
2✔
66
            return 'author';
2✔
67
        }
68
        if (str_ends_with($options->getFieldNamePrefix(), '_')) {
1✔
69
            return $options->getFieldNamePrefix() . 'author';
1✔
70
        } else {
71
            return $options->getFieldNamePrefix() . 'Author';
1✔
72
        }
73
    }
74

75
    public static function getSubscribedServices(): array
76
    {
77
        $services = parent::getSubscribedServices();
×
78
        $services['token_storage'] = TokenStorageInterface::class;
×
79
        return $services;
×
80
    }
81

82
    public function createAuthorField(AuthorFieldConfiguration $options): array
83
    {
84
        /** @var TokenStorageInterface $tokenStorage */
85
        $tokenStorage = $this->container->get('token_storage');
1✔
86
        $user = $tokenStorage->getToken()?->getUser();
1✔
87

88
        $authorField = [
1✔
89
            'inputType' => 'select',
1✔
90
            'eval' => [
1✔
91
                'doNotCopy' => true,
1✔
92
                'mandatory' => true,
1✔
93
                'chosen' => true,
1✔
94
                'includeBlankOption' => true,
1✔
95
                'tl_class' => 'w50'
1✔
96
            ],
1✔
97
            'sql' => "int(10) unsigned NOT NULL default 0",
1✔
98
        ];
1✔
99

100
        $this->applyDefaultFieldAdjustments($authorField, $options);
1✔
101

102
        if ($options->isUseDefaultLabel()) {
1✔
103
            $authorField['label'] = &$GLOBALS['TL_LANG']['MSC']['utilsBundle']['author'];
1✔
104
        }
105

106
        $authorField['default'] = 0;
1✔
107
        if (AuthorField::TYPE_USER === $options->getType()) {
1✔
108
            if ($user instanceof BackendUser) {
1✔
NEW
109
                $authorField['default'] = $user->id;
×
110
            }
111
            $authorField['foreignKey'] = 'tl_user.name';
1✔
112
            $authorField['relation'] = ['type' => 'hasOne', 'load' => 'lazy'];
1✔
NEW
113
        } elseif (AuthorField::TYPE_MEMBER === $options->getType()) {
×
NEW
114
            if ($user instanceof FrontendUser) {
×
NEW
115
                $authorField['default'] = $user->id;
×
116
            }
NEW
117
            $authorField['foreignKey'] = "tl_member.CONCAT(firstname,' ',lastname)";
×
NEW
118
            $authorField['relation'] = ['type' => 'hasOne', 'load' => 'lazy'];
×
119
        }
120
        return $authorField;
1✔
121
    }
122
}
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