Created
January 28, 2013 13:50
-
-
Save andyhoman/4655632 to your computer and use it in GitHub Desktop.
Automatically populate a matrix field by injecting jQuery into the fields instructions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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