diff --git a/CHANGE.md b/CHANGE.md index 7b15b21d..97baee1f 100755 --- a/CHANGE.md +++ b/CHANGE.md @@ -3,9 +3,10 @@ Change Log: `yii2-grid` ## Version 3.5.1 -**Date:** 08-Mar-2023 +**Date:** 23-Jul-2023 - (enh #1057): Persist resize uniq column ids. +- (enh #1056): Enhance GridView layout parts rendering for preventing wrong Bootstrap 5 plugin initializations. - (enh #1054): Update Dutch Translations. - (enh #1050): Fix documentation and code mismatch. - (enh #1045): Add `isFilterEqual` method to validate filters correctly. diff --git a/src/GridViewTrait.php b/src/GridViewTrait.php index 9cd847f4..32d5ef50 100644 --- a/src/GridViewTrait.php +++ b/src/GridViewTrait.php @@ -832,7 +832,7 @@ protected function initGridView() * @var Request $request */ $request = $this->_module->get('request', false); - if ($request === null || !($request instanceof Request)) { + if (!($request instanceof Request)) { $request = Yii::$app->request; } $this->_isShowAll = $request->getQueryParam($this->_toggleDataKey, $this->defaultPagination) === 'all'; @@ -1583,7 +1583,7 @@ protected function initBootstrapStyle() /** * Initialize the grid layout. * - * @throws InvalidConfigException + * @throws InvalidConfigException|Exception */ protected function initLayout() { @@ -1600,33 +1600,14 @@ protected function initLayout() if ($this->hideResizeMobile) { Html::addCssClass($this->options, 'hide-resize'); } - $this->replaceLayoutTokens([ - '{toolbarContainer}' => $this->renderToolbarContainer(), - '{toolbar}' => $this->renderToolbar(), - '{export}' => $this->renderExport(), - '{toggleData}' => $this->renderToggleData(), - '{items}' => Html::tag('div', '{items}', $this->containerOptions), - ]); + $this->replaceLayoutPart('{toolbarContainer}', [$this, 'renderToolbarContainer']); + $this->replaceLayoutPart('{toolbar}', [$this, 'renderToolbar']); + $this->replaceLayoutPart('{export}', [$this, 'renderExport']); + $this->replaceLayoutPart('{toggleData}', [$this, 'renderToggleData']); + $this->replaceLayoutPart('{items}', [$this, 'renderGridItems']); if (is_array($this->replaceTags) && !empty($this->replaceTags)) { - foreach ($this->replaceTags as $key => $value) { - if ($value instanceof Closure) { - $value = call_user_func($value, $this); - } - $this->layout = Lib::str_replace($key, $value, $this->layout); - } - } - } - - /** - * Replace layout tokens - * - * @param array $pairs the token to find and its replaced value as key value pairs - */ - protected function replaceLayoutTokens($pairs) - { - foreach ($pairs as $token => $replace) { - if (Lib::strpos($this->layout, $token) !== false) { - $this->layout = Lib::str_replace($token, $replace, $this->layout); + foreach ($this->replaceTags as $key => $callback) { + $this->replaceLayoutPart($key, $callback, ['grid' => $this]); } } } @@ -1674,6 +1655,45 @@ protected function endPjax() Pjax::end(); } + /** + * Replaces token within haystack using custom callable. + * + * @param string $prop the template property in this module + * @param string $needle + * @param string|array $callback the callback function name + * @param array $params arguments to the callback + */ + protected function replacePart($prop, $needle, $callback, $params = []) + { + if (!property_exists($this, $prop)) { + return; + } + if (is_array($callback)) { + if (count($callback) > 1) { + $exists = method_exists($callback[0], $callback[1]); + } else { + return; + } + } else { + $exists = is_callable($callback); + } + if (Lib::strpos($this->$prop, $needle) !== false) { + $this->$prop = Lib::strtr($this->layout, [$needle => $exists ? call_user_func_array($callback, $params) : '']); + } + } + + /** + * Replaces layout token using custom callable. + * + * @param string $needle + * @param string|array $callback the callback function name + * @param array $params arguments to the callback + */ + protected function replaceLayoutPart($needle, $callback, $params = []) + { + $this->replacePart('layout', $needle, $callback, $params); + } + /** * Initializes and sets the grid panel layout based on the [[template]] and [[panel]] settings. * @@ -1745,6 +1765,14 @@ protected function initPanel() ]), $options); } + /** + * @return string + */ + protected function renderGridItems() + { + return Html::tag('div', '{items}', $this->containerOptions); + } + /** * Generates the toolbar. *