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

heimrichhannot / contao-utils-bundle / 6493786151

12 Oct 2023 09:23AM UTC coverage: 21.909% (-0.2%) from 22.128%
6493786151

push

github

web-flow
Add new dca author field mechanic (#69)

* add new author field mechanic and deprecate DcaUtil::addAuthorFieldAndCallback()

* adjust readme

54 of 54 new or added lines in 2 files covered. (100.0%)

1196 of 5459 relevant lines covered (21.91%)

1.55 hits per line

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

0.0
/src/EventListener/DcaAuthorListener.php
1
<?php
2

3
namespace HeimrichHannot\UtilsBundle\EventListener;
4

5
use Contao\BackendUser;
6
use Contao\CoreBundle\Framework\ContaoFramework;
7
use Contao\CoreBundle\ServiceAnnotation\Hook;
8
use Contao\DataContainer;
9
use Contao\FrontendUser;
10
use Contao\Model;
11
use HeimrichHannot\UtilsBundle\Dca\AuthorField;
12
use Symfony\Component\Security\Core\Security;
13

14
class DcaAuthorListener
15
{
16
    private ContaoFramework $framework;
17
    private Security $security;
18

19
    public function __construct(ContaoFramework $framework, Security $security)
20
    {
21
        $this->framework = $framework;
×
22
        $this->security = $security;
×
23
    }
24

25

26
    /**
27
     * @Hook("loadDataContainer")
28
     */
29
    public function onLoadDataContainer(string $table): void
30
    {
31
        if (!isset(AuthorField::getRegistrations()[$table])) {
×
32
            return;
×
33
        }
34

35
        $options = $this->getOptions($table);
×
36

37
        $authorFieldName = empty($options['fieldNamePrefix']) ? 'author' : $options['fieldNamePrefix'].'Author';
×
38

39
        $authorField = [
×
40
            'exclude' => $options['exclude'],
×
41
            'search' => $options['search'],
×
42
            'filter' => $options['filter'],
×
43
            'inputType' => 'select',
×
44
            'eval' => [
×
45
                'doNotCopy' => true,
×
46
                'mandatory' => true,
×
47
                'chosen' => true,
×
48
                'includeBlankOption' => true,
×
49
                'tl_class' => 'w50'
×
50
            ],
×
51
            'sql' => "int(10) unsigned NOT NULL default 0",
×
52
        ];
×
53

54
        if ($options['useDefaultLabel']) {
×
55
            $authorField['label'] = &$GLOBALS['TL_LANG']['MSC']['utilsBundle']['author'];
×
56
        }
57

58
        if (AuthorField::TYPE_USER === $options['type']) {
×
59
            $authorField['default'] = BackendUser::getInstance()->id;
×
60
            $authorField['foreignKey'] = 'tl_user.name';
×
61
            $authorField['relation'] = ['type'=>'hasOne', 'load'=>'lazy'];
×
62
        } elseif (AuthorField::TYPE_MEMBER === $options['type']) {
×
63
            $authorField['default'] = FrontendUser::getInstance()->id;
×
64
            $authorField['foreignKey'] = "tl_member.CONCAT(firstname,' ',lastname)";
×
65
            $authorField['relation'] = ['type'=>'hasOne', 'load'=>'lazy'];
×
66
        } else {
67
            $authorField['default'] = 0;
×
68
        }
69

70
        $GLOBALS['TL_DCA'][$table]['fields'][$authorFieldName] = $authorField;
×
71
        $GLOBALS['TL_DCA'][$table]['config']['oncopy_callback'][] = [self::class, 'onConfigCopyCallback'];
×
72
    }
73

74

75
    public function onConfigCopyCallback(int $insertId, DataContainer $dc): void
76
    {
77
        $options = $this->getOptions($dc->table);
×
78
        $authorFieldName = empty($options['fieldNamePrefix']) ? 'author' : $options['fieldNamePrefix'].'Author';
×
79

80
        /** @var class-string<Model> $modelClass */
81
        $modelClass = $this->framework->getAdapter(Model::class)->getClassFromTable($dc->table);
×
82
        $model = $modelClass::findByPk($insertId);
×
83
        if (!$model) {
×
84
            return;
×
85
        }
86

87
        if (AuthorField::TYPE_USER === $options['type']) {
×
88
            $model->{$authorFieldName} = BackendUser::getInstance()->id;
×
89
        } elseif (AuthorField::TYPE_MEMBER === $options['type']) {
×
90
            $model->{$authorFieldName} = FrontendUser::getInstance()->id;
×
91
        } else {
92
            $model->{$authorFieldName} = 0;
×
93
        }
94
    }
95

96
    /**
97
     * @param string $table
98
     * @return array|bool[]|string[]
99
     */
100
    protected function getOptions(string $table): array
101
    {
102
        return array_merge([
×
103
            'type' => AuthorField::TYPE_USER,
×
104
            'fieldNamePrefix' => '',
×
105
            'useDefaultLabel' => true,
×
106
            'exclude' => true,
×
107
            'search' => true,
×
108
            'filter' => true,
×
109
        ], AuthorField::getRegistrations()[$table]);
×
110
    }
111
}
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