Skip to content

Commit

Permalink
fix(SFT-1323): neo fields not saving (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
seandelaney authored Sep 12, 2024
1 parent 719111c commit 81f933b
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions packages/plugin/src/Elements/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use craft\i18n\Locale;
use craft\models\FieldLayout;
use craft\models\FieldLayoutTab;
use craft\web\UploadedFile;
use Illuminate\Support\Collection;
use RRule\RRule;
use Solspace\Calendar\Calendar;
Expand Down Expand Up @@ -145,6 +146,8 @@ class Event extends Element implements \JsonSerializable
/** @var Event[] */
private $occurrenceCache = [];

private ?string $_fieldParamNamePrefix = null;

/**
* Event constructor.
*/
Expand Down Expand Up @@ -1612,6 +1615,67 @@ public static function actions(string $source): array
return $event->actions;
}

public function setFieldValuesFromRequest(string $paramNamespace = ''): void
{
$this->setFieldParamNamespace($paramNamespace);

if (isset($this->_fieldParamNamePrefix)) {
$values = \Craft::$app->getRequest()->getBodyParam($paramNamespace, []);
} else {
$values = \Craft::$app->getRequest()->getBodyParams();
}

// Run through this multiple times, in case any fields become visible as a result of other field value changes
$processedFields = [];
do {
$processedAnyFields = false;
foreach ($this->fieldLayoutFields(true) as $field) {
// Have we already processed this field?
if (isset($processedFields[$field->id])) {
continue;
}

$processedFields[$field->id] = true;
$processedAnyFields = true;

// Do we have any post data for this field?
if (isset($values[$field->handle])) {
$value = $values[$field->handle];
} elseif (
isset($this->_fieldParamNamePrefix)
&& UploadedFile::getInstancesByName("{$this->_fieldParamNamePrefix}.{$field->handle}")
) {
// A file was uploaded for this field
$value = null;
} else {
continue;
}

// Add in additional support for other field types
if ($field instanceof \benf\neo\Field) {
if (!empty($values[$field->handle]['blocks']) && \is_array($values[$field->handle]['blocks'])) {
$index = 0;
$blocks = [];
foreach ($values[$field->handle]['blocks'] as $block) {
$blocks['new'.$index] = $block;
++$index;
}
$this->setFieldValues([$field->handle => $blocks]);
} else {
$this->setFieldValues([$field->handle => '']);
}
} else {
$this->setFieldValueFromRequest($field->handle, $value);
}
}
} while ($processedAnyFields);
}

public function setFieldParamNamespace(string $namespace): void
{
$this->_fieldParamNamePrefix = '' !== $namespace ? $namespace : null;
}

protected static function defineSources(string $context = null): array
{
$sources = [
Expand Down

0 comments on commit 81f933b

Please sign in to comment.