Skip to content

Commit 2491486

Browse files
committed
fix handling of label_callback in tl_page if other extensions are also providing callbacks
1 parent c339394 commit 2491486

File tree

1 file changed

+56
-24
lines changed

1 file changed

+56
-24
lines changed

src/EventListener/DataContainer/PageListener.php

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Benny Born <[email protected]>
77
* @author Michael Bösherz <[email protected]>
88
* @license LGPL-3.0-or-later
9-
* @copyright Copyright (c) 2024, numero2 - Agentur für digitales Marketing GbR
9+
* @copyright Copyright (c) 2025, numero2 - Agentur für digitales Marketing GbR
1010
*/
1111

1212

@@ -60,51 +60,83 @@ public function addBackendHelperFields( $dc ) {
6060
->addField(['bh_info'], 'expert_legend', 'append')
6161
;
6262

63-
foreach( $GLOBALS['TL_DCA']['tl_page']['palettes'] as $key => $value ) {
63+
foreach( $GLOBALS['TL_DCA'][$dc->table]['palettes'] as $key => $value ) {
6464

6565
if( in_array($key, ['__selector__', 'default']) ) {
6666
continue;
6767
}
6868

69-
$pm->applyToPalette($key, 'tl_page');
69+
$pm->applyToPalette($key, $dc->table);
7070
}
7171
}
7272

7373

7474
/**
75-
* Add backend helper information to the label
75+
* Sets the new label callback with respect to any previously added ones
7676
*
77-
* @param array $row
78-
* @param string $label
79-
* @param Contao\DataContainer $dc
80-
* @param string $imageAttribute
81-
* @param boolean $blnReturnImage
82-
* @param boolean $blnProtected
83-
* @param boolean $isVisibleRootTrailPage
84-
*
85-
* @return string
77+
* @param Contao\DataContainer $dc
8678
*
87-
* @Callback(table="tl_page", target="list.label.label")
79+
* @Callback(table="tl_page", target="config.onload")
8880
*/
89-
public function addBackendHelperInfos( $row, $label, DataContainer|null $dc=null, $imageAttribute='', $blnReturnImage=false, $blnProtected=false, $isVisibleRootTrailPage=false ) {
81+
public function setLabelCallback( $dc ) {
82+
83+
$previousCallback = $GLOBALS['TL_DCA'][$dc->table]['list']['label']['label_callback'] ?? null;
84+
85+
$callback = [$this, 'addBackendHelperInfos'];
9086

91-
$t = System::importStatic('tl_page');
87+
$GLOBALS['TL_DCA'][$dc->table]['list']['label']['label_callback'] = function () use ($callback, $previousCallback) {
9288

93-
if( method_exists($t, 'addIcon') ) {
94-
$defaultLabel = $t->addIcon(...func_get_args());
89+
$args = \func_get_args();
90+
$result = null;
91+
92+
if( \is_callable($previousCallback) || \is_array($previousCallback) ) {
93+
$result = $this->executeCallback($previousCallback, $args);
94+
}
95+
96+
return $this->executeCallback($callback, [$args, $result]);
97+
};
98+
}
99+
100+
101+
/**
102+
* @param callable|array<string, string> $callback
103+
* @param array<mixed> $args
104+
*
105+
* @return mixed
106+
*/
107+
private function executeCallback( $callback, array $args ) {
108+
109+
if( \is_array($callback) ) {
110+
111+
return \call_user_func_array(
112+
[System::importStatic($callback[0]), $callback[1]],
113+
$args,
114+
);
95115
}
96116

117+
return $callback(...$args);
118+
}
119+
120+
121+
/**
122+
* Add backend helper information to the label
123+
*
124+
* @param array $args
125+
* @param string $label
126+
*
127+
* @return string
128+
*/
129+
public function addBackendHelperInfos( array $args, string $label ): string {
130+
97131
$request = $this->requestStack->getCurrentRequest();
98132
if( !$request || !$this->scopeMatcher->isBackendRequest($request) ) {
99-
return $defaultLabel;
133+
return $label;
100134
}
101135

102-
if( !empty($row['bh_info']) ) {
103-
104-
$info = '<span class="bh_info">' . $row['bh_info'] . '</span>';
105-
$defaultLabel = preg_replace("%</a>$%", $info.'</a>', $defaultLabel);
136+
if( !empty($args[0]['bh_info']) ) {
137+
$label .= '<span class="bh_info">' . $args[0]['bh_info'] . '</span>';
106138
}
107139

108-
return $defaultLabel;
140+
return $label;
109141
}
110142
}

0 commit comments

Comments
 (0)