Browse Source

Allow global attributes to get merged in.

mscherer 3 years ago
parent
commit
968357ff3c

+ 4 - 3
src/View/Icon/BootstrapIcon.php

@@ -34,14 +34,15 @@ class BootstrapIcon extends AbstractIcon {
 	 * @return string
 	 */
 	public function render(string $icon, array $options = [], array $attributes = []): string {
-		$formatOptions = $attributes + [
-		];
+		if (!empty($this->config['attributes'])) {
+			$attributes += $this->config['attributes'];
+		}
 
 		$options['class'] = 'bi bi-' . $icon;
 		if (!empty($attributes['class'])) {
 			$options['class'] .= ' ' . $attributes['class'];
 		}
-		$options['attributes'] = $this->template->formatAttributes($formatOptions, ['class']);
+		$options['attributes'] = $this->template->formatAttributes($attributes, ['class']);
 
 		return $this->template->format('icon', $options);
 	}

+ 4 - 3
src/View/Icon/FeatherIcon.php

@@ -34,11 +34,12 @@ class FeatherIcon extends AbstractIcon {
 	 * @return string
 	 */
 	public function render(string $icon, array $options = [], array $attributes = []): string {
-		$formatOptions = $attributes + [
-		];
+		if (!empty($this->config['attributes'])) {
+			$attributes += $this->config['attributes'];
+		}
 
 		$options['name'] = $icon;
-		$options['attributes'] = $this->template->formatAttributes($formatOptions);
+		$options['attributes'] = $this->template->formatAttributes($attributes);
 
 		return $this->template->format('icon', $options);
 	}

+ 4 - 3
src/View/Icon/FontAwesome4Icon.php

@@ -34,8 +34,9 @@ class FontAwesome4Icon extends AbstractIcon {
 	 * @return string
 	 */
 	public function render(string $icon, array $options = [], array $attributes = []): string {
-		$formatOptions = $attributes + [
-		];
+		if (!empty($this->config['attributes'])) {
+			$attributes += $this->config['attributes'];
+		}
 
 		$namespace = 'fa';
 
@@ -64,7 +65,7 @@ class FontAwesome4Icon extends AbstractIcon {
 		if (!empty($attributes['class'])) {
 			$options['class'] .= ' ' . $attributes['class'];
 		}
-		$options['attributes'] = $this->template->formatAttributes($formatOptions, ['class']);
+		$options['attributes'] = $this->template->formatAttributes($attributes, ['class']);
 
 		return $this->template->format('icon', $options);
 	}

+ 4 - 3
src/View/Icon/FontAwesome5Icon.php

@@ -35,8 +35,9 @@ class FontAwesome5Icon extends AbstractIcon {
 	 * @return string
 	 */
 	public function render(string $icon, array $options = [], array $attributes = []): string {
-		$formatOptions = $attributes + [
-			];
+		if (!empty($this->config['attributes'])) {
+			$attributes += $this->config['attributes'];
+		}
 
 		$class = [
 			$this->config['namespace'],
@@ -63,7 +64,7 @@ class FontAwesome5Icon extends AbstractIcon {
 		if (!empty($attributes['class'])) {
 			$options['class'] .= ' ' . $attributes['class'];
 		}
-		$options['attributes'] = $this->template->formatAttributes($formatOptions, ['class']);
+		$options['attributes'] = $this->template->formatAttributes($attributes, ['class']);
 
 		return $this->template->format('icon', $options);
 	}

+ 4 - 3
src/View/Icon/FontAwesome6Icon.php

@@ -35,8 +35,9 @@ class FontAwesome6Icon extends AbstractIcon {
 	 * @return string
 	 */
 	public function render(string $icon, array $options = [], array $attributes = []): string {
-		$formatOptions = $attributes + [
-		];
+		if (!empty($this->config['attributes'])) {
+			$attributes += $this->config['attributes'];
+		}
 
 		$namespace = 'fa-' . $this->config['namespace'];
 
@@ -65,7 +66,7 @@ class FontAwesome6Icon extends AbstractIcon {
 		if (!empty($attributes['class'])) {
 			$options['class'] .= ' ' . $attributes['class'];
 		}
-		$options['attributes'] = $this->template->formatAttributes($formatOptions, ['class']);
+		$options['attributes'] = $this->template->formatAttributes($attributes, ['class']);
 
 		return $this->template->format('icon', $options);
 	}

+ 0 - 1
src/View/Icon/IconCollection.php

@@ -118,7 +118,6 @@ class IconCollection {
 			}
 		}
 
-		$attributes += $options['attributes'] ?? [];
 		unset($options['attributes']);
 
 		return $this->iconSets[$set]->render($icon, $options, $attributes);

+ 4 - 3
src/View/Icon/MaterialIcon.php

@@ -35,15 +35,16 @@ class MaterialIcon extends AbstractIcon {
 	 * @return string
 	 */
 	public function render(string $icon, array $options = [], array $attributes = []): string {
-		$formatOptions = $attributes + [
-		];
+		if (!empty($this->config['attributes'])) {
+			$attributes += $this->config['attributes'];
+		}
 
 		$options['name'] = $icon;
 		$options['class'] = $this->config['namespace'];
 		if (!empty($attributes['class'])) {
 			$options['class'] .= ' ' . $attributes['class'];
 		}
-		$options['attributes'] = $this->template->formatAttributes($formatOptions, ['class']);
+		$options['attributes'] = $this->template->formatAttributes($attributes, ['class']);
 
 		return $this->template->format('icon', $options);
 	}

+ 4 - 1
tests/TestCase/View/Icon/IconCollectionTest.php

@@ -38,6 +38,9 @@ class IconCollectionTest extends TestCase {
 				'material' => [
 					'class' => MaterialIcon::class,
 					'namespace' => 'material-symbols',
+					'attributes' => [
+						'data-custom' => 'some-custom',
+					],
 				],
 			],
 			'separator' => ':',
@@ -47,7 +50,7 @@ class IconCollectionTest extends TestCase {
 		];
 		$result = (new IconCollection($config))->render('material:foo');
 
-		$this->assertSame('<span class="material-symbols" title="Foo" data-default="some-default">foo</span>', $result);
+		$this->assertSame('<span class="material-symbols" title="Foo" data-custom="some-custom" data-default="some-default">foo</span>', $result);
 	}
 
 	/**