Skip to content

Commit

Permalink
Nullify raw [object Object] values
Browse files Browse the repository at this point in the history
  • Loading branch information
lindseydiloreto committed Jan 10, 2025
1 parent cf929a6 commit ecbd945
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

### Fixed
- Patched bug in multisite migration. ([#127](https://github.com/doublesecretagency/craft-googlemaps/issues/127))
- Nullify raw `[object Object]` values.

## 5.1.0 - 2024-12-03

Expand Down
37 changes: 31 additions & 6 deletions src/fields/AddressField.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ public function normalizeValue(mixed $value, ?ElementInterface $element = null):
$lat = ($value['lat'] ?? null);
$lng = ($value['lng'] ?? null);
$zoom = ($value['zoom'] ?? null);
// Normalize raw value
$value['raw'] = static::normalizeRaw($value['raw']);
// Return Address model
return new AddressModel([
'elementId' => (int) $elementId,
Expand Down Expand Up @@ -310,12 +312,8 @@ public function normalizeValue(mixed $value, ?ElementInterface $element = null):
$attr['lat'] = ($attr['lat'] ? (float) $attr['lat'] : null);
$attr['lng'] = ($attr['lng'] ? (float) $attr['lng'] : null);

// Check if JSON is valid
// Must use this function to validate (I know it's redundant)
$valid = json_decode($attr['raw']);

// Convert raw data to an array
$attr['raw'] = ($valid ? Json::decode($attr['raw']) : null);
// Normalize raw value
$attr['raw'] = static::normalizeRaw($attr['raw']);

// Get handles of visible subfields
$attr['enabledSubfields'] = $this->_getEnabledSubfields();
Expand All @@ -329,6 +327,33 @@ public function normalizeValue(mixed $value, ?ElementInterface $element = null):
return new AddressModel($attr);
}

/**
* Normalize the raw value.
*
* @param mixed $raw
* @return array|null
*/
public static function normalizeRaw(mixed $raw): ?array
{
// If already an array, return as-is
if (is_array($raw)) {
return $raw;
}

// If not a string, return null
if (!is_string($raw)) {
return null;
}

// If string contains `[object Object]`, return null
if (str_contains($raw, '[object Object]')) {
return null;
}

// Convert string to an array
return Json::decode($raw);
}

// ========================================================================= //

/**
Expand Down
6 changes: 3 additions & 3 deletions src/migrations/m240530_122024_multisite_support.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ private function _migrate(string $fieldLayoutElementUid): void
continue;
}

// Extract raw data from Address field
$raw = ($content['raw'] ?? null);
// Normalize raw value
$content['raw'] = AddressField::normalizeRaw($content['raw'] ?? null);

// Get current time as a fallback
$now = DateTimeHelper::currentUTCDateTime()->format('Y-m-d H:i:s');
Expand All @@ -214,7 +214,7 @@ private function _migrate(string $fieldLayoutElementUid): void
'siteId' => (int) $elementSite['siteId'],
'fieldId' => (int) $content['fieldId'],
'formatted' => ($content['formatted'] ?? null),
'raw' => ($raw ? Json::encode($raw) : null),
'raw' => ($content['raw'] ? Json::encode($content['raw']) : null),
'name' => ($content['name'] ?? null),
'street1' => ($content['street1'] ?? null),
'street2' => ($content['street2'] ?? null),
Expand Down

0 comments on commit ecbd945

Please sign in to comment.