Skip to content

Commit

Permalink
Fix #168
Browse files Browse the repository at this point in the history
  • Loading branch information
ttempleton committed Jan 12, 2019
1 parent 1efc479 commit 1839ea8
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.1.4 - 2019-01-13
### Fixed
- Fixed issue on multi-site Craft installs where entry/category fields in new or pasted/cloned Neo blocks were always listing elements from the primary site

## 2.1.3 - 2019-01-12
### Fixed
- Fixed error when duplicating an element with a Neo field set not to manage blocks on a per-site basis
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "spicyweb/craft-neo",
"description": "A Matrix-like field type that uses existing fields",
"version": "2.1.3",
"version": "2.1.4",
"type": "craft-plugin",
"keywords": [
"cms",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "craft-neo",
"version": "2.1.3",
"version": "2.1.4",
"description": "A Matrix-like field type that uses existing fields",
"main": "webpack.config.js",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,8 @@ private function _getInputHtml($value, ElementInterface $element = null, bool $s
->all();
}

$siteId = $element !== null ? $element->siteId : null;

$html = '';

// Disable Neo fields inside Matrix, SuperTable and potentially other field-grouping field types.
Expand All @@ -612,7 +614,7 @@ private function _getInputHtml($value, ElementInterface $element = null, bool $s
else
{
$viewService->registerAssetBundle(FieldAsset::class);
$viewService->registerJs(FieldAsset::createInputJs($this, $value, $static));
$viewService->registerJs(FieldAsset::createInputJs($this, $value, $static, $siteId));

$html = $viewService->renderTemplate('neo/input', [
'neoField' => $this,
Expand Down
10 changes: 6 additions & 4 deletions src/assets/FieldAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ public static function createSettingsJs(Field $field): string
* @param Field $field The Neo field.
* @param array $value The Neo blocks, associated with this field, to generate inputs for.
* @param bool $static Whether to generate static HTML for the blocks, e.g. for displaying entry revisions.
* @param int|null $siteId
* @return string
*/
public static function createInputJs(Field $field, $value, bool $static = false): string
public static function createInputJs(Field $field, $value, bool $static = false, int $siteId = null): string
{
$viewService = Craft::$app->getView();

Expand All @@ -94,7 +95,7 @@ public static function createInputJs(Field $field, $value, bool $static = false)
$jsSettings = [
'name' => $name,
'namespace' => $viewService->namespaceInputName($name),
'blockTypes' => self::_getBlockTypesJsSettings($blockTypes, true, $static),
'blockTypes' => self::_getBlockTypesJsSettings($blockTypes, true, $static, $siteId),
'groups' => self::_getBlockTypeGroupsJsSettings($blockTypeGroups),
'inputId' => $viewService->namespaceInputId($id),
'minBlocks' => $field->minBlocks,
Expand Down Expand Up @@ -156,9 +157,10 @@ private static function _getBlocksJsSettings(array $blocks, bool $static = false
* @param array $blockTypes The Neo block types.
* @param bool $renderTabs Whether to render the block types' tabs.
* @param bool $static Whether to generate static HTML for the block types, e.g. for displaying entry revisions.
* @param int|null $siteId
* @return array
*/
private static function _getBlockTypesJsSettings(array $blockTypes, bool $renderTabs = false, bool $static = false): array
private static function _getBlockTypesJsSettings(array $blockTypes, bool $renderTabs = false, bool $static = false, int $siteId = null): array
{
$jsBlockTypes = [];

Expand Down Expand Up @@ -209,7 +211,7 @@ private static function _getBlockTypesJsSettings(array $blockTypes, bool $render

if ($renderTabs)
{
$tabsHtml = Neo::$plugin->blockTypes->renderTabs($blockType, $static);
$tabsHtml = Neo::$plugin->blockTypes->renderTabs($blockType, $static, null, $siteId);
$jsBlockType['tabs'] = $tabsHtml;
}

Expand Down
15 changes: 8 additions & 7 deletions src/controllers/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class Input extends Controller
{
/**
* Renders copied/pasted input blocks.
* Renders pasted or cloned input blocks.
*
* @return Response
*/
Expand All @@ -33,7 +33,7 @@ public function actionRenderBlocks(): Response

$blocks = $requestService->getRequiredBodyParam('blocks');
$namespace = $requestService->getParam('namespace');
$locale = $requestService->getParam('locale');
$siteId = $requestService->getParam('locale');
$renderedBlocks = [];

foreach ($blocks as $rawBlock)
Expand All @@ -45,7 +45,8 @@ public function actionRenderBlocks(): Response
$block->level = $rawBlock['level'];
$block->enabled = isset($rawBlock['enabled']);
$block->setCollapsed(isset($rawBlock['collapsed']));
$block->ownerSiteId = $locale;
$block->ownerSiteId = $siteId;
$block->siteId = $siteId ?? $sitesService->getPrimarySite()->id;

if (!empty($rawBlock['content']))
{
Expand Down Expand Up @@ -85,15 +86,15 @@ public function actionSaveExpansion(): Response

// If the `locale` parameter wasn't passed, then this Craft installation has only one site, thus we can just
// grab the primary site ID.
$locale = $requestService->getParam('locale', $sitesService->getPrimarySite()->id);
$siteId = $requestService->getParam('locale', $sitesService->getPrimarySite()->id);

$return = $this->asJson([
'success' => false,
'blockId' => $blockId,
'locale' => $locale,
'locale' => $siteId,
]);

$block = $blockId ? Neo::$plugin->blocks->getBlockById($blockId, $locale) : null;
$block = $blockId ? Neo::$plugin->blocks->getBlockById($blockId, $siteId) : null;

if ($block)
{
Expand All @@ -103,7 +104,7 @@ public function actionSaveExpansion(): Response
$return = $this->asJson([
'success' => true,
'blockId' => $blockId,
'locale' => $locale,
'locale' => $siteId,
'expanded' => !$block->getCollapsed(),
]);
}
Expand Down
10 changes: 9 additions & 1 deletion src/services/BlockTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,21 @@ public function deleteGroupsByFieldId($fieldId): bool
* @param Block $block The Neo block type having its tabs rendered.
* @param bool $static Whether to generate static tab content.
* @param string|null $namespace
* @param int|null $siteId
* @return array The tabs data.
*/
public function renderTabs(BlockType $blockType, bool $static = false, $namespace = null): array
public function renderTabs(BlockType $blockType, bool $static = false, $namespace = null, int $siteId = null): array
{
$block = new Block();
$block->typeId = $blockType->id;

// Ensure that the passed site ID is valid before applying it
// If the site ID is not passed or is invalid, the block will default to the primary site
if ($siteId !== null && Craft::$app->getSites()->getSiteById($siteId) !== null)
{
$block->siteId = $siteId;
}

return Neo::$plugin->blocks->renderTabs($block, $static, $namespace);
}

Expand Down

0 comments on commit 1839ea8

Please sign in to comment.