Display selected existing text fields using labels embedded in their values. Drupal 11 ready
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

24 lines
677 B

<?php
namespace Drupal\dynamic_field_label;
/**
* Parses plain-text labels without changing stored field values.
*/
final class LabelParser {
/**
* Splits at the first colon, returning NULL for malformed input.
*/
public static function parse(string $raw): ?array {
if (!str_contains($raw, ':')) {
return NULL;
}
[$label, $value] = array_map('trim', explode(':', $raw, 2));
// A label must be plain text. Reject markup before the separator,
// including colons occurring in HTML attributes rather than in a label.
return $label === '' || strpbrk($label, '<>') !== FALSE
? NULL : ['label' => $label, 'value' => $value];
}
}