Files
materio-d9/web/modules/custom/materio_flag/materio_flag.install
T
2026-09-26 01:10:59 +02:00

39 lines
1.3 KiB
PHP

<?php
/**
* @file
* Install, update and uninstall functions for the materio_flag module.
*/
/**
* Fix flag configs with empty labels.
*
* Flag lists (flag_lists) can create relflag_* flag configs without a label.
* Core 10 silently tolerated NULL labels, but core 11 typed
* Html::escape() as string, which makes \Drupal::token()->getInfo() fatal
* (the flag tokens build t('@flag flag link', ['@flag' => $flag->label()])).
*
* Give every flag a string label: the related flagging collection name when
* available, the flag ID as a fallback.
*/
function materio_flag_update_10200(&$sandbox = NULL) {
$flags = \Drupal::service('flag')->getAllFlags();
$fixed = 0;
foreach ($flags as $id => $flag) {
$label = $flag->label();
if (is_string($label) && $label !== '') {
continue;
}
$new_label = $id;
if (preg_match('/^relflag_(\d+)_/', $id, $matches)) {
$collection = \Drupal::entityTypeManager()->getStorage('flagging_collection')->load($matches[1]);
if ($collection && is_string($collection->label()) && $collection->label() !== '') {
$new_label = $collection->label();
}
}
$flag->set('label', $new_label)->save();
$fixed++;
}
return t('@count flag(s) with an empty label have been fixed.', ['@count' => $fixed]);
}