-
Notifications
You must be signed in to change notification settings - Fork 576
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for browsers using HasRelated behavior
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace A17\Twill\Repositories\Behaviors; | ||
|
||
/** | ||
* Mimic HandleBrowsers, but when the relation uses | ||
* HasRelated instead of being a proper model relation. | ||
* | ||
* @see A17\Twill\Repositories\Behaviors\HandleBrowsers | ||
* @see https://github.com/area17/twill/discussions/940 | ||
*/ | ||
trait HandleRelatedBrowsers | ||
{ | ||
/** | ||
* All related browsers used in the model, as an array of browser names: | ||
* [ | ||
* 'books', | ||
* 'publications' | ||
* ]. | ||
* | ||
* When only the browser name is given here, its rest information will be inferred from the name. | ||
* Each browser's detail can also be overriden with an array | ||
* [ | ||
* 'books', | ||
* 'publication' => [ | ||
* 'relation' => 'magazine', | ||
* 'model' => 'Magazine' | ||
* ] | ||
* ] | ||
* | ||
* @var string|array(array)|array(mix(string|array)) | ||
*/ | ||
protected $relatedBrowsers = []; | ||
|
||
/** | ||
* @param \A17\Twill\Models\Model $object | ||
* @param array $fields | ||
* @return void | ||
*/ | ||
public function afterSaveHandleRelatedBrowsers($object, $fields) | ||
{ | ||
foreach ($this->getRelatedBrowsers() as $browser) { | ||
$this->updateRelatedBrowser($object, $fields, $browser['browserName']); | ||
} | ||
} | ||
|
||
/** | ||
* @param \A17\Twill\Models\Model $object | ||
* @param array $fields | ||
* @return array | ||
*/ | ||
public function getFormFieldsHandleRelatedBrowsers($object, $fields) | ||
{ | ||
foreach ($this->getRelatedBrowsers() as $browser) { | ||
$fields['browsers'][$browser['browserName']] = $this->getFormFieldsForRelatedBrowser($object, $browser['relation']); | ||
} | ||
|
||
return $fields; | ||
} | ||
|
||
/** | ||
* Get all related browser' detail info from the $relatedBrowsers attribute. | ||
* The missing information will be inferred by convention of Twill. | ||
* | ||
* @return Illuminate\Support\Collection | ||
*/ | ||
protected function getRelatedBrowsers() | ||
{ | ||
return collect($this->relatedBrowsers)->map(function ($browser, $key) { | ||
$browserName = is_string($browser) ? $browser : $key; | ||
$moduleName = !empty($browser['moduleName']) ? $browser['moduleName'] : $this->inferModuleNameFromBrowserName($browserName); | ||
|
||
return [ | ||
'relation' => !empty($browser['relation']) ? $browser['relation'] : $this->inferRelationFromBrowserName($browserName), | ||
'model' => !empty($browser['model']) ? $browser['model'] : $this->inferModelFromModuleName($moduleName), | ||
'browserName' => $browserName, | ||
]; | ||
})->values(); | ||
} | ||
} |