Skip to content

Instantly share code, notes, and snippets.

@andyhoman
Created January 28, 2013 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyhoman/4655632 to your computer and use it in GitHub Desktop.
Save andyhoman/4655632 to your computer and use it in GitHub Desktop.
Automatically populate a matrix field by injecting jQuery into the fields instructions.
<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 == 8) {
// "Custom Parameters"
initLabels('132', [
'Sydney',
'Melbourne'
]);
}
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment