Skip to content

Commit

Permalink
fix error when translatable null
Browse files Browse the repository at this point in the history
  • Loading branch information
bastihilger committed Oct 28, 2021
1 parent 1db4100 commit 02c8aae
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions src/TranslatableFieldMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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);
}

Expand All @@ -63,15 +65,15 @@ public function translatable()
'original_attribute' => $this->attribute,
'original_component' => $component,
'locales' => $locales,
'value' => $value ?: ($this->resolveDefaultValue($request) ?? ""),
'value' => $value ?: ($this->resolveDefaultValue($request) ?? ''),
],
]);

$this->component = 'translatable-field';

// 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;
}
Expand All @@ -83,30 +85,34 @@ 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
*/
if ($this instanceof Textarea) {
$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] ?? '';
}
});

Expand All @@ -118,32 +124,42 @@ 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);
}

return $this;
}

$setRule($locales, $rules);

return $this;
};
}
Expand Down

0 comments on commit 02c8aae

Please sign in to comment.