From 63640db65ca43f3829afe3ea8c28267277e82a51 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 13 Dec 2013 21:15:43 -0500 Subject: [PATCH] Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs --- framework/CHANGELOG.md | 2 +- framework/yii/base/Widget.php | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 4a78c09..b255482 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -12,7 +12,7 @@ Yii Framework 2 Change Log - Enh #1437: Added ListView::viewParams (qiangxue) - Enh #1469: ActiveRecord::find() now works with default conditions (default scope) applied by createQuery (cebe) - Enh: Added `favicon.ico` and `robots.txt` to defauly application templates (samdark) -- Enh: Widget IDs are now always unique no matter if it's the same request or new one (samdark) +- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue) - New #1438: [MongoDB integration](https://github.com/yiisoft/yii2-mongodb) ActiveRecord and Query (klimov-paul) 2.0.0 alpha, December 1, 2013 diff --git a/framework/yii/base/Widget.php b/framework/yii/base/Widget.php index a45dfaf..16c13f7 100644 --- a/framework/yii/base/Widget.php +++ b/framework/yii/base/Widget.php @@ -25,6 +25,17 @@ use ReflectionClass; class Widget extends Component implements ViewContextInterface { /** + * @var integer a counter used to generate [[id]] for widgets. + * @internal + */ + public static $counter = 0; + /** + * @var string the prefix to the automatically generated widget IDs. + * @see [[getId()]] + */ + public static $autoIdPrefix = 'w'; + + /** * @var Widget[] the widgets that are currently being rendered (not ended). This property * is maintained by [[begin()]] and [[end()]] methods. * @internal @@ -96,7 +107,7 @@ class Widget extends Component implements ViewContextInterface public function getId($autoGenerate = true) { if ($autoGenerate && $this->_id === null) { - $this->_id = 'w-' . str_replace('.', '-', uniqid('', true)); + $this->_id = self::$autoIdPrefix . self::$counter++; } return $this->_id; }