Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG-5.11.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release Notes for Craft CMS 5.11 (WIP)

### Content Management
- Asset alt text no longer gets propagated to all sites the first time it’s filled in. ([#19067](https://github.com/craftcms/cms/pull/19067))

### Development
- The `params` argument of the `url()` Twig function now accepts `false` to remove all params from the passed-in URL. ([#19102](https://github.com/craftcms/cms/pull/19102))
- Added `craft\web\DbSession`, which should be used instead of `yii\web\DbSession` to prevent “headers already sent” warnings from getting logged. ([#19139](https://github.com/craftcms/cms/issues/19139))
Expand Down
2 changes: 1 addition & 1 deletion src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'id' => 'CraftCMS',
'name' => 'Craft CMS',
'version' => '5.10.13.2',
'schemaVersion' => '5.10.0.0',
'schemaVersion' => '5.11.0.0',
'minVersionRequired' => '4.5.0',
'basePath' => dirname(__DIR__), // Defines the @app alias
'runtimePath' => '@storage/runtime', // Defines the @runtime alias
Expand Down
53 changes: 16 additions & 37 deletions src/elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -1214,20 +1214,6 @@ private static function isFolderIndex(): bool
*/
private ?int $_oldVolumeId = null;

/**
* @inheritdoc
*/
public function __construct($config = [])
{
// alt='' actually means something, so we should preserve it.
$alt = ArrayHelper::remove($config, 'alt');
if ($alt !== null) {
$this->alt = $alt;
}

parent::__construct($config);
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -1307,20 +1293,6 @@ public function init(): void
$this->_oldVolumeId = $this->_volumeId;
}

/**
* @inheritdoc
*/
public function setAttributesFromRequest(array $values): void
{
// alt='' actually means something, so we should preserve it.
$alt = ArrayHelper::remove($values, 'alt');
if ($alt !== null) {
$this->alt = $alt;
}

parent::setAttributesFromRequest($values);
}

/**
* Returns the volume’s ID.
*
Expand Down Expand Up @@ -3406,10 +3378,6 @@ public function afterSave(bool $isNew): void
$record->mimeType = $this->_mimeType;
}

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

if ($this->getHasFocalPoint()) {
$focal = $this->getFocalPoint();
$record->focalPoint = number_format($focal['x'], 4) . ';' . number_format($focal['y'], 4);
Expand All @@ -3418,8 +3386,16 @@ public function afterSave(bool $isNew): void
}

$record->save(false);

// we're not propagating at this point, so save the alt ONLY against the site we're saving to
Db::upsert(Table::ASSETS_SITES, [
'assetId' => $this->id,
'siteId' => $this->siteId,
'alt' => $this->alt,
]);
}

$upsert = false;
if (
$this->propagating &&
$this->propagatingFrom &&
Expand All @@ -3432,15 +3408,18 @@ public function afterSave(bool $isNew): void
$this->alt !== $from->alt &&
$this->getAltTranslationKey() === $from->getAltTranslationKey()
) {
$upsert = true;
$this->alt = $from->alt;
}
}

Db::upsert(Table::ASSETS_SITES, [
'assetId' => $this->id,
'siteId' => $this->siteId,
'alt' => $this->alt,
]);
if ($upsert or $this->propagateAll) {
Db::upsert(Table::ASSETS_SITES, [
'assetId' => $this->id,
'siteId' => $this->siteId,
'alt' => $this->alt,
]);
}

parent::afterSave($isNew);
}
Expand Down
54 changes: 19 additions & 35 deletions src/elements/db/AssetQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -1015,39 +1015,25 @@ protected function beforePrepare(): bool
protected function afterPrepare(): bool
{
if ($this->hasAlt !== null) {
$hasAltCondition = [
'or',
['not', ['assets_sites.alt' => '']],
[
'and',
['assets_sites.alt' => null],
['not', ['assets.alt' => '']],
['not', ['assets.alt' => null]],
],
];

$withoutAltCondition = [
'or',
['assets_sites.alt' => ''],
[
$this->subQuery->leftJoin(['assets_sites' => Table::ASSETS_SITES], [
'and',
'[[assets_sites.assetId]] = [[assets.id]]',
'[[assets_sites.siteId]] = [[elements_sites.siteId]]',
]);

if ($this->hasAlt) {
$this->subQuery->andWhere([
'and',
['not', ['assets_sites.alt' => '']],
['not', ['assets_sites.alt' => null]],
]);
} else {
$this->subQuery->andWhere([
'or',
['assets_sites.alt' => ''],
['assets_sites.alt' => null],
[
'or',
['assets.alt' => ''],
['assets.alt' => null],
],

],
];

$this->subQuery
->leftJoin(['assets_sites' => Table::ASSETS_SITES], [
'and',
'[[assets_sites.assetId]] = [[assets.id]]',
'[[assets_sites.siteId]] = [[elements_sites.siteId]]',
])
->andWhere($this->hasAlt ? $hasAltCondition : $withoutAltCondition);
]);
}
}

return parent::afterPrepare();
Expand Down Expand Up @@ -1154,11 +1140,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 = ArrayHelper::remove($row, 'siteAlt');
if ($siteAlt !== null) {
$row['alt'] = $siteAlt;
}
$row['alt'] = $siteAlt;

return parent::createElement($row);
}
Expand Down
66 changes: 66 additions & 0 deletions src/migrations/m260610_065719_translatable_alt_text_content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace craft\migrations;

use craft\db\Migration;
use craft\db\Query;
use craft\db\Table;
use craft\helpers\Db;

/**
* m260610_065719_translatable_alt_text_content migration.
*/
class m260610_065719_translatable_alt_text_content extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
// Find assets that have globally-defined alt text and null/missing translated alt text,
// and fill in their translated values.

$query = (new Query())
// fetch assets_sites.assetId so we know whether the row already exists
->select(['assets.id', 'elements_sites.siteId', 'assets.alt', 'assets_sites.assetId'])
->from(['assets' => Table::ASSETS])
->innerJoin(['elements_sites' => Table::ELEMENTS_SITES], '[[elements_sites.elementId]] = [[assets.id]]')
->leftJoin(['assets_sites' => Table::ASSETS_SITES], [
'and',
'[[assets_sites.assetId]] = [[assets.id]]',
'[[assets_sites.siteId]] = [[elements_sites.siteId]]',
])
->where(['not', ['assets.alt' => null]])
->andWhere(['not', ['assets.alt' => '']])
->andWhere(['assets_sites.alt' => null]);

foreach (Db::each($query) as $row) {
// If the assets_sites.assetId value came back, the row already exists
if (isset($row['assetId'])) {
Db::update(Table::ASSETS_SITES, [
'alt' => $row['alt'],
], [
'assetId' => $row['id'],
'siteId' => $row['siteId'],
]);
} else {
Db::insert(Table::ASSETS_SITES, [
'assetId' => $row['id'],
'siteId' => $row['siteId'],
'alt' => $row['alt'],
]);
}
}

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m260610_065719_translatable_alt_text_content cannot be reverted.\n";
return false;
}
}