Last active
January 4, 2023 17:28
-
-
Save croxton/e7dae90a447c27ce929a13dcebe2c0ca to your computer and use it in GitHub Desktop.
Craft CMS - Sidebar Entry Types plugin: change the 'New entry' button to create an entry in the selected section and entry type.
This file contains 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
(function() { | |
// on click of sidebar entry type | |
document.addEventListener('click', function (event) { | |
if (event.target.parentNode && event.target.parentNode.matches('a[data-entry-type].sel')) { | |
setTimeout(function () { | |
manageNewEntryButton(event.target.parentNode); | |
}, 10); | |
} | |
}, false); | |
// on initial load | |
setTimeout(function () { | |
var selectedEntryTypeListing = document.querySelector('a.sel[data-entry-type]'); | |
if (selectedEntryTypeListing) { | |
manageNewEntryButton(selectedEntryTypeListing); | |
} | |
}, 10); | |
// Change 'New entry' button so it creates a new entry using the entry type selected in the sidebar | |
function manageNewEntryButton(elm) { | |
var newEntryButtonWrapper = document.querySelector('#action-buttons div[data-wrapper]'); | |
if (newEntryButtonWrapper) { | |
var buttonLabelText = "New entry"; | |
var button = newEntryButtonWrapper.querySelector('button'); | |
if (button) { | |
var buttonLabel = button.querySelector('.label'); | |
if (buttonLabel) { | |
buttonLabelText = buttonLabel.textContent; | |
} | |
button.innerHTML = null; | |
button.classList.remove('icon'); | |
var newEntryByTypeButton = document.createElement('a'); | |
newEntryByTypeButton.classList.add('btn', 'submit', 'add', 'icon'); | |
var newEntryByTypeLabel = document.createElement('div'); | |
newEntryByTypeLabel.classList.add('label'); | |
newEntryByTypeLabel.textContent = buttonLabelText; | |
var newEntryByTypeSpinner = document.createElement('div'); | |
newEntryByTypeSpinner.classList.add('spinner', 'spinner-absolute'); | |
newEntryByTypeButton.appendChild(newEntryByTypeLabel); | |
newEntryByTypeButton.appendChild(newEntryByTypeSpinner); | |
// Add href to create a new entry of the selected type | |
var seg1 = location.pathname.split('/')[1]; | |
var section = elm.dataset.section; | |
var typeId = elm.dataset.typeid; | |
newEntryByTypeButton.href = '/' + seg1 + '/actions/entries/create?section=' + section + '&typeId=' + typeId; | |
newEntryButtonWrapper.prepend(newEntryByTypeButton); | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment