diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index e610491..8e57d60 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -5,6 +5,7 @@ Yii Framework 2 Change Log ----------------------- - Removed methods marked as deprected in 2.0.x (samdark) +- Chg #10771: Consistent behavior of `run()` method in all framework widgets. All return the result now for better extensibility (pkirill99, cebe) - Chg #11397: Minimum required version of PHP is 5.5.0 now (samdark) - Chg: Removed `yii\base\Object::className()` in favor of native PHP syntax `::class`, which does not trigger autoloading (cebe) diff --git a/framework/widgets/ActiveForm.php b/framework/widgets/ActiveForm.php index 137342c..e49624e 100644 --- a/framework/widgets/ActiveForm.php +++ b/framework/widgets/ActiveForm.php @@ -185,6 +185,7 @@ class ActiveForm extends Widget /** * Runs the widget. * This registers the necessary javascript code and renders the form close tag. + * @return string the result of widget execution to be outputted. * @throws InvalidCallException if `beginField()` and `endField()` calls are not matching */ public function run() @@ -194,8 +195,8 @@ class ActiveForm extends Widget } $content = ob_get_clean(); - echo Html::beginForm($this->action, $this->method, $this->options); - echo $content; + $html = Html::beginForm($this->action, $this->method, $this->options); + $html .= $content; if ($this->enableClientScript) { $id = $this->options['id']; @@ -206,7 +207,9 @@ class ActiveForm extends Widget $view->registerJs("jQuery('#$id').yiiActiveForm($attributes, $options);"); } - echo Html::endForm(); + $html .= Html::endForm(); + + return $html; } /** diff --git a/framework/widgets/Block.php b/framework/widgets/Block.php index 998dd6c..5bb7fa9 100644 --- a/framework/widgets/Block.php +++ b/framework/widgets/Block.php @@ -57,13 +57,15 @@ class Block extends Widget /** * Ends recording a block. * This method stops output buffering and saves the rendering result as a named block in the view. + * @return string the result of widget execution to be outputted. */ public function run() { $block = ob_get_clean(); if ($this->renderInPlace) { - echo $block; + return $block; } $this->view->blocks[$this->getId()] = $block; + return ''; } } diff --git a/framework/widgets/Breadcrumbs.php b/framework/widgets/Breadcrumbs.php index c5f00e3..8359d50 100644 --- a/framework/widgets/Breadcrumbs.php +++ b/framework/widgets/Breadcrumbs.php @@ -127,11 +127,12 @@ class Breadcrumbs extends Widget /** * Renders the widget. + * @return string the result of widget execution to be outputted. */ public function run() { if (empty($this->links)) { - return; + return ''; } $links = []; if ($this->homeLink === null) { @@ -148,7 +149,7 @@ class Breadcrumbs extends Widget } $links[] = $this->renderItem($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate); } - echo Html::tag($this->tag, implode('', $links), $this->options); + return Html::tag($this->tag, implode('', $links), $this->options); } /** diff --git a/framework/widgets/ContentDecorator.php b/framework/widgets/ContentDecorator.php index 26dee57..f75cf4d 100644 --- a/framework/widgets/ContentDecorator.php +++ b/framework/widgets/ContentDecorator.php @@ -68,12 +68,13 @@ class ContentDecorator extends Widget /** * Ends recording a clip. * This method stops output buffering and saves the rendering result as a named clip in the controller. + * @return string the result of widget execution to be outputted. */ public function run() { $params = $this->params; $params['content'] = ob_get_clean(); // render under the existing context - echo $this->view->renderFile($this->viewFile, $params); + return $this->view->renderFile($this->viewFile, $params); } } diff --git a/framework/widgets/DetailView.php b/framework/widgets/DetailView.php index f9e423e..1566846 100644 --- a/framework/widgets/DetailView.php +++ b/framework/widgets/DetailView.php @@ -131,6 +131,7 @@ class DetailView extends Widget /** * Renders the detail view. * This is the main entry of the whole detail view rendering. + * @return string the result of widget execution to be outputted. */ public function run() { @@ -142,7 +143,7 @@ class DetailView extends Widget $options = $this->options; $tag = ArrayHelper::remove($options, 'tag', 'table'); - echo Html::tag($tag, implode("\n", $rows), $options); + return Html::tag($tag, implode("\n", $rows), $options); } /** diff --git a/framework/widgets/FragmentCache.php b/framework/widgets/FragmentCache.php index 1d007e7..307bba3 100644 --- a/framework/widgets/FragmentCache.php +++ b/framework/widgets/FragmentCache.php @@ -98,17 +98,18 @@ class FragmentCache extends Widget * Content displayed before this method call and after [[init()]] * will be captured and saved in cache. * This method does nothing if valid content is already found in cache. + * @return string the result of widget execution to be outputted. */ public function run() { if (($content = $this->getCachedContent()) !== false) { - echo $content; + return $content; } elseif ($this->cache instanceof Cache) { array_pop($this->getView()->cacheStack); $content = ob_get_clean(); if ($content === false || $content === '') { - return; + return ''; } if (is_array($this->dependency)) { $this->dependency = Yii::createObject($this->dependency); @@ -119,8 +120,9 @@ class FragmentCache extends Widget if (empty($this->getView()->cacheStack) && !empty($this->dynamicPlaceholders)) { $content = $this->updateDynamicContent($content, $this->dynamicPlaceholders); } - echo $content; + return $content; } + return ''; } /** diff --git a/framework/widgets/LinkPager.php b/framework/widgets/LinkPager.php index b500fd8..bcbacc3 100644 --- a/framework/widgets/LinkPager.php +++ b/framework/widgets/LinkPager.php @@ -124,13 +124,14 @@ class LinkPager extends Widget /** * Executes the widget. * This overrides the parent implementation by displaying the generated page buttons. + * @return string the result of widget execution to be outputted. */ public function run() { if ($this->registerLinkTags) { $this->registerLinkTags(); } - echo $this->renderPageButtons(); + return $this->renderPageButtons(); } /** diff --git a/framework/widgets/LinkSorter.php b/framework/widgets/LinkSorter.php index 465baf7..59b840f 100644 --- a/framework/widgets/LinkSorter.php +++ b/framework/widgets/LinkSorter.php @@ -59,10 +59,11 @@ class LinkSorter extends Widget /** * Executes the widget. * This method renders the sort links. + * @return string the result of widget execution to be outputted. */ public function run() { - echo $this->renderSortLinks(); + return $this->renderSortLinks(); } /** diff --git a/framework/widgets/MaskedInput.php b/framework/widgets/MaskedInput.php index 95a70a4..0ea2eaa 100644 --- a/framework/widgets/MaskedInput.php +++ b/framework/widgets/MaskedInput.php @@ -125,9 +125,9 @@ class MaskedInput extends InputWidget { $this->registerClientScript(); if ($this->hasModel()) { - echo Html::activeInput($this->type, $this->model, $this->attribute, $this->options); + return Html::activeInput($this->type, $this->model, $this->attribute, $this->options); } else { - echo Html::input($this->type, $this->name, $this->value, $this->options); + return Html::input($this->type, $this->name, $this->value, $this->options); } } diff --git a/framework/widgets/Menu.php b/framework/widgets/Menu.php index 3c2e0ad..5cda5f6 100644 --- a/framework/widgets/Menu.php +++ b/framework/widgets/Menu.php @@ -162,6 +162,7 @@ class Menu extends Widget /** * Renders the menu. + * @return string the result of widget execution to be outputted. */ public function run() { @@ -172,12 +173,14 @@ class Menu extends Widget $this->params = Yii::$app->request->getQueryParams(); } $items = $this->normalizeItems($this->items, $hasActiveChild); - if (!empty($items)) { - $options = $this->options; - $tag = ArrayHelper::remove($options, 'tag', 'ul'); - - echo Html::tag($tag, $this->renderItems($items), $options); + if (empty($items)) { + return ''; } + + $options = $this->options; + $tag = ArrayHelper::remove($options, 'tag', 'ul'); + + return Html::tag($tag, $this->renderItems($items), $options); } /** diff --git a/framework/widgets/Spaceless.php b/framework/widgets/Spaceless.php index 50f402e..e1fb8b7 100644 --- a/framework/widgets/Spaceless.php +++ b/framework/widgets/Spaceless.php @@ -60,10 +60,11 @@ class Spaceless extends Widget /** * Marks the end of content to be cleaned from whitespace characters between HTML tags. - * Stops capturing an output and echoes cleaned result. + * Stops capturing an output and returns cleaned result. + * @return string the result of widget execution to be outputted. */ public function run() { - echo trim(preg_replace('/>\s+<', ob_get_clean())); + return trim(preg_replace('/>\s+<', ob_get_clean())); } }