Skip to content

Commit 6fc60ec

Browse files
committed
icon constants, doc update icon, cleanup
1 parent da8bd0f commit 6fc60ec

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

src/widgets/Icon.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,42 @@
1515
/**
1616
* Icon can be used to display a Materialize icon.
1717
*
18-
* Please note that the Materialize icons are shipped in a separate font file. This font file is automatically registered
19-
* by the [[\macgyer\yii2materializecss\assets\MaterializeAsset|MaterializeAsset]].
18+
* To be compatible with GDPR (EU) the MaterializeFontAsset is not loaded automatically via the [[\macgyer\yii2materializecss\assets\MaterializeAsset|MaterializeAsset]]. The font asset requests the Material Icon font from Google servers (as stated in the Materialize docs).
19+
* If you are not affected by GDPR, simply load the [[\macgyer\yii2materializecss\assets\MaterializeFontAsset|MaterializeFontAsset]] in your layout or AppAsset.
2020
*
21-
* If you do not load the default [[\macgyer\yii2materializecss\assets\MaterializeAsset|MaterializeAsset]] make sure to at least load
22-
* [[\macgyer\yii2materializecss\assets\MaterializeFontAsset|MaterializeFontAsset]] (or another source providing the font file) to correctly
23-
* display the icons.
21+
* Otherwise you need to self-host the Material Icon font (i. e. do not request them from Google). You could use `material-icons` (https://www.npmjs.com/package/material-icons) to load the font files, CSS and SCSS from NPM and include them in your build process.
2422
*
2523
* @author Christoph Erdmann <yii2-materializecss@pluspunkt-coding.de>
2624
* @package widgets
2725
* @see https://materializecss.com/icons.html
2826
*/
2927
class Icon extends BaseWidget
3028
{
29+
/**
30+
* Sets the [[size]] of the icon to "tiny".
31+
*/
32+
public const SIZE_TINY = 'tiny';
33+
/**
34+
* Sets the [[size]] of the icon to "small". This is the default.
35+
*/
36+
public const SIZE_SMALL = 'small';
37+
/**
38+
* Sets the [[size]] of the icon to "medium".
39+
*/
40+
public const SIZE_MEDIUM = 'medium';
41+
/**
42+
* Sets the [[size]] of the icon to "large".
43+
*/
44+
public const SIZE_LARGE = 'large';
45+
/**
46+
* Sets the [[position]] of the icon to "left".
47+
*/
48+
public const POSITION_LEFT = 'left';
49+
/**
50+
* Sets the [[position]] of the icon to "right".
51+
*/
52+
public const POSITION_RIGHT = 'right';
53+
3154
/**
3255
* @var string the name of the icon.
3356
*
@@ -40,11 +63,16 @@ class Icon extends BaseWidget
4063
*
4164
* Currently "left" and "right" are natively supported by Materialize, but you can set this property to a custom string
4265
* which will be added to the HTML class attribute and thus can be individually styled.
43-
*
44-
* The default icon position is "left".
4566
*/
4667
public $position;
4768

69+
/**
70+
* @var string the size of the icon.
71+
*
72+
* The default icon size is "small".
73+
*/
74+
public $size = self::SIZE_SMALL;
75+
4876
/**
4977
* @var array the HTML options for the icon tag.
5078
*
@@ -69,12 +97,15 @@ public function init()
6997
if (!$this->name) {
7098
throw new InvalidConfigException('The icon name must be specified.');
7199
}
100+
Html::addCssClass($this->options, ['widget' => 'material-icons']);
72101

73-
if ($this->position === null) {
74-
$this->position = 'left';
102+
if ($this->position) {
103+
Html::addCssClass($this->options, ['material-icon-position' => $this->position]);
75104
}
76105

77-
Html::addCssClass($this->options, ['material-icons', $this->position]);
106+
if ($this->size) {
107+
Html::addCssClass($this->options, ['material-icon-size' => $this->size]);
108+
}
78109
}
79110

80111
/**

src/widgets/media/MaterialBox.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function init()
5858
if (!$this->imageSrc) {
5959
$imageSrc = ArrayHelper::remove($this->imageOptions, 'src', null);
6060
if (!$imageSrc) {
61-
throw new InvalidConfigException("Image src must be defined.");
61+
throw new InvalidConfigException('Image src must be defined.');
6262
}
6363

6464
$this->imageSrc = $imageSrc;
@@ -69,6 +69,11 @@ public function init()
6969
if ($this->imageCaption !== false) {
7070
$this->imageOptions['data-caption'] = $this->encodeImageCaption ? Html::encode($this->imageCaption) : $this->imageCaption;
7171
}
72+
73+
$alt = Html::encode(ArrayHelper::getValue($this->imageOptions, 'alt'));
74+
if (!$alt) {
75+
$this->imageOptions['alt'] = Html::encode($this->imageCaption);
76+
}
7277
}
7378

7479
/**

0 commit comments

Comments
 (0)