Adding the following script to the control panel allows one to define a custom state for one or more matrix fields.
The following script can be modified as needed and then copy and paste into your custom field's "Field Instructions" box beneath your custom user instructions!
Many thanks to @MediaGirl for that trick!
Ref: How to add a custom JavaScript to the "New Entry" control panel?
<script>
$(function() {
// Make sure that this is the publish form
if (!EE.publish)
return;
// Only proceed when creating a new entry
var entryId = $('#publishForm').find('input[name="entry_id"]').val();
if (entryId != 0)
return;
function initLabels(id, labels) {
var addEntryButton = $('#sub_hold_field_' + id + ' .matrix-btn.matrix-add');
if (!addEntryButton.length)
return;
// Need to wait after `document.ready` has finished executing!
setTimeout(function() {
var field = $('#sub_hold_field_' + id);
// Only proceed if matrix is empty
// Note: This is not the case if a validation error occurs!
if (!field.find('.matrix-norows').is(':visible'))
return;
// Create one row for each label
for (var i = 0; i < labels.length; ++i)
addEntryButton.click();
// Skip the placeholder row for "No rows have been added yet..."
field.find('tbody tr:not(.matrix-norows)').each(function(i) {
$(this).find('.matrix-firstcell textarea').val(labels[i]);
});
}, 0);
}
// Check that this is an entry of the expected channel field group
if (EE.publish.field_group == 2) {
// "Custom Parameters"
initLabels('23', [
'Lens Type',
'Focal Length',
'Manufacturer',
'Model',
'Serial Number'
]);
// "Track Information"
initLabels('17', [
'Artist',
'Title',
'Label',
'Artwork'
]);
}
})
</script>