First of all I would like to leave a few links for you.
- Pluginfactory.io is a page where you can create your "starting" files. It creates an empty Plugin, with all the required files/functions with example codes for routing and services....
- The Docs about Craft Plugin developement
Then to the code: you'll need to insert a function like this in your Plugin's primary class
craft()->templates->hook('cp.entries.edit', function(&$context) {
if ($context['sectionHandle'] === 'course') {
// you can get these from a config file or you could create
// plugin settings, I'll just insert an array here, it's faster
$config = [
'entryTypeOne' => [
'title' => 'A title',
'body' => 'some information',
'relationField' => [1, 2, 4] //<-- array of element ids
],
'anotherEntryType' => [
'title' => 'Another title',
'body' => 'some more information',
'relationField' => [] //<-- array of element ids
]
];
/** @var \Craft\EntryModel $entry */
$entry = $context['entry'];
$entry->getContent()->setAttributes($config[$entry->getType()->handle]);
}
}
However: unfortunately you can't set default values when the user changes the entry type with this method, thus we'll need another "hack".
As you can see in the Relabel Plugin Craft provides ways to change default functions. You can insert this javascript
Craft.EntryTypeSwitcher.prototype.onTypeChange = function(ev)
{
this.$spinner.removeClass('hidden');
Craft.postActionRequest('entries/switchEntryType', Craft.cp.$container.serialize(), $.proxy(function(response, textStatus) {
this.$spinner.addClass('hidden');
if (textStatus == 'success')
{
var fieldsPane = this.$fields.data('pane');
fieldsPane.deselectTab();
this.$fields.html(response.paneHtml);
fieldsPane.destroy();
this.$fields.pane();
Craft.initUiElements(this.$fields);
Craft.appendHeadHtml(response.headHtml);
Craft.appendFootHtml(response.footHtml);
// Update the slug generator with the new title input
if (typeof slugGenerator != "undefined")
{
slugGenerator.setNewSource('#title');
}
}
}, this));
}
To change the default behavior when someone changes the entry type. All you have to do is to change the destination of the script
from
Craft.postActionRequest('entries/switchEntryType'
to something like
Craft.postActionRequest('yourpluginhandle/controllername/switchEntryType'
Then you'll take the default EntriesController function
public function actionSwitchEntryType()
{
$this->requirePostRequest();
$this->requireAjaxRequest();
$entry = $this->_getEntryModel();
$this->enforceEditEntryPermissions($entry);
$this->_populateEntryModel($entry);
$variables['sectionId'] = $entry->sectionId;
$variables['entry'] = $entry;
$variables['showEntryTypes'] = true;
$this->_prepEditEntryVariables($variables);
$paneHtml = craft()->templates->render('_includes/tabs', $variables) .
craft()->templates->render('entries/_fields', $variables);
$this->returnJson(array(
'paneHtml' => $paneHtml,
'headHtml' => craft()->templates->getHeadHtml(),
'footHtml' => craft()->templates->getFootHtml(),
));
}
And do exactly the same like in the first function
$entry->getContent()->setAttributes($config[$entry->getType()->handle]);
Unfortunately if you want to take care of all use cases you have to overwrite another function. When someone is trying to create an entry via an ElementEditor
(the thing that pops up when you click on "new entry") in a relation field. It works exactly the same like my first method and it's the same like Here
save as new entry
function. Otherwise it requires a plugin to use certain hooks to overwrite the default values. Is this function mainly for the CP or do you want to be able to show "fallback" values in your frontend as well? – Robin Schambach Feb 28 '18 at 16:03cp.entries.edit'
hook to insert your values. I can show you how if you want, just let me know – Robin Schambach Feb 28 '18 at 16:14