Skip to content

Commit

Permalink
Merge pull request #731 from brandonkelly/normalize-posted-values
Browse files Browse the repository at this point in the history
Pass on `normalizeValueFromRequest()` calls to sub-fields
  • Loading branch information
ttempleton authored Apr 4, 2023
2 parents c7e55ed + aa1c838 commit b1ead89
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use craft\db\Query;
use craft\db\Table;
use craft\elements\db\ElementQueryInterface;
use craft\errors\InvalidFieldException;
use craft\fields\conditions\EmptyFieldConditionRule;
use craft\helpers\ArrayHelper;
use craft\helpers\ElementHelper;
Expand Down Expand Up @@ -592,6 +593,19 @@ public function getStaticHtml(mixed $value, ElementInterface $element): string
* @inheritdoc
*/
public function normalizeValue(mixed $value, ?ElementInterface $element = null): mixed
{
return $this->_normalizeValueInternal($value, $element, false);
}

/**
* @inheritdoc
*/
public function normalizeValueFromRequest(mixed $value, ?ElementInterface $element = null): mixed
{
return $this->_normalizeValueInternal($value, $element, true);
}

private function _normalizeValueInternal(mixed $value, ?ElementInterface $element, bool $fromRequest): mixed
{
if ($value instanceof ElementQueryInterface) {
return $value;
Expand All @@ -606,7 +620,7 @@ public function normalizeValue(mixed $value, ?ElementInterface $element = null):
$query->setCachedResult([]);
$query->useMemoized(false);
} elseif ($element && is_array($value)) {
$elements = $this->_createBlocksFromSerializedData($value, $element);
$elements = $this->_createBlocksFromSerializedData($value, $element, $fromRequest);
$query->setCachedResult($elements);
$query->useMemoized($elements);
}
Expand Down Expand Up @@ -1191,9 +1205,10 @@ private function _getNestingErrorHtml(): string
*
* @param array $value The raw field data.
* @param ElementInterface $element The element associated with this field
* @param bool $fromRequest Whether the data came from the request post data
* @return Block[] The Blocks created from the given data.
*/
private function _createBlocksFromSerializedData(array $value, ElementInterface $element): array
private function _createBlocksFromSerializedData(array $value, ElementInterface $element, bool $fromRequest): array
{
$draftsService = Craft::$app->getDrafts();
$request = Craft::$app->getRequest();
Expand Down Expand Up @@ -1325,7 +1340,16 @@ private function _createBlocksFromSerializedData(array $value, ElementInterface
}

if (isset($blockData['fields'])) {
$block->setFieldValues($blockData['fields']);
foreach ($blockData['fields'] as $fieldHandle => $fieldValue) {
try {
if ($fromRequest) {
$block->setFieldValueFromRequest($fieldHandle, $fieldValue);
} else {
$block->setFieldValue($fieldHandle, $fieldValue);
}
} catch (InvalidFieldException) {
}
}
}

if ($prevBlock) {
Expand Down

0 comments on commit b1ead89

Please sign in to comment.