Skip to content

Commit

Permalink
Merge pull request #66 from lilt/3.x-development
Browse files Browse the repository at this point in the history
Mark all fields as changed on draft creation
  • Loading branch information
hadomskyi authored Nov 10, 2022
2 parents 50efa07 + 4525776 commit e4fb8b9
Show file tree
Hide file tree
Showing 3 changed files with 112 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/).

## 3.1.1- 2022-11-10
### Fixed
- Draft apply issue (all fields in translation draft now marked as changed for new drafts)

## 3.1.0 - 2022-10-11
### Added
- Retry logic for failed jobs
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": "lilt/craft-lilt-plugin",
"description": "The Lilt plugin makes it easy for you to send content to Lilt for translation right from within Craft CMS.",
"type": "craft-plugin",
"version": "3.1.0",
"version": "3.1.1",
"keywords": [
"craft",
"cms",
Expand Down
117 changes: 107 additions & 10 deletions src/services/handlers/CreateDraftHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
namespace lilthq\craftliltplugin\services\handlers;

use Craft;
use craft\base\Element;
use craft\base\ElementInterface;
use craft\base\FieldInterface;
use craft\db\Table;
use craft\db\Table as DbTable;
use craft\elements\db\ElementQuery;
use craft\elements\Entry;
use craft\errors\ElementNotFoundException;
use craft\errors\InvalidFieldException;
use craft\helpers\Db;
use lilthq\craftliltplugin\Craftliltplugin;
use lilthq\craftliltplugin\datetime\DateTime;
use lilthq\craftliltplugin\parameters\CraftliltpluginParameters;
use Throwable;
use yii\base\Exception;

Expand Down Expand Up @@ -72,16 +80,6 @@ public function create(
foreach ($fields as $field) {
$field->copyValue($element, $draft);
}
/*
if(
get_class($field) === CraftliltpluginParameters::BENF_NEO_FIELD
|| get_class($field) === CraftliltpluginParameters::CRAFT_FIELDS_MATRIX
|| get_class($field) === CraftliltpluginParameters::CRAFT_FIELDS_SUPER_TABLE
) {
$draft->setFieldValue($field->handle, $element->getFieldValue($field->handle));
}
*/
//Craft::$app->elements->saveElement($draft);

$draft->title = $element->title;

Expand All @@ -91,6 +89,105 @@ public function create(

Craft::$app->elements->saveElement($draft);

$this->markFieldsAsChanged($draft);

return $draft;
}

/**
* @throws InvalidFieldException
* @throws \yii\db\Exception
*/
private function markFieldsAsChanged(ElementInterface $element): void
{
$fieldLayout = $element->getFieldLayout();
$fields = $fieldLayout ? $fieldLayout->getFields() : [];

foreach ($fields as $field) {
if (
get_class($field) === CraftliltpluginParameters::CRAFT_FIELDS_MATRIX
|| get_class($field) === CraftliltpluginParameters::BENF_NEO_FIELD
|| get_class($field) === CraftliltpluginParameters::CRAFT_FIELDS_SUPER_TABLE
) {

/**
* @var ElementQuery $matrixBlockQuery
*/
$matrixBlockQuery = $element->getFieldValue($field->handle);

/**
* @var Element[] $blockElements
*/
$blockElements = $matrixBlockQuery->all();

foreach ($blockElements as $blockElement) {
$this->markFieldsAsChanged($blockElement);
}

continue;
}

$this->upsertChangedFields($element, $field);
$this->upsertChangedAttributes($element);
}
}



/**
* @throws \yii\db\Exception
*/
private function upsertChangedFields(ElementInterface $element, FieldInterface $field): void
{
$userId = Craft::$app->getUser()->getId();
$timestamp = Db::prepareDateForDb(new DateTime());

$insert = [
'elementId' => $element->getId(),
'siteId' => $element->getSite()->id,
'fieldId' => $field->id,
];

$update = [
'dateUpdated' => $timestamp,
'propagated' => $element->propagating,
'userId' => $userId,
];

Db::upsert(
DbTable::CHANGEDFIELDS,
$insert,
$update,
[],
false
);
}

private function upsertChangedAttributes(ElementInterface $element, array $attributes = ['title']): void
{
$userId = Craft::$app->getUser()->getId();
$timestamp = Db::prepareDateForDb(new DateTime());

foreach ($attributes as $attribute) {
$insert = [
'elementId' => $element->id,
'siteId' => $element->siteId,
'attribute' => $attribute,
];

$update = [
'dateUpdated' => $timestamp,
'propagated' => $element->propagating,
'userId' => $userId,
];

Db::upsert(
Table::CHANGEDATTRIBUTES,
$insert,
$update,
[],
false
);
}
}
}

0 comments on commit e4fb8b9

Please sign in to comment.