By avpaderno on
Change record status:
Published (View all published change records)
Project:
Issue links:
Description:
UserRestrictionTypeBase has a new method, ::setLogMessage(), which should be used by UserRestrictionType plugins to set the message logged when the data submitted in a user-related form is forbidden by a user restriction.
Previously, UserRestrictionType plugins would log that message in their ::matches() method, for example using code similar to the following one.
public function matches(array $data, string $form_id = ''): false|UserRestrictionInterface {
if (!isset($data['name'])) {
return FALSE;
}
$restriction = $this->matchesValue($data['name'], $form_id);
if ($restriction) {
$this->setErrorMessage($this->t(
'The name %name is reserved, and cannot be used.',
['%name' => $data['name']]
));
try {
$this->logger->notice(
'Restricted name %name matching %restriction has been blocked.', [
'%name' => $data['name'],
'%restriction' => $restriction->label(),
'link' => $restriction->toLink($this->t('Edit restriction'), 'edit-form'),
]
);
}
catch (EntityMalformedException $e) {
// Ignore the exception, which was thrown when logging a notice message.
}
}
return $restriction;
}
Now, the used code is similar to the following one.
public function buildData(array $form, FormStateInterface $form_state, string $form_id, string $type = 'string'): string|object|null {
$name = $form_state->getValue('name');
$this->setLogMessage([
'Restricted name %name matching %restriction has been blocked.',
['%name' => $name],
]);
$this->setErrorMessage($this->t(
'The name %name is reserved, and cannot be used.',
['%name' => $name]
));
return match($type) {
'object' => (object) ['name' => $name],
'string' => $name,
default => NULL,
};
}
Impacts:
Module developers