From 49c82edf816614477c62034382006b2651fd0761 Mon Sep 17 00:00:00 2001 From: i-just Date: Wed, 5 Aug 2026 10:36:37 +0200 Subject: [PATCH] respect alt text translation method from the start --- src/Asset/Elements/Asset.php | 45 ++++++++----------- src/Cms.php | 2 +- ...26_06_10_112441_drop_assets_alt_column.php | 33 ++++++++++++++ src/Database/Migrations/Install.php | 1 - src/Element/Queries/AssetQuery.php | 8 +--- .../Queries/Concerns/Asset/QueriesAlt.php | 14 +----- .../Queries/Concerns/Asset/QueriesAltTest.php | 2 +- 7 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 src/Database/Migrations/2026_06_10_112441_drop_assets_alt_column.php diff --git a/src/Asset/Elements/Asset.php b/src/Asset/Elements/Asset.php index b6c9140a5b0..876207ac467 100644 --- a/src/Asset/Elements/Asset.php +++ b/src/Asset/Elements/Asset.php @@ -327,12 +327,6 @@ class Asset extends Element public function __construct($config = []) { - // alt='' actually means something, so we should preserve it. - $alt = Arr::pull($config, 'alt'); - if ($alt !== null) { - $this->alt = $alt; - } - parent::__construct($config); if (isset($this->alt)) { @@ -1152,17 +1146,6 @@ public function __get($name) } } - #[Override] - public function setAttributesFromRequest(array $values): void - { - // alt='' actually means something, so we should preserve it. - if (Arr::has($values, 'alt')) { - $this->alt = Arr::pull($values, 'alt') ?? ''; - } - - parent::setAttributesFromRequest($values); - } - /** * Returns the volume’s ID. */ @@ -2977,10 +2960,6 @@ public function afterSave(bool $isNew): void $model->mimeType = $this->_mimeType; } - if ($model->alt === null) { - $model->alt = $this->alt; - } - if ($this->getHasFocalPoint()) { $focal = $this->getFocalPoint(); $model->focalPoint = number_format($focal['x'], 4).';'.number_format($focal['y'], 4); @@ -2989,8 +2968,17 @@ public function afterSave(bool $isNew): void } $model->save(); + + // we're not propagating at this point, so save the alt ONLY against the site we're saving to + DB::table(Table::ASSETS_SITES) + ->upsert([ + 'assetId' => $this->id, + 'siteId' => $this->siteId, + 'alt' => $this->alt, + ], ['assetId', 'siteId']); } + $upsert = false; if ( $this->propagating && $this->propagatingFrom && @@ -3003,16 +2991,19 @@ public function afterSave(bool $isNew): void $this->alt !== $from->alt && $this->getAltTranslationKey() === $from->getAltTranslationKey() ) { + $upsert = true; $this->alt = $from->alt; } } - DB::table(Table::ASSETS_SITES) - ->upsert([ - 'assetId' => $this->id, - 'siteId' => $this->siteId, - 'alt' => $this->alt, - ], ['assetId', 'siteId']); + if ($upsert || $this->propagateAll) { + DB::table(Table::ASSETS_SITES) + ->upsert([ + 'assetId' => $this->id, + 'siteId' => $this->siteId, + 'alt' => $this->alt, + ], ['assetId', 'siteId']); + } parent::afterSave($isNew); } diff --git a/src/Cms.php b/src/Cms.php index 132fdc8edda..e42adbbfb4e 100644 --- a/src/Cms.php +++ b/src/Cms.php @@ -32,7 +32,7 @@ public const string SCHEMA_VERSION = '6.0.0.4'; - public const string MIN_VERSION_REQUIRED = '5.9.0'; + public const string MIN_VERSION_REQUIRED = '5.11.0'; public static function name(): string { diff --git a/src/Database/Migrations/2026_06_10_112441_drop_assets_alt_column.php b/src/Database/Migrations/2026_06_10_112441_drop_assets_alt_column.php new file mode 100644 index 00000000000..702894f42cb --- /dev/null +++ b/src/Database/Migrations/2026_06_10_112441_drop_assets_alt_column.php @@ -0,0 +1,33 @@ +dropColumn('alt'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + $this->output->error('2026_06_10_112441_drop_assets_alt_column cannot be reverted.'); + } +}; diff --git a/src/Database/Migrations/Install.php b/src/Database/Migrations/Install.php index 91111f8ebd5..2537add4045 100644 --- a/src/Database/Migrations/Install.php +++ b/src/Database/Migrations/Install.php @@ -286,7 +286,6 @@ public function createTables(?Logger $logger = null): void $table->string('filename'); $table->string('mimeType')->nullable(); $table->string('kind', 50)->default(FileKind::Unknown->value); - $table->text('alt')->nullable(); $table->unsignedInteger('width')->nullable(); $table->unsignedInteger('height')->nullable(); $table->unsignedBigInteger('size')->nullable(); diff --git a/src/Element/Queries/AssetQuery.php b/src/Element/Queries/AssetQuery.php index 36b70aa711e..be54e401017 100644 --- a/src/Element/Queries/AssetQuery.php +++ b/src/Element/Queries/AssetQuery.php @@ -70,7 +70,6 @@ public function __construct(array $config = []) 'assets.width as width', 'assets.height as height', 'assets.size as size', - 'assets.alt as alt', 'assets.focalPoint as focalPoint', 'assets.keptFile as keptFile', 'assets.dateModified as dateModified', @@ -189,12 +188,9 @@ private function applyAuthParam(?bool $value, string $permissionPrefix, string $ #[Override] public function createElement(array $row): ElementInterface { - // Use the site-specific alt text, if set + // Use the site-specific alt text $siteAlt = Arr::pull($row, 'siteAlt'); - - if ($siteAlt !== null) { - $row['alt'] = $siteAlt; - } + $row['alt'] = $siteAlt; return parent::createElement($row); } diff --git a/src/Element/Queries/Concerns/Asset/QueriesAlt.php b/src/Element/Queries/Concerns/Asset/QueriesAlt.php index 5e2587e5fdf..c5404065ace 100644 --- a/src/Element/Queries/Concerns/Asset/QueriesAlt.php +++ b/src/Element/Queries/Concerns/Asset/QueriesAlt.php @@ -28,22 +28,12 @@ protected function initQueriesAlt(): void $hasAltCondition = function (Builder $query) { $query->where('assets_sites.alt', '!=', '') - ->orWhere(function (Builder $query) { - $query->whereNull('assets_sites.alt') - ->where('assets.alt', '!=', '') - ->whereNotNull('assets.alt'); - }); + ->whereNotNull('assets_sites.alt'); }; $withoutAltCondition = function (Builder $query) { $query->where('assets_sites.alt', '=', '') - ->orWhere(function (Builder $query) { - $query->whereNull('assets_sites.alt') - ->where(function (Builder $query) { - $query->where('assets.alt', '=', '') - ->orWhereNull('assets.alt'); - }); - }); + ->orWhereNull('assets_sites.alt'); }; $assetQuery->where($this->hasAlt ? $hasAltCondition : $withoutAltCondition); diff --git a/tests/Feature/Element/Queries/Concerns/Asset/QueriesAltTest.php b/tests/Feature/Element/Queries/Concerns/Asset/QueriesAltTest.php index 17e8aee7a69..1fb7c45e08c 100644 --- a/tests/Feature/Element/Queries/Concerns/Asset/QueriesAltTest.php +++ b/tests/Feature/Element/Queries/Concerns/Asset/QueriesAltTest.php @@ -8,7 +8,7 @@ Asset::factory()->create(); // With alt - Asset::factory()->create([ + Asset::factory()->create()->sites()->attach(Site::all(), [ 'alt' => 'Alt text', ]);