From 4e5047c481faf93f95be6415c7429f9ec3835891 Mon Sep 17 00:00:00 2001 From: kongulov Date: Thu, 15 Oct 2020 14:21:46 +0400 Subject: [PATCH] Add setTitle method and tab to row trait --- src/NovaTabTranslatable.php | 13 ++++++-- ...rait.php => TranslatableTabToRowTrait.php} | 31 ++++++++++++++----- 2 files changed, 33 insertions(+), 11 deletions(-) rename src/{TranslatableTabTrait.php => TranslatableTabToRowTrait.php} (81%) diff --git a/src/NovaTabTranslatable.php b/src/NovaTabTranslatable.php index ddb6315..5312b24 100644 --- a/src/NovaTabTranslatable.php +++ b/src/NovaTabTranslatable.php @@ -19,6 +19,7 @@ class NovaTabTranslatable extends Field */ public $component = 'nova-tab-translatable'; + public $name = 'Tab translatable'; public $data = []; public $locales = []; public $requiredLocales = []; @@ -35,7 +36,7 @@ class NovaTabTranslatable extends Field public function __construct(array $fields = []) { - parent::__construct('Tab translatable'); + parent::__construct($this->name); $config = config('tab-translatable'); if($config['source'] == 'database') $this->locales = $config['database']['model']::pluck($config['database']['code_field'])->toArray(); @@ -43,8 +44,8 @@ public function __construct(array $fields = []) $this->locales = $config['locales']; $this->displayLocalizedNameUsingCallback = self::$displayLocalizedNameByDefaultUsingCallback ?? function (Field $field, string $locale) { - return ucfirst($field->name) . " [{$locale}]"; - }; + return ucfirst($field->name) . " [{$locale}]"; + }; $this->originalFields = $fields; @@ -58,6 +59,12 @@ public function __construct(array $fields = []) ]); } + public function setTitle($title){ + $this->name = $title; + + return $this; + } + protected function createTranslatableFields() { collect($this->locales) diff --git a/src/TranslatableTabTrait.php b/src/TranslatableTabToRowTrait.php similarity index 81% rename from src/TranslatableTabTrait.php rename to src/TranslatableTabToRowTrait.php index 925254a..87de0d7 100644 --- a/src/TranslatableTabTrait.php +++ b/src/TranslatableTabToRowTrait.php @@ -8,7 +8,7 @@ use Laravel\Nova\Fields\FieldCollection; use Laravel\Nova\Http\Requests\NovaRequest; -trait TranslatableTabTrait +trait TranslatableTabToRowTrait { protected $childFieldsArr = []; @@ -22,16 +22,27 @@ public function availableFields(NovaRequest $request) $fields = $this->filter($this->fields($request)); $availableFields = []; - foreach ($fields as $field) { + foreach ($fields as $key => $field) { if ($field instanceof NovaTabTranslatable) { $availableFields[] = $this->filterFieldForRequest($field, $request); + if($this->extractableRequest($request, $this->model())) { + if ($this->doesRouteRequireChildFields()) { + $this->extractChildFields($field->originalFields, $key); + } + } } else { $availableFields[] = $this->filterFieldForRequest($field, $request); } } if ($this->childFieldsArr) { - $availableFields = array_merge($availableFields, $this->childFieldsArr); + for ($i = count($fields)-1; $i >= 0; $i--){ + $field = $fields[$i]; + if ($field instanceof NovaTabTranslatable) { + array_splice($availableFields, $i+1,0, $this->childFieldsArr[$i]); + unset($availableFields[$i]); + } + } } @@ -86,12 +97,16 @@ public function filterFieldsForRequest(Collection $availableFields, NovaRequest */ protected function doesRouteRequireChildFields(): bool { + return Str::endsWith(Route::currentRouteAction(), [ - 'FieldDestroyController@handle', + /*'FieldDestroyController@handle', 'ResourceUpdateController@handle', 'ResourceStoreController@handle', 'AssociatableController@index', - 'MorphableController@index', + 'MorphableController@index',*/ + + 'ResourceIndexController@handle', + 'ResourceShowController@handle', ]); } @@ -99,16 +114,16 @@ protected function doesRouteRequireChildFields(): bool * @param [array] $childFields [meta fields] * @return void */ - protected function extractChildFields($childFields) + protected function extractChildFields($childFields, $key) { foreach ($childFields as $childField) { if ($childField instanceof NovaTabTranslatable) { - $this->extractChildFields($childField->data); + $this->extractChildFields($childField->data, $key); } else { if (array_search($childField->attribute, array_column($this->childFieldsArr, 'attribute')) === false) { // @todo: we should not randomly apply rules to child-fields. $childField = $this->applyRulesForChildFields($childField); - $this->childFieldsArr[] = $childField; + $this->childFieldsArr[$key][] = $childField; } } }