diff --git a/src/TranslatableFieldMixin.php b/src/TranslatableFieldMixin.php index f6dab6d..faf2697 100644 --- a/src/TranslatableFieldMixin.php +++ b/src/TranslatableFieldMixin.php @@ -18,12 +18,12 @@ public function translatable() $this->resolveUsing(function ($value, $resource, $attribute) use ($locales, $component, $originalResolveCallback) { $this->resolveCallback = $originalResolveCallback; $attribute = FieldServiceProvider::normalizeAttribute($attribute); - + // Load value from either the model or from the given $value if (isset($resource) && (is_object($resource) || is_string($resource)) && method_exists($resource, 'getTranslationsArray')) { // In case a model has the HasTranslations trait, but some fields are wrapped // we must be prepared to get an Exception here - + try { $allTranslations = $resource->getTranslationsArray(); $value = []; @@ -40,20 +40,22 @@ public function translatable() } try { - if (!is_array($value)) { + if (! is_array($value)) { if (is_object($value)) { $value = (array) $value; } else { $testValue = json_decode($value, true); - if (is_array($testValue)) $value = $testValue; + if (is_array($testValue)) { + $value = $testValue; + } } } } catch (Exception $e) { } - if (!empty($value)) { + if (! empty($value)) { $value = array_map(function ($val) { - return !is_numeric($val) ? $val : (float) $val; + return ! is_numeric($val) ? $val : (float) $val; }, (array) $value); } @@ -63,7 +65,7 @@ public function translatable() 'original_attribute' => $this->attribute, 'original_component' => $component, 'locales' => $locales, - 'value' => $value ?: ($this->resolveDefaultValue($request) ?? ""), + 'value' => $value ?: ($this->resolveDefaultValue($request) ?? ''), ], ]); @@ -71,7 +73,7 @@ public function translatable() // If it's a CREATE or UPDATE request, we need to trick the validator a bit $hasValidationTrick = property_exists($this, '__validationTrick') && $this->__validationTrick; - if (in_array(request()->method(), ['PUT', 'POST']) && !$hasValidationTrick) { + if (in_array(request()->method(), ['PUT', 'POST']) && ! $hasValidationTrick) { $this->attribute = "{$this->attribute}.*"; $this->__validationTrick = true; } @@ -83,7 +85,7 @@ public function translatable() $this->displayUsing(function ($value, $resource, $attribute) use ($originalDisplayCallback) { $this->displayCallback = $originalDisplayCallback; - /** + /* * Avoid calling resolveForDisplay on the main Textarea instance as it contains a call to e() * and it only accepts string, passing an array will cause a crash */ @@ -91,22 +93,26 @@ public function translatable() $this->displayCallback = null; parent::resolveForDisplay($resource, $attribute); - if (is_string($value)) return $value; + if (is_string($value)) { + return $value; + } + return collect(array_values((array) ($value ?? [])))->filter()->first() ?? ''; } $this->resolveForDisplay($resource, $attribute); + return $value; }); $this->fillUsing(function ($request, $model, $attribute, $requestAttribute) use ($locales) { $realAttribute = FieldServiceProvider::normalizeAttribute($this->meta['translatable']['original_attribute'] ?? $attribute); $translations = $request->{$realAttribute}; - + foreach ($locales as $localeKey => $localeName) { $translationEntry = $model->translateOrNew($localeKey); - $translationEntry->{$realAttribute} = $translations[$localeKey]; + $translationEntry->{$realAttribute} = $translations[$localeKey] ?? ''; } }); @@ -118,25 +124,34 @@ public function rulesFor() { return function ($locales, $rules) { $setRule = function ($locale, $rules) { - if (!in_array($locale, array_keys(FieldServiceProvider::getLocales()))) { + if (! in_array($locale, array_keys(FieldServiceProvider::getLocales()))) { throw new Exception("Invalid locale specified ({$locale})"); } - if (!is_array($rules)) $rules = [$rules]; + if (! is_array($rules)) { + $rules = [$rules]; + } $this->rules['translatable'][$locale] = $rules; + return $this; }; - if (is_callable($locales)) $locales = call_user_func($locales); + if (is_callable($locales)) { + $locales = call_user_func($locales); + } // Array of locales or callable rules if (is_array($locales) || is_callable($rules)) { // Single locale with callable rules - if (!is_array($locales)) return $setRule($locales, call_user_func($rules, $locales)); + if (! is_array($locales)) { + return $setRule($locales, call_user_func($rules, $locales)); + } foreach ($locales as $locale) { $_rules = $rules; - if (is_callable($_rules)) $_rules = call_user_func($rules, $locale); + if (is_callable($_rules)) { + $_rules = call_user_func($rules, $locale); + } $setRule($locale, $_rules); } @@ -144,6 +159,7 @@ public function rulesFor() } $setRule($locales, $rules); + return $this; }; }