Skip to content

Commit

Permalink
Fixes an issue where adding child entries in a multi-site install wou…
Browse files Browse the repository at this point in the history
…ld always create children for the primary site. Bump to 1.0.4
  • Loading branch information
mmikkel committed Jun 5, 2018
1 parent cb7def8 commit d198e51
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.4 - 2018-06-05
### Fixed
- Fixes an issue where adding child entries in a multi-site install would always create children for the primary site

## 1.0.3 - 2018-05-23
### Improved
- Child Me! quickmenu now closes immediately when clicking outside the container
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mmikkel/child-me",
"description": "Easily create child elements",
"type": "craft-plugin",
"version": "1.0.3",
"version": "1.0.4",
"keywords": [
"craft",
"cms",
Expand Down
19 changes: 16 additions & 3 deletions src/ChildMe.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,25 @@ protected function registerResources()
View::EVENT_BEFORE_RENDER_TEMPLATE,
function () {
try {
$data = [
'entryTypes' => [],
'sites' => [],
];
// Map section and entry type IDs to entry type names
$entryTypes = EntryTypeRecord::find()->all();
$data = [];
foreach ($entryTypes as $entryType) {
$section = $entryType->section->handle;
if (!isset($data[$section])) $data[$section] = [];
$data[$section][$entryType->id] = Craft::t('site', $entryType->name);
if (!isset($data['entryTypes'][$section])) {
$data['entryTypes'][$section] = [];
}
$data['entryTypes'][$section][$entryType->id] = Craft::t('site', $entryType->name);
}
// Map site IDs to site handles
$sites = Craft::$app->getSites()->getAllSites();
foreach ($sites as $site) {
if (!$site->primary) {
$data['sites']['site:' . $site->id] = $site->handle;
}
}
Craft::$app->getView()->registerAssetBundle(ChildMeBundle::class);
Craft::$app->getView()->registerJs('Craft.ChildMePlugin.init(' . Json::encode($data) . ')');
Expand Down
48 changes: 41 additions & 7 deletions src/resources/childme.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
}
},

getSiteHandle: function () {
var siteId = Craft.getLocalStorage('BaseElementIndex.siteId');
return this.data['sites']['site:' + siteId] || null;
},

createEntryTypeButtons: function () {

if (this.entryTypeButtons) {
Expand All @@ -83,14 +88,19 @@
$button = $(this);

var sectionHandle = $button.data('section') || null;
if (!sectionHandle) return false;
if (!sectionHandle) {
return false;
}

var entryTypes = _self.data[sectionHandle] || {};
var entryTypes = _self.data['entryTypes'][sectionHandle] || {};
var entryTypeIds = Object.keys(entryTypes);
if (entryTypeIds.length <= 1) return false;
if (entryTypeIds.length <= 1) {
return false;
}

var menuHtml = '<div class="menu" data-align="center"><ul>';
var menuOptions = [];
var href;

for (var j = 0; j < entryTypeIds.length; ++j) {
menuOptions.push('<li><a data-type="' + entryTypeIds[j] + '" data-parent="' + $button.data('id') + '" data-section="' + sectionHandle + '">' + entryTypes[entryTypeIds[j]] + '</a></li>');
Expand All @@ -115,7 +125,12 @@
e.preventDefault();
e.stopPropagation();
var $option = $(e.currentTarget);
var url = Craft.getCpUrl(['entries', $option.data('section'), 'new'].join('/'), {
var siteHandle = this.getSiteHandle();
var segments = ['entries', $option.data('section'), 'new'];
if (siteHandle) {
segments.push(siteHandle);
}
var url = Craft.getCpUrl(segments.join('/'), {
typeId: $option.data('type'),
parentId: $option.data('parent')
});
Expand All @@ -130,7 +145,7 @@
}
},

onChildMeButtonClick: function (e) {
onChildMeButtonFocus: function (e) {
this.closeActiveEntryTypeMenu();
var $target = $(e.currentTarget);
var menu = $target.data('_childmemenu');
Expand All @@ -142,6 +157,24 @@
}
},

onChildMeButtonClick: function (e) {
e.preventDefault();
e.stopPropagation();
var $target = $(e.currentTarget);
var menu = $target.data('_childmemenu');
if (menu) {
return false;
}
var url = $target.attr('href');
var siteId = Craft.getLocalStorage('BaseElementIndex.siteId');
var siteHandle = this.data['sites']['site:' + siteId] || null;
if (siteHandle) {
url = url.split('?');
url = url[0] + '/' + siteHandle + '?' + url[1];
}
window.location.href = url;
},

onDragStop: function (tableSorter) {

var $items = tableSorter.$items;
Expand All @@ -164,15 +197,16 @@

addEventListeners: function () {
Garnish.$doc
.on('focus', '[data-childmeadd]', this.onChildMeButtonClick.bind(this))
.on('focus', '[data-childmeadd]', this.onChildMeButtonFocus.bind(this))
.on('click', '[data-childmeadd]', this.onChildMeButtonClick.bind(this))
.on('blur', '[data-childmeadd]', this.closeActiveEntryTypeMenu.bind(this))
.on('click', '[data-childmeadd] a', this.onEntryTypeOptionSelect.bind(this))
.on('click', this.onDocClick.bind(this));
},

removeEventListeners: function () {
Garnish.$doc
.off('focus blur', '[data-childmeadd]')
.off('focus blur click', '[data-childmeadd]')
.off('click', '[data-childmeadd] a')
.off('click', this.onDocClick.bind(this))
}
Expand Down

0 comments on commit d198e51

Please sign in to comment.