#97636Warning when trying to update not existing file properties with File::updateProperties
Description
We want to add custom properties to a \TYPO3\CMS\Core\Resource\File. However, as these properties may not exist, a warning is thrown.
PHP Warning: Undefined array key "alternative" in typo3/sysext/core/Classes/Resource/File.php line 184
The problematic code:
foreach ($properties as $key => $value) {
if ($this->properties[$key] !== $value) {
if (!in_array($key, $this->updatedProperties)) {
$this->updatedProperties[] = $key;
}
$this->properties[$key] = $value;
}
}
The simplest fix might be to change
$this->properties[$key] !== $value
to
$this->properties[$key] ?? null !== $value
or replace the whole code with something like this (untested):
$this->updatedProperties = array_merge($this->updatedProperties, array_keys(array_diff_assoc($this->properties, $properties)));
$this->properties = array_merge($this->properties, $properties);Reviews 0
No change resolves or names this issue in its commit message, and no comment links one.
History 6
-
Florian Müller changed TYPO3 Version
1211 05/18/22, 08:42 -
Oliver Bartsch commented and changed status
NewNeeds Feedback 05/24/22, 10:01 -
Oliver Bartsch changed Related to #98387 07/05/23, 18:26
-
Oliver Bartsch changed
Related to #9838707/05/23, 18:26 -
Oliver Bartsch changed Duplicates #98387 07/05/23, 18:26
-
Oliver Bartsch commented and changed status
Needs FeedbackClosed 07/05/23, 18:27Closing this as duplicate of #98387, which already fixed the issue.
Hi, thanks for creating an issue.
I hade a quick look into this. Please note that this method is marked as
internaland should therefore actually not be used in extension code. Additionally, the method is only meant to update existing properties, not to add new ones. That seems to be the reason, why there is no further check for existence of$key. What exactly is your use case for adding new properties via this method?