Feature: Decide what a profile image's metadata will be
Description
\FGTCLB\ announces
the metadata this extension is about to write for the image of a profile, and a
listener decides what is written. Whatever it leaves in
get is
the field map that reaches the database; an empty map writes nothing.
The event is dispatched for each of the two records that carry image metadata,
and
get says which one is being written:
sys_, the record of the file itself, written once by the frontend upload that created the file and only for the fields it found empty —file_ metadata title,alternativeand, where typo3/cms-filemetadata adds it,copyright. This is the record an installation requiring file attributes reads, which is why theFileis handed over rather than only its uid.sys_, the profile's own relation row, written whenever the name of the profile record changes — from a backend save, a localization or a frontend edit.file_ reference
Both records are handed over either way:
get is the file, whose
own metadata record is
$event->get, and
get the image relation of the profile.
get is the request the write happens in, and
null
where
there is none — on the command line, for instance.
Fields the target table does not declare are dropped before the write, so a
listener may set
copyright
unconditionally: without
typo3/cms-filemetadata
the column does not exist and the value goes
nowhere. System fields are dropped as well, with a warning in the log: the
identity of the record, the relation it is part of, its localization, its
workspace and its enable columns are the
Data's, and this event
writes metadata.
#[AsEventListener(identifier: 'my-extension/add-image-copyright')]
public function __invoke(ModifyProfileImageMetadataEvent $event): void
{
if ($event->getTargetTable() !== 'sys_file_metadata') {
return;
}
$metadata = $event->getMetadata();
$metadata['copyright'] = $metadata['alternative'] ?? '';
$event->setMetadata($metadata);
}
Impact
Nothing changes without a listener: the composed name of the profile record is
written to the columns named above. With one, a project fills the columns its
own installation adds and requires —
right_ of
fgtclb/file-required-attributes
, a copyright composed differently,
a caption of its own.
A listener runs inside the write of the profile record, for a backend save from
within a
Data hook. It has to be short, and it must not write
profile records itself.