Skip to content

Commit

Permalink
Yii2 Bootstrap 5 plugin init enhancements fixes #1056
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Jul 23, 2023
1 parent c407f8e commit 027e6bc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
3 changes: 2 additions & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
84 changes: 56 additions & 28 deletions src/GridViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -1583,7 +1583,7 @@ protected function initBootstrapStyle()
/**
* Initialize the grid layout.
*
* @throws InvalidConfigException
* @throws InvalidConfigException|Exception
*/
protected function initLayout()
{
Expand All @@ -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]);
}
}
}
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -1745,6 +1765,14 @@ protected function initPanel()
]), $options);
}

/**
* @return string
*/
protected function renderGridItems()
{
return Html::tag('div', '{items}', $this->containerOptions);
}

/**
* Generates the toolbar.
*
Expand Down

0 comments on commit 027e6bc

Please sign in to comment.