Skip to content

Commit 90f8a45

Browse files
committed
add oncopy callback to fill relation table
1 parent c6d0c75 commit 90f8a45

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ $GLOBALS['TL_DCA']['tl_my_extension']['fields']['my_tags'] = [
113113
`foreignKey`, `options_callback`and `save_callback`are mandatory.
114114
In the `eval` section we add a class called `tags` - this is also needed for the JavaScript handling. There are some more options which can be defined in `eval`.
115115

116+
If you also want to enable copying tags while copying an entry, you must add following code to your dca
117+
```php
118+
$GLOBALS['TL_DCA']['tl_my_extension']['config']['oncopy_callback'][] = ['numero2_tags.listener.data_container.tags', 'onCopy'];
119+
```
120+
This will copy all tags for the copied entry, except the fields excluded by `doNotCopy`.
121+
122+
116123
### Eval-Options
117124

118125
| Option | Description |

src/EventListener/DataContainer/TagsListener.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Benny Born <benny.born@numero2.de>
77
* @author Michael Bösherz <michael.boesherz@numero2.de>
88
* @license LGPL-3.0-or-later
9-
* @copyright Copyright (c) 2025, numero2 - Agentur für digitales Marketing GbR
9+
* @copyright Copyright (c) 2026, numero2 - Agentur für digitales Marketing GbR
1010
*/
1111

1212

@@ -219,6 +219,49 @@ public function loadTags( $varValue, DataContainer $dc ): ?array {
219219
}
220220

221221

222+
/**
223+
* On copy also copy all entries in tag relation table, except for the fields with doNotCopy
224+
*
225+
* @param mixed $insertId
226+
* @param Contao\DataContainer $dc
227+
*/
228+
public function onCopy( $insertId, DataContainer $dc ): void {
229+
230+
$srcId = $dc->id;
231+
$table = $dc->table;
232+
$destId = $insertId;
233+
234+
$t = TagsRelModel::getTable();
235+
$rows = $this->connection->executeQuery(
236+
"SELECT * FROM $t WHERE ptable=:ptable AND pid=:pid"
237+
, ['ptable'=> $table,'pid'=>$srcId]
238+
)->fetchAllAssociative();
239+
240+
// get present rows as md5
241+
$present = $this->connection->executeQuery(
242+
"SELECT MD5(CONCAT_WS(';',tag_id,pid,ptable,field)) FROM $t WHERE ptable=:ptable AND pid=:pid"
243+
, ['ptable'=> $table,'pid'=>$destId]
244+
)->fetchFirstColumn();
245+
246+
foreach( $rows as $row ) {
247+
248+
if( $GLOBALS['TL_DCA']['tl_news']['fields'][$row['field']]['eval']['doNotCopy'] ?? false ) {
249+
continue;
250+
}
251+
252+
$md5NewRow = md5(implode(';', [$row['tag_id'], $destId, $row['ptable'], $row['field']]));
253+
if( in_array($md5NewRow, $present) ) {
254+
continue;
255+
}
256+
257+
$this->connection->executeStatement(
258+
"INSERT INTO $t (tag_id, pid, ptable, field) VALUES (:tagId, :pid, :ptable, :field)"
259+
, ['tagId'=>$row['tag_id'], 'pid'=>$destId, 'ptable'=>$row['ptable'], 'field'=>$row['field']]
260+
);
261+
}
262+
}
263+
264+
222265
/**
223266
* Add tag merge button to the select section and handle it
224267
*

src/Resources/contao/dca/tl_calendar_events.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Benny Born <benny.born@numero2.de>
77
* @author Michael Bösherz <michael.boesherz@numero2.de>
88
* @license LGPL-3.0-or-later
9-
* @copyright Copyright (c) 2024, numero2 - Agentur für digitales Marketing GbR
9+
* @copyright Copyright (c) 2026, numero2 - Agentur für digitales Marketing GbR
1010
*/
1111

1212

@@ -16,6 +16,8 @@
1616

1717
if( class_exists(ContaoCalendarBundle::class) ) {
1818

19+
$GLOBALS['TL_DCA']['tl_calendar_events']['config']['oncopy_callback'][] = ['numero2_tags.listener.data_container.tags', 'onCopy'];
20+
1921
$pm = PaletteManipulator::create()
2022
->addLegend('tags_legend', 'details_legend', 'before')
2123
->addField('tags', 'tags_legend', 'append');

src/Resources/contao/dca/tl_news.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Benny Born <benny.born@numero2.de>
77
* @author Michael Bösherz <michael.boesherz@numero2.de>
88
* @license LGPL-3.0-or-later
9-
* @copyright Copyright (c) 2024, numero2 - Agentur für digitales Marketing GbR
9+
* @copyright Copyright (c) 2026, numero2 - Agentur für digitales Marketing GbR
1010
*/
1111

1212

@@ -16,6 +16,8 @@
1616

1717
if( class_exists(ContaoNewsBundle::class) ) {
1818

19+
$GLOBALS['TL_DCA']['tl_news']['config']['oncopy_callback'][] = ['numero2_tags.listener.data_container.tags', 'onCopy'];
20+
1921
$pm = PaletteManipulator::create()
2022
->addLegend('tags_legend', 'teaser_legend', 'before')
2123
->addField('tags', 'tags_legend', 'append');

0 commit comments

Comments
 (0)