Commit 327dde72 by Alexander Makarov

Fixes #5467: Added ability to pass HTML tag options to `asEmail()`, `asImage()`…

Fixes #5467: Added ability to pass HTML tag options to `asEmail()`, `asImage()` and `asUrl()` methods of `yii\i18n\Formatter`
parent ce4bf844
......@@ -18,6 +18,7 @@ Yii Framework 2 Change Log
- Enh #4275: Added `removeChildren()` to `yii\rbac\ManagerInterface` and implementations (samdark)
- Enh: Added `yii\base\Application::loadedModules` (qiangxue)
- Enh #5316: Added `startsWith()` and `endsWith()` to `yii\helpers\StringHelper`. Methods are binary-safe, multibyte-safe and optionally case-insensitive (armab)
- Enh #5467: Added ability to pass HTML tag options to `asEmail()`, `asImage()` and `asUrl()` methods of `yii\i18n\Formatter` (alxkolm, samdark)
- Chg #2037: Dropped the support for using `yii\base\Module` as concrete module classes (qiangxue)
......
......@@ -24,6 +24,9 @@ Upgrade from Yii 2.0 RC
* The `yii\base\View::beforeRender()` and `yii\base\View::afterRender()` methods have two extra parameters `$viewFile`
and `$params`. If you are overriding these methods, you should adjust the method signature accordingly.
* If you've used `asImage` formatter i.e. `Yii::$app->formatter->asImage($value, $alt);` you should change it
to `Yii::$app->formatter->asImage($value, ['alt' => $alt]);`.
Upgrade from Yii 2.0 Beta
......
......@@ -332,28 +332,29 @@ class Formatter extends Component
/**
* Formats the value as a mailto link.
* @param mixed $value the value to be formatted.
* @param array $options the tag options in terms of name-value pairs. See [[Html::mailto()]].
* @return string the formatted result.
*/
public function asEmail($value)
public function asEmail($value, $options = [])
{
if ($value === null) {
return $this->nullDisplay;
}
return Html::mailto(Html::encode($value), $value);
return Html::mailto(Html::encode($value), $value, $options);
}
/**
* Formats the value as an image tag.
* @param mixed $value the value to be formatted.
* @param string $altText an optional `alt`-tag to be added to the image.
* @param array $options the tag options in terms of name-value pairs. See [[Html::img()]].
* @return string the formatted result.
*/
public function asImage($value, $altText = '')
public function asImage($value, $options = [])
{
if ($value === null) {
return $this->nullDisplay;
}
return Html::img($value, ['alt' => $altText]);
return Html::img($value, $options);
}
/**
......
......@@ -143,6 +143,8 @@ class FormatterTest extends TestCase
{
$value = 'test@sample.com';
$this->assertSame("<a href=\"mailto:$value\">$value</a>", $this->formatter->asEmail($value));
$value = 'test@sample.com';
$this->assertSame("<a href=\"mailto:$value\" target=\"_blank\">$value</a>", $this->formatter->asEmail($value, ['target' => '_blank']));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asEmail(null));
......@@ -169,6 +171,9 @@ class FormatterTest extends TestCase
{
$value = 'http://sample.com/img.jpg';
$this->assertSame("<img src=\"$value\" alt=\"\">", $this->formatter->asImage($value));
$value = 'http://sample.com/img.jpg';
$alt = "Hello!";
$this->assertSame("<img src=\"$value\" alt=\"$alt\">", $this->formatter->asImage($value, ['alt' => $alt]));
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asImage(null));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment