Skip to content

Commit

Permalink
Add update script
Browse files Browse the repository at this point in the history
  • Loading branch information
robdekort committed Jan 24, 2024
1 parent 56f4a32 commit 22fa491
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ServiceProvider extends AddonServiceProvider
\Studio1902\PeakTools\Updates\UpdateFormFields::class,
\Studio1902\PeakTools\Updates\UpdateFormErrorHandling::class,
\Studio1902\PeakTools\Updates\UpdateButtonAttributeTags::class,
\Studio1902\PeakTools\Updates\UpdateImagesBlueprintWithExemptToggle::class,
];

public function bootAddon()
Expand Down
56 changes: 56 additions & 0 deletions src/Updates/UpdateImagesBlueprintWithExemptToggle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Studio1902\PeakTools\Updates;

use Statamic\Support\Arr;
use Illuminate\Support\Facades\File;
use Symfony\Component\Yaml\Yaml;
use Statamic\UpdateScripts\UpdateScript;

class UpdateImagesBlueprintWithExemptToggle extends UpdateScript
{
public function shouldUpdate($newVersion, $oldVersion)
{
return $this->isUpdatingTo('4.4.0');
}

public function update()
{
$file = base_path("resources/blueprints/assets/images.yaml");

if (File::exists($file)) {
$newField = [
'handle' => 'exempt_from_alt',
'field' => [
'inline_label' => "This image doesn't need an alt text.",
'default' => false,
'type' => 'toggle',
'display' => 'Exempt',
'icon' => 'toggle',
'listable' => 'hidden',
'instructions_position' => 'below',
'visibility' => 'visible',
'replicator_preview' => true,
'hide_display' => false,
'instructions' => "When enabled, this image won't show up in the control panel dashboard as an image that misses alt text. Images without an alt text will automatically be hidden for screen readers."
]
];

$blueprint = Yaml::parseFile($file);
$existingFields = Arr::get($blueprint, 'sections.main.fields');

if ($existingFields) {
$existingFields[] = $newField;

Arr::set($blueprint, 'sections.main.fields', $existingFields);
File::put($file, Yaml::dump($blueprint, 99, 2));

$this->console()->info('Added an exempt from alt toggle to the images blueprint in `resources/blueprints/assets/images.yaml`');
} else {
$this->console()->info("Couldn't add an exempt from alt toggle to the images blueprint in `resources/blueprints/assets/images.yaml`");
}
} else {
$this->console()->info('Add an `exempt_from_alt` toggle field to any asset blueprints where you want this functionality.');
}
}
}

0 comments on commit 22fa491

Please sign in to comment.