-
Notifications
You must be signed in to change notification settings - Fork 29
/
MaterialBox.php
96 lines (79 loc) · 2.83 KB
/
MaterialBox.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* @link https://github.com/MacGyer/yii2-materializecss
* @copyright Copyright (c) 2016 ... MacGyer for pluspunkt coding
* @license https://github.com/MacGyer/yii2-materializecss/blob/master/LICENSE
*/
namespace macgyer\yii2materializecss\widgets\media;
use macgyer\yii2materializecss\lib\BaseWidget;
use macgyer\yii2materializecss\lib\Html;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
/**
* MaterialBox creates a lightweight lightbox variant to present images.
*
* @author Christoph Erdmann <yii2-materializecss@pluspunkt-coding.de>
* @package widgets
* @subpackage media
*
* @see https://materializecss.com/media.html#materialbox
*/
class MaterialBox extends BaseWidget
{
/**
* @var string the source of the image.
* You must either specify this option or provide an image source via [[$imageOptions]].
*/
public $imageSrc;
/**
* @var array the HTML attributes for the image tag.
* @see [yii\helpers\BaseHtml::renderTagAttributes()](http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#renderTagAttributes()-detail)
* for details on how attributes are being rendered.
*/
public $imageOptions = [];
/**
* @var string|false the caption of the image.
* If you do not want a caption to be rendered, set this option to `false`.
*/
public $imageCaption = false;
/**
* @var boolean whether the image caption shall be HTML-encoded. This defaults to `true`.
*/
public $encodeImageCaption = true;
/**
* Initialize the widget.
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
if (!$this->imageSrc) {
$imageSrc = ArrayHelper::remove($this->imageOptions, 'src', null);
if (!$imageSrc) {
throw new InvalidConfigException('Image src must be defined.');
}
$this->imageSrc = $imageSrc;
}
Html::addCssClass($this->imageOptions, ['plugin' => 'materialboxed']);
if ($this->imageCaption !== false) {
$this->imageOptions['data-caption'] = $this->encodeImageCaption ? Html::encode($this->imageCaption) : $this->imageCaption;
}
$alt = Html::encode(ArrayHelper::getValue($this->imageOptions, 'alt'));
if (!$alt) {
$this->imageOptions['alt'] = Html::encode($this->imageCaption);
}
}
/**
* Execute the widget.
* @return string the widget's markup.
*/
public function run()
{
$this->registerPlugin('Materialbox', '.materialboxed');
$tag = ArrayHelper::remove($this->options, 'tag', 'div');
$html[] = Html::beginTag($tag, $this->options);
$html[] = Html::img($this->imageSrc, $this->imageOptions);
$html[] = Html::endTag($tag);
return implode("\n", $html);
}
}