To allow Admins to dynamically update several field attributes, I am working on code to store several attributes in the DB and apply them using JS and Labels API. Everything is working except the Placeholder. I am using v11, Build 43339. I have the following function. The original attempt is commented out and the longer placeholder code works on full page loads but not on a pop-up page. It simply displays nothing rather than the placeholder value.
private static function applyPhpSettings($tableName, $row) {
// Debug output - will appear in HTML comments
echo "<!-- FieldSettings Debug:\n";
echo "Table: ".$tableName."\n";
echo "Field: ".$row["FieldName"]."\n";
echo "Placeholder Value: ".$row["Placeholder"]."\n";
echo "Full \$row contents:\n";
print_r($row);
echo "-->\n";
if (!empty($row["Placeholder"])) {
// Generate all possible field ID formats
$possibleIds = [
$tableName.'_'.$row["FieldName"], // Standard format
'value_'.$row["FieldName"].'_1', // Your custom format
'edit_'.$row["FieldName"], // Common edit format
$row["FieldName"] // Simple format
];
// Create JavaScript to try all formats
$js = "var field;";
foreach ($possibleIds as $id) {
$js .= "field = field || document.getElementById('$id');";
}
echo "<script>
document.addEventListener('DOMContentLoaded', function() {
$js
if (field) {
field.placeholder = '".addslashes($row["Placeholder"])."';
console.log('Set placeholder for', field.id);
} else {
console.warn('Field not found: ".$row["FieldName"]."');
}
});
</script>";
}
/* This was the original attempt that does not display anything in the Placeholder
if (!empty($row["Placeholder"])) {
echo "<!-- Trying to set placeholder for ".$tableName."_".$row["FieldName"].": ".$row["Placeholder"]." -->\n";
Labels::setPlaceholder($tableName, $row["FieldName"], $row["Placeholder"]);
}
*/
if (!empty($row["FieldLabel"])) {
Labels::setFieldLabel($tableName, $row["FieldName"], $row["FieldLabel"]);
}
if (!empty($row["Tooltip"])) {
Labels::setFieldTooltip($tableName, $row["FieldName"], $row["Tooltip"]);
}
}
The function is called from the BeforeProcess event on the Edit page using
$jsFieldSettings = FieldSettingsHelper::applySettings("awd_SetBackEvent", $pageObject);
$pageObject->setProxyValue("jsFieldSettings", $jsFieldSettings);
When I look at the debug output in the View Source, I can see the Placeholder text is being retrieved properly from the database. Any idea why Labels::setFieldLabel and Labels::setFieldTooltip work great and Labels::setPlaceholder does not?
Thank you.