Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(methods): Add custom input field setter + fix: moonshine 3+ сompatibility #11

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions src/Fields/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace VI\MoonShineSpatieTranslatable\Fields;

use Illuminate\Support\Str;
use MoonShine\Exceptions\FieldException;
use MoonShine\Fields\Field;
use MoonShine\Fields\Fields;
use MoonShine\Fields\Json;
use MoonShine\Fields\Select;
use MoonShine\Fields\Text;
use MoonShine\Fields\Textarea;
use MoonShine\Fields\TinyMce;
use MoonShine\Contracts\Core\DependencyInjection\FieldsContract;
use MoonShine\Contracts\UI\FieldContract;
use MoonShine\UI\Exceptions\FieldException;
use MoonShine\UI\Fields\Field;
use MoonShine\UI\Fields\Json;
use MoonShine\UI\Fields\Select;
use MoonShine\UI\Fields\Text;
use MoonShine\UI\Fields\Textarea;
use MoonShine\TinyMce\Fields\TinyMce;

use Illuminate\Contracts\View\View;

Expand All @@ -21,7 +22,7 @@ final class Translatable extends Json
protected bool $onlyValue = false;

/**
* @var class-string<Field>
* @var class-string<FieldContract>
*/
protected string $inputField = Text::class;

Expand All @@ -43,23 +44,22 @@ final class Translatable extends Json

protected function prepareFill(array $raw = [], mixed $casted = null): mixed
{
return $casted->getTranslations($this->column());
return $casted->getOriginal()->getTranslations($this->column);
}

/**
* @throws \Throwable
*/
public function onlyValue(
string $value = 'Value',
?Field $valueField = null,
?FieldContract $valueField = null,
): static
{
throw new FieldException('Can`t set onlyValue for this field!');
}

public function getFields(mixed $data = null): Fields
public function getFields(mixed $data = null): FieldsContract
{

$inputField = $this->inputField::make(__('Value'), 'value');

if (empty($this->fields)) {
Expand Down Expand Up @@ -110,7 +110,6 @@ protected function getLanguagesCodes(): array

public function textarea(): static
{

$this->inputField = Textarea::class;

return $this;
Expand All @@ -130,11 +129,22 @@ public function json(): static
return $this;
}

public function customInputField(string $class): static
{
if (!is_subclass_of($class, Field::class)) {
throw new FieldException('The passed class must be a subclass of MoonShine\UI\Fields\Field');
}

$this->inputField = $class;

return $this;
}

public function keyValue(
string $key = 'Language',
string $value = 'Value',
?Field $keyField = null,
?Field $valueField = null,
?FieldContract $keyField = null,
?FieldContract $valueField = null,
): static {
$this->fields([
Select::make($key, 'key')
Expand All @@ -154,6 +164,7 @@ public function hasFields(): bool

protected function resolvePreview(): View|string
{
return $this?->data?->{$this->column()} ?? '';
return $this?->data?->{$this->column} ?? '';
}
}