我有一些表单字段看起来像这样:“是(如果”是“,跳过下一个问题)”并且我希望括号内的任何带有文本的标签都被包裹在一个范围中,这样它们就可以被设置为样式。我尝试编写预处理程序,但它没有做任何事情。
function THEME_preprocess_form_element_label(&$variables){
$element = $variables['element'];
$title = $element['#title'];
$pat = "/\((.*?)\)/";
if (isset($title) && preg_match($pat, $title)) {
$titleSplit = preg_split($pat, $title);
$newTitle = $titleSplit[0]+"<span>"+$titleSplit[1]+"</span>";
$title = $newTitle;
}
}
答案 0 :(得分:0)
$title = $newTitle;
正在设置函数中的$title
变量,而不是通过引用传递的&$variables
。
尝试:
function THEME_preprocess_form_element_label(&$variables){
$pat = "/\((.*?)\)/";
if (isset($variables['element']['#title']) && preg_match($pat, $variables['element']['#title'])) {
$titleSplit = preg_split($pat, $variables['element']['#title']);
$newTitle = $titleSplit[0]+"<span>"+$titleSplit[1]+"</span>";
$variables['element']['#title'] = $newTitle;
}
}