4

How can I pre-fill a matrix field when creating a new channel entry.

The reason to do this (and why a matrix field is needed) is:

  • Data is best entered and represented using a table.
  • Unwanted rows can be removed.
  • Additional rows can be added.
  • To avoid having to re-enter row descriptions that are frequently used.
| improve this question | | | | |
12

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>
| improve this answer | | | | |
  • You could write an extension to get it there if you wanted full control of when and where via goo.gl/Pbbwh or use this one that's all ready built: devot-ee.com/add-ons/cp-css-js – Natetronn Dec 13 '12 at 6:16
  • @Natetronn That is good to know thanks. Though in this case it seems that custom scripts can be placed within the "Field Instructions" of the custom field settings! Nice trick thanks again to MediaGirl :-) – Lea Hayes Dec 13 '12 at 6:41
  • Very cool, favoriting this thread so I can find this easily in the future. – CreateSean Dec 13 '12 at 16:10
1

Though slightly different, I was inspired by this to create Matrix Presets which adds the ability to load and save values to P&T Matrix cells. You would be able to load rather than prefill values into the Matrix Cells.

It could be cleaned up but it works on EE 2.7.3 with Matrix 2.5.10, and so far works with basic fields and within Zenbu+Henshu.

https://github.com/ignetic/ee-matrix-presets

Perhaps in a future update, I may add an options replace all fields or add to them.

Give it a try if it suits your needs, but it may prove useful to others.

Cheers

| improve this answer | | | | |
0

It's not pre filling, but have you thought about using CKEditor templates ? You would create as many code blocks as you would need and all the client would need to do is click the template button and choose the content chunk they want to create.

| improve this answer | | | | |
  • Think of this a little like the way eBay provides a default selection of fields when you go to sell an item. Like a camera for example, it would have boxes that say "Lens Type", "Focal Length", etc. and you can remove the ones that are not relevant and add additional ones that are. This is exactly what I want to do only I have multiple columns for each row. My custom template can then render this as needed. I am not sure that this could be done using a WYSIWYG field type. – Lea Hayes Dec 13 '12 at 4:30
  • I could solve this perhaps by somehow adding a JavaScript to the control panel. I just did the following in Chrome and it seemed to add the rows, I suspect that the values could be pre-filled with a JavaScript somehow. for (var i = 0; i < 6; ++i) $('#sub_hold_field_23 .matrix-btn.matrix-add').click() – Lea Hayes Dec 13 '12 at 4:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.