Commit ce4bf844 by Alexey Kolmakov Committed by Alexander Makarov

#5467 Allow pass options to Html:a in Formatter:asUrl

parent 6c6afd3f
...@@ -359,9 +359,10 @@ class Formatter extends Component ...@@ -359,9 +359,10 @@ class Formatter extends Component
/** /**
* Formats the value as a hyperlink. * Formats the value as a hyperlink.
* @param mixed $value the value to be formatted. * @param mixed $value the value to be formatted.
* @param array $options the tag options in terms of name-value pairs. See [[Html::a()]].
* @return string the formatted result. * @return string the formatted result.
*/ */
public function asUrl($value) public function asUrl($value, $options = [])
{ {
if ($value === null) { if ($value === null) {
return $this->nullDisplay; return $this->nullDisplay;
...@@ -371,7 +372,7 @@ class Formatter extends Component ...@@ -371,7 +372,7 @@ class Formatter extends Component
$url = 'http://' . $url; $url = 'http://' . $url;
} }
return Html::a(Html::encode($value), $url); return Html::a(Html::encode($value), $url, $options);
} }
/** /**
......
...@@ -158,6 +158,8 @@ class FormatterTest extends TestCase ...@@ -158,6 +158,8 @@ class FormatterTest extends TestCase
$this->assertSame("<a href=\"http://$value\">$value</a>", $this->formatter->asUrl($value)); $this->assertSame("<a href=\"http://$value\">$value</a>", $this->formatter->asUrl($value));
$value = 'https://www.yiiframework.com/?name=test&value=5"'; $value = 'https://www.yiiframework.com/?name=test&value=5"';
$this->assertSame("<a href=\"https://www.yiiframework.com/?name=test&amp;value=5&quot;\">https://www.yiiframework.com/?name=test&amp;value=5&quot;</a>", $this->formatter->asUrl($value)); $this->assertSame("<a href=\"https://www.yiiframework.com/?name=test&amp;value=5&quot;\">https://www.yiiframework.com/?name=test&amp;value=5&quot;</a>", $this->formatter->asUrl($value));
$value = 'http://www.yiiframework.com/';
$this->assertSame("<a href=\"$value\" target=\"_blank\">$value</a>", $this->formatter->asUrl($value, ['target' => '_blank']));
// null display // null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asUrl(null)); $this->assertSame($this->formatter->nullDisplay, $this->formatter->asUrl(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