Skip to content

Commit

Permalink
Prepare new release
Browse files Browse the repository at this point in the history
- Fixed saving new reverse relations
- Fixed bug when importing with Schematic when the target field didn't yet exist
- Fixed bug where targets where validated
  • Loading branch information
boboldehampsink committed Dec 10, 2018
1 parent 8caa52c commit 3b1859f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 33 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## 1.0.4 - 2018-12-10
### Fixed
- Fixed saving new reverse relations
- Fixed bug when importing with Schematic when the target field didn't yet exist
- Fixed bug where targets where validated

## 1.0.3 - 2018-12-07
### Fixed
- Fixed bug when importing in Schematic
- Fixed bug when importing with Schematic

## 1.0.2 - 2018-12-07
### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.3",
"version": "1.0.4",
"name": "robuust/craft-reverserelations",
"description": "Reverse Relations for Craft 3",
"type": "craft-plugin",
Expand Down
10 changes: 8 additions & 2 deletions src/converters/fields/ReverseEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public function getRecordDefinition(Model $record): array
$definition = parent::getRecordDefinition($record);

if ($record->targetFieldId) {
$definition['attributes']['targetField'] = Craft::$app->getFields()->getFieldById($record->targetFieldId)->handle;
$targetField = Craft::$app->getFields()->getFieldById($record->targetFieldId);
if ($targetField) {
$definition['attributes']['targetField'] = $targetField->handle;
}
}

unset($definition['attributes']['targetFieldId']);
Expand All @@ -35,7 +38,10 @@ public function getRecordDefinition(Model $record): array
public function saveRecord(Model $record, array $definition): bool
{
if (array_key_exists('targetField', $definition['attributes'])) {
$record->targetFieldId = Craft::$app->getFields()->getFieldByHandle($definition['attributes']['targetField'])->id;
$targetField = Craft::$app->getFields()->getFieldByHandle($definition['attributes']['targetField']);
if ($targetField) {
$record->targetFieldId = $targetField->id;
}
}

return parent::saveRecord($record, $definition);
Expand Down
59 changes: 30 additions & 29 deletions src/fields/ReverseEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use craft\fields\Entries;
use craft\base\FieldInterface;
use craft\base\ElementInterface;
use craft\events\FieldElementEvent;
use craft\elements\db\ElementQuery;

/**
Expand Down Expand Up @@ -87,16 +86,9 @@ public function normalizeValue($value, ElementInterface $element = null)
/** @var Element|null $element */
$query = parent::normalizeValue($value, $element);

// Get allowed sources
$sources = [];
foreach ($this->inputSources() as $source) {
list($type, $id) = explode(':', $source);
$sources[] = (int) $id;
}

// Overwrite inner join to switch sourceId and targetId
$query->join = [];
if ($value !== '' && $element && $element->id) {
if (!is_array($value) && $value !== '' && $element && $element->id) {
$query->join = [];
$query
->innerJoin(
'{{%relations}} relations',
Expand All @@ -114,7 +106,7 @@ public function normalizeValue($value, ElementInterface $element = null)
],
]
)
->where(['entries.sectionId' => $sources]);
->where(['entries.sectionId' => $this->inputSourceIds()]);
}

return $query;
Expand All @@ -137,29 +129,22 @@ public function afterElementSave(ElementInterface $element, bool $isNew)
}

// Get targets
$targetIds = $element->getFieldValue($this->handle);
$value = $element->getFieldValue($this->handle);

// Loop through sources
/** @var ElementInterface $source */
foreach ($value->all() as $source) {
$target = $source->getFieldValue($field->handle);

// Loop through targets
/** @var ElementInterface $target */
foreach ($targetIds->all() as $target) {
// Set this element on that entry
$target->setFieldValue(
$field->handle,
array_merge($target->getFieldValue($field->handle)->ids(), [$element->id])
Craft::$app->getRelations()->saveRelations(
$field,
$source,
array_merge($target->ids(), [$element->id])
);

// Save target
Craft::$app->elements->saveElement($target);
}

// This code is from the grandparent method
// Trigger an 'afterElementSave' event
if ($this->hasEventHandlers(self::EVENT_AFTER_ELEMENT_SAVE)) {
$this->trigger(self::EVENT_AFTER_ELEMENT_SAVE, new FieldElementEvent([
'element' => $element,
'isNew' => $isNew,
]));
}
Field::afterElementSave($element, $isNew);
}

/**
Expand Down Expand Up @@ -199,6 +184,22 @@ public function settingsAttributes(): array
return $attributes;
}

/**
* Get allowed input source ids.
*
* @return array
*/
private function inputSourceIds(): array
{
$sources = [];
foreach ($this->inputSources() as $source) {
list($type, $id) = explode(':', $source);
$sources[] = (int) $id;
}

return $sources;
}

/**
* Determine if a field can save a reverse relation.
*
Expand Down

0 comments on commit 3b1859f

Please sign in to comment.