Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 18 additions & 29 deletions src/Asset/Elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,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)) {
Expand Down Expand Up @@ -1148,19 +1142,6 @@ public function __get($name)
}
}

#[Override]
public function setAttributesFromRequest(array $values): void
{
// alt='' actually means something, so we should preserve it.
$alt = Arr::pull($values, 'alt');

if ($alt !== null) {
$this->alt = $alt;
}

parent::setAttributesFromRequest($values);
}

/**
* Returns the volume’s ID.
*/
Expand Down Expand Up @@ -2966,10 +2947,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);
Expand All @@ -2978,8 +2955,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 &&
Expand All @@ -2992,16 +2978,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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Cms.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

public const string VERSION = '6.0.0-alpha.6';

public const string SCHEMA_VERSION = '6.0.0.2';
public const string SCHEMA_VERSION = '6.0.0.3';

public const string MIN_VERSION_REQUIRED = '5.9.0';
public const string MIN_VERSION_REQUIRED = '5.11.0';

public static function name(): string
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use CraftCms\Cms\Database\Migration;
use CraftCms\Cms\Database\Table;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
if (! Schema::hasColumn(Table::ASSETS, 'alt')) {
return;
}

Schema::table(Table::ASSETS, function (Blueprint $table) {
$table->dropColumn('alt');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
$this->output->error('2026_06_10_112441_drop_assets_alt_column cannot be reverted.');
}
};
1 change: 0 additions & 1 deletion src/Database/Migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 2 additions & 6 deletions src/Element/Queries/AssetQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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',
Expand Down Expand Up @@ -188,12 +187,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);
}
Expand Down
14 changes: 2 additions & 12 deletions src/Element/Queries/Concerns/Asset/QueriesAlt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Asset::factory()->create();

// With alt
Asset::factory()->create([
Asset::factory()->create()->sites()->attach(Site::all(), [
'alt' => 'Alt text',
]);

Expand Down
27 changes: 5 additions & 22 deletions yii2-adapter/legacy/elements/db/AssetQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,6 @@ protected function beforePrepare(): bool
'assets.width',
'assets.height',
'assets.size',
'assets.alt',
'assets.focalPoint',
'assets.keptFile',
'assets.dateModified',
Expand Down Expand Up @@ -1021,29 +1020,15 @@ protected function afterPrepare(): bool
{
if ($this->hasAlt !== null) {
$hasAltCondition = [
'or',
'and',
['not', ['assets_sites.alt' => '']],
[
'and',
['assets_sites.alt' => null],
['not', ['assets.alt' => '']],
['not', ['assets.alt' => null]],
],
['not', ['assets_sites.alt' => null]],
];

$withoutAltCondition = [
'or',
['assets_sites.alt' => ''],
[
'and',
['assets_sites.alt' => null],
[
'or',
['assets.alt' => ''],
['assets.alt' => null],
],

],
['assets_sites.alt' => null],
];

$this->subQuery
Expand Down Expand Up @@ -1159,11 +1144,9 @@ private function _normalizeVolumeId(): void
*/
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);
}
Expand Down
Loading