Skip to content

Commit

Permalink
Add setTitle method and tab to row trait
Browse files Browse the repository at this point in the history
  • Loading branch information
kongulov committed Oct 15, 2020
1 parent 16a1410 commit 4e5047c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/NovaTabTranslatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class NovaTabTranslatable extends Field
*/
public $component = 'nova-tab-translatable';

public $name = 'Tab translatable';
public $data = [];
public $locales = [];
public $requiredLocales = [];
Expand All @@ -35,16 +36,16 @@ 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();
else
$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;

Expand All @@ -58,6 +59,12 @@ public function __construct(array $fields = [])
]);
}

public function setTitle($title){
$this->name = $title;

return $this;
}

protected function createTranslatableFields()
{
collect($this->locales)
Expand Down
31 changes: 23 additions & 8 deletions src/TranslatableTabTrait.php → src/TranslatableTabToRowTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Laravel\Nova\Fields\FieldCollection;
use Laravel\Nova\Http\Requests\NovaRequest;

trait TranslatableTabTrait
trait TranslatableTabToRowTrait
{
protected $childFieldsArr = [];

Expand All @@ -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]);
}
}
}


Expand Down Expand Up @@ -86,29 +97,33 @@ 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',
]);
}

/**
* @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;
}
}
}
Expand Down

0 comments on commit 4e5047c

Please sign in to comment.