Skip to content

Commit

Permalink
Copy deactivated fields to the target sites too
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasNo1 committed Aug 13, 2020
1 parent 1ed5169 commit 91334a1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Ability to copy global sets
### Changed
- Automatic copy: Renamed current OR implementation to XOR and added new non-breaking "OR" check method.
### Fixed
- Deactivated fields now get copied to the target site too ([#21](https://github.com/Goldinteractive/craft3-sitecopy/issues/21))

## 0.5.3 - 2020-08-10
### Fixed
Expand Down
28 changes: 26 additions & 2 deletions src/services/SiteCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use craft\base\Component;
use craft\base\Element;
use craft\base\Model;
use craft\elements\db\ElementQuery;
use craft\elements\Entry;
use craft\elements\GlobalSet;
use craft\events\ElementEvent;
Expand All @@ -18,7 +19,6 @@
use Exception;
use goldinteractive\sitecopy\jobs\SyncElementContent;
use Throwable;
use yii\base\Event;

/**
* Class SiteCopy
Expand Down Expand Up @@ -256,7 +256,7 @@ public function syncElementContent(ElementEvent $event, array $elementSettings)
->siteId($entry->siteId)
->one();

$tmp = $refetchedEntry->getSerializedFieldValues();
$tmp = $this->getSerializedFieldValues($refetchedEntry);
}

if (empty($tmp)) {
Expand All @@ -278,6 +278,30 @@ public function syncElementContent(ElementEvent $event, array $elementSettings)
}
}

/**
* @param Entry|craft\commerce\elements\Product|GlobalSet $element
*/
public function getSerializedFieldValues($element)
{
$fields = Craft::$app->fields->getFieldsByLayoutId($element->getFieldLayout()->id);
$serializedValues = [];

// fix for https://github.com/spicywebau/craft-neo/issues/391
// child elements of a disabled neo blocks will still be in the serialized response, but their parent not
// solution: copy all disabled blocks too
foreach ($fields as $field) {
$value = $element->getFieldValue($field->handle);

if ($value instanceof ElementQuery) {
$serializedValues[$field->handle] = $field->serializeValue($value->status([Element::STATUS_ENABLED, Element::STATUS_DISABLED]), $element);
} else {
$serializedValues[$field->handle] = $field->serializeValue($value, $element);
}
}

return $serializedValues;
}

/**
* @param Entry|craft\commerce\elements\Product|GlobalSet $element
* @return array
Expand Down

0 comments on commit 91334a1

Please sign in to comment.