Skip to content

Commit

Permalink
Add index fieldtype
Browse files Browse the repository at this point in the history
  • Loading branch information
martyf committed Aug 29, 2024
1 parent 4afb67d commit 366e6b2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions resources/dist/build/assets/index-5f8b80ee.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion resources/dist/build/assets/index-eea66144.js

This file was deleted.

2 changes: 1 addition & 1 deletion resources/dist/build/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"css": [
"assets/index-78a0ade5.css"
],
"file": "assets/index-eea66144.js",
"file": "assets/index-5f8b80ee.js",
"isEntry": true,
"src": "resources/js/index.js"
}
Expand Down
11 changes: 11 additions & 0 deletions resources/js/fieldtypes/iconamic_index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>
<div class="w-auto h-4" v-html="value"></div>
</div>
</template>

<script>
export default {
mixins: [IndexFieldtype],
}
</script>
2 changes: 2 additions & 0 deletions resources/js/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import IconamicField from './fieldtypes/iconamic.vue';
import IconamicIndexField from './fieldtypes/iconamic_index.vue';

Statamic.booting(() => {
Statamic.$components.register('iconamic-fieldtype', IconamicField);
Statamic.$components.register('iconamic-fieldtype-index', IconamicIndexField);
});
25 changes: 25 additions & 0 deletions src/Fieldtypes/Iconamic.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use MityDigital\Iconamic\Facades\Iconamic as IconamicFacade;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Statamic\Facades\Blink;
use Statamic\Fields\Fieldtype;

class Iconamic extends Fieldtype
Expand Down Expand Up @@ -181,4 +182,28 @@ protected function configFieldItems(): array
],
];
}

public function preProcessIndex($value)
{
$path = IconamicFacade::getPath($this->config('path'), $this->config('path_helper', 'default'), $value.'.svg');

try {
$svg = file_get_contents($path);

// get index
$index = Blink::get('iconamic-index-fieldtype', 0);
$index++;

// clean the svg markup to prevent duplicate IDs
$svg = IconamicFacade::cleanSvg($svg, $index);

// set index
Blink::put('iconamic-index-fieldtype', $index);

return $svg;
}
catch (\ErrorException $e) {
return $value;
}
}
}
4 changes: 2 additions & 2 deletions src/Support/Iconamic.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getPath(string $path, string $pathHelper, ?string $filename = nu
if ($pathHelper === 'default') {
$pathHelper = config('iconamic.path_helper');
}

ray($pathHelper)->blue();
// build the directory based off of the configured path and path helper
switch ($pathHelper) {
case 'app_path':
Expand All @@ -95,7 +95,7 @@ public function getPath(string $path, string $pathHelper, ?string $filename = nu
default:
throw IconamicException::unknownPathHelper($pathHelper);
}

ray($directory)->red();
$path = explode('/', $filename);
$stack = [];
foreach ($path as $seg) {
Expand Down

0 comments on commit 366e6b2

Please sign in to comment.