Skip to content

Commit 968357f

Browse files
committed
Allow global attributes to get merged in.
1 parent 3723f96 commit 968357f

File tree

8 files changed

+28
-20
lines changed

8 files changed

+28
-20
lines changed

src/View/Icon/BootstrapIcon.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ public function names(): array {
3434
* @return string
3535
*/
3636
public function render(string $icon, array $options = [], array $attributes = []): string {
37-
$formatOptions = $attributes + [
38-
];
37+
if (!empty($this->config['attributes'])) {
38+
$attributes += $this->config['attributes'];
39+
}
3940

4041
$options['class'] = 'bi bi-' . $icon;
4142
if (!empty($attributes['class'])) {
4243
$options['class'] .= ' ' . $attributes['class'];
4344
}
44-
$options['attributes'] = $this->template->formatAttributes($formatOptions, ['class']);
45+
$options['attributes'] = $this->template->formatAttributes($attributes, ['class']);
4546

4647
return $this->template->format('icon', $options);
4748
}

src/View/Icon/FeatherIcon.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ public function names(): array {
3434
* @return string
3535
*/
3636
public function render(string $icon, array $options = [], array $attributes = []): string {
37-
$formatOptions = $attributes + [
38-
];
37+
if (!empty($this->config['attributes'])) {
38+
$attributes += $this->config['attributes'];
39+
}
3940

4041
$options['name'] = $icon;
41-
$options['attributes'] = $this->template->formatAttributes($formatOptions);
42+
$options['attributes'] = $this->template->formatAttributes($attributes);
4243

4344
return $this->template->format('icon', $options);
4445
}

src/View/Icon/FontAwesome4Icon.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ public function names(): array {
3434
* @return string
3535
*/
3636
public function render(string $icon, array $options = [], array $attributes = []): string {
37-
$formatOptions = $attributes + [
38-
];
37+
if (!empty($this->config['attributes'])) {
38+
$attributes += $this->config['attributes'];
39+
}
3940

4041
$namespace = 'fa';
4142

@@ -64,7 +65,7 @@ public function render(string $icon, array $options = [], array $attributes = []
6465
if (!empty($attributes['class'])) {
6566
$options['class'] .= ' ' . $attributes['class'];
6667
}
67-
$options['attributes'] = $this->template->formatAttributes($formatOptions, ['class']);
68+
$options['attributes'] = $this->template->formatAttributes($attributes, ['class']);
6869

6970
return $this->template->format('icon', $options);
7071
}

src/View/Icon/FontAwesome5Icon.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public function names(): array {
3535
* @return string
3636
*/
3737
public function render(string $icon, array $options = [], array $attributes = []): string {
38-
$formatOptions = $attributes + [
39-
];
38+
if (!empty($this->config['attributes'])) {
39+
$attributes += $this->config['attributes'];
40+
}
4041

4142
$class = [
4243
$this->config['namespace'],
@@ -63,7 +64,7 @@ public function render(string $icon, array $options = [], array $attributes = []
6364
if (!empty($attributes['class'])) {
6465
$options['class'] .= ' ' . $attributes['class'];
6566
}
66-
$options['attributes'] = $this->template->formatAttributes($formatOptions, ['class']);
67+
$options['attributes'] = $this->template->formatAttributes($attributes, ['class']);
6768

6869
return $this->template->format('icon', $options);
6970
}

src/View/Icon/FontAwesome6Icon.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public function names(): array {
3535
* @return string
3636
*/
3737
public function render(string $icon, array $options = [], array $attributes = []): string {
38-
$formatOptions = $attributes + [
39-
];
38+
if (!empty($this->config['attributes'])) {
39+
$attributes += $this->config['attributes'];
40+
}
4041

4142
$namespace = 'fa-' . $this->config['namespace'];
4243

@@ -65,7 +66,7 @@ public function render(string $icon, array $options = [], array $attributes = []
6566
if (!empty($attributes['class'])) {
6667
$options['class'] .= ' ' . $attributes['class'];
6768
}
68-
$options['attributes'] = $this->template->formatAttributes($formatOptions, ['class']);
69+
$options['attributes'] = $this->template->formatAttributes($attributes, ['class']);
6970

7071
return $this->template->format('icon', $options);
7172
}

src/View/Icon/IconCollection.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ public function render(string $icon, array $options = [], array $attributes = []
118118
}
119119
}
120120

121-
$attributes += $options['attributes'] ?? [];
122121
unset($options['attributes']);
123122

124123
return $this->iconSets[$set]->render($icon, $options, $attributes);

src/View/Icon/MaterialIcon.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ public function names(): array {
3535
* @return string
3636
*/
3737
public function render(string $icon, array $options = [], array $attributes = []): string {
38-
$formatOptions = $attributes + [
39-
];
38+
if (!empty($this->config['attributes'])) {
39+
$attributes += $this->config['attributes'];
40+
}
4041

4142
$options['name'] = $icon;
4243
$options['class'] = $this->config['namespace'];
4344
if (!empty($attributes['class'])) {
4445
$options['class'] .= ' ' . $attributes['class'];
4546
}
46-
$options['attributes'] = $this->template->formatAttributes($formatOptions, ['class']);
47+
$options['attributes'] = $this->template->formatAttributes($attributes, ['class']);
4748

4849
return $this->template->format('icon', $options);
4950
}

tests/TestCase/View/Icon/IconCollectionTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public function testRenderNamespaced(): void {
3838
'material' => [
3939
'class' => MaterialIcon::class,
4040
'namespace' => 'material-symbols',
41+
'attributes' => [
42+
'data-custom' => 'some-custom',
43+
],
4144
],
4245
],
4346
'separator' => ':',
@@ -47,7 +50,7 @@ public function testRenderNamespaced(): void {
4750
];
4851
$result = (new IconCollection($config))->render('material:foo');
4952

50-
$this->assertSame('<span class="material-symbols" title="Foo" data-default="some-default">foo</span>', $result);
53+
$this->assertSame('<span class="material-symbols" title="Foo" data-custom="some-custom" data-default="some-default">foo</span>', $result);
5154
}
5255

5356
/**

0 commit comments

Comments
 (0)