Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added universal inheritance for label and child record callbacks (#87)
- Loading branch information
Showing
2 changed files
with
117 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
library/Terminal42/ChangeLanguage/Helper/LabelCallback.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Terminal42\ChangeLanguage\Helper; | ||
|
||
use Contao\Controller; | ||
use Contao\System; | ||
|
||
class LabelCallback | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
private $previous; | ||
|
||
/** | ||
* Registers callback for given table. | ||
* | ||
* @param string $table | ||
* @param callable $callback | ||
*/ | ||
public function register($table, callable $callback) | ||
{ | ||
Controller::loadDataContainer($table); | ||
|
||
$chain = function () use ($callback) { | ||
$args = func_get_args(); | ||
$result = null; | ||
|
||
if (is_callable($this->previous)) { | ||
$result = $this->executeCallback($this->previous, $args); | ||
} | ||
|
||
return $this->executeCallback($callback, [$args, $result]); | ||
}; | ||
|
||
if (4 === $GLOBALS['TL_DCA'][$table]['list']['sorting']['mode']) { | ||
$this->previous = $GLOBALS['TL_DCA'][$table]['list']['sorting']['child_record_callback']; | ||
$GLOBALS['TL_DCA'][$table]['list']['sorting']['child_record_callback'] = $chain; | ||
} else { | ||
$this->previous = $GLOBALS['TL_DCA'][$table]['list']['label']['label_callback']; | ||
$GLOBALS['TL_DCA'][$table]['list']['label']['label_callback'] = $chain; | ||
} | ||
} | ||
|
||
/** | ||
* @param \Closure|array $callback | ||
* @param array $args | ||
* | ||
* @return mixed | ||
*/ | ||
private function executeCallback($callback, array $args) | ||
{ | ||
// Support Contao's getInstance() method when callback is an array | ||
if (is_array($callback)) { | ||
return call_user_func_array( | ||
[System::importStatic($callback[0]), $callback[1]], | ||
$args | ||
); | ||
} | ||
|
||
return call_user_func_array($callback, $args); | ||
} | ||
|
||
/** | ||
* Creates and registers new LabelCallback. | ||
* | ||
* @param string $table | ||
* @param callable $callback | ||
* | ||
* @return static | ||
*/ | ||
public static function createAndRegister($table, callable $callback) | ||
{ | ||
$instance = new static(); | ||
$instance->register($table, $callback); | ||
|
||
return $instance; | ||
} | ||
} |
9cd34d5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. I will try that at next opportunity.