Skip to content

Commit

Permalink
Update ModuleController.php
Browse files Browse the repository at this point in the history
  • Loading branch information
darita92 authored and ifox committed Jan 21, 2021
1 parent 98984bd commit fce6465
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/Http/Controllers/Admin/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ abstract class ModuleController extends Controller
*/
protected $titleColumnKey = 'title';

/**
* Name of the index column to use as identifier column.
*
* @var string
*/
protected $identifierColumnKey = 'id';

/**
* Attribute to use as title in forms.
*
Expand Down Expand Up @@ -378,11 +385,11 @@ public function store($parentModuleId = null)
if ($parentModuleId) {
$params = [
Str::singular(explode('.', $this->moduleName)[0]) => $parentModuleId,
Str::singular(explode('.', $this->moduleName)[1]) => $item->id,
Str::singular(explode('.', $this->moduleName)[1]) => $item[$this->identifierColumnKey],
];
} else {
$params = [
Str::singular($this->moduleName) => $item->id,
Str::singular($this->moduleName) => $item[$this->identifierColumnKey],
];
}

Expand Down Expand Up @@ -582,7 +589,7 @@ public function restoreRevision($id)
{
if ($this->request->has('revisionId')) {
$item = $this->repository->previewForRevision($id, $this->request->get('revisionId'));
$item->id = $id;
$item[$this->identifierColumnKey] = $id;
$item->cmsRestoring = true;
} else {
throw new NotFoundHttpException();
Expand Down Expand Up @@ -956,16 +963,16 @@ protected function getIndexTableData($items)
$canDuplicate = $this->getIndexOption('duplicate');

return array_replace([
'id' => $item->id,
'id' => $item[$this->identifierColumnKey],
'name' => $name,
'publish_start_date' => $item->publish_start_date,
'publish_end_date' => $item->publish_end_date,
'edit' => $canEdit ? $this->getModuleRoute($item->id, 'edit') : null,
'duplicate' => $canDuplicate ? $this->getModuleRoute($item->id, 'duplicate') : null,
'delete' => $itemCanDelete ? $this->getModuleRoute($item->id, 'destroy') : null,
'edit' => $canEdit ? $this->getModuleRoute($item[$this->identifierColumnKey], 'edit') : null,
'duplicate' => $canDuplicate ? $this->getModuleRoute($item[$this->identifierColumnKey], 'duplicate') : null,
'delete' => $itemCanDelete ? $this->getModuleRoute($item[$this->identifierColumnKey], 'destroy') : null,
] + ($this->getIndexOption('editInModal') ? [
'editInModal' => $this->getModuleRoute($item->id, 'edit'),
'updateUrl' => $this->getModuleRoute($item->id, 'update'),
'editInModal' => $this->getModuleRoute($item[$this->identifierColumnKey], 'edit'),
'updateUrl' => $this->getModuleRoute($item[$this->identifierColumnKey], 'update'),
] : []) + ($this->getIndexOption('publish') && ($item->canPublish ?? true) ? [
'published' => $item->published,
] : []) + ($this->getIndexOption('feature') && ($item->canFeature ?? true) ? [
Expand Down Expand Up @@ -1019,7 +1026,7 @@ protected function getItemColumnData($item, $column)
$field = $column['nested'];
$nestedCount = $item->{$column['nested']}->count();
$value = '<a href="';
$value .= moduleRoute("$this->moduleName.$field", $this->routePrefix, 'index', [$item->id]);
$value .= moduleRoute("$this->moduleName.$field", $this->routePrefix, 'index', [$item[$this->identifierColumnKey]]);
$value .= '">' . $nestedCount . " " . (strtolower($nestedCount > 1
? Str::plural($column['title'])
: Str::singular($column['title']))) . '</a>';
Expand Down Expand Up @@ -1272,9 +1279,9 @@ protected function getBrowserTableData($items)
unset($columnsData[$this->titleColumnKey]);

return [
'id' => $item->id,
'id' => $item[$this->identifierColumnKey],
'name' => $name,
'edit' => moduleRoute($this->moduleName, $this->routePrefix, 'edit', $item->id),
'edit' => moduleRoute($this->moduleName, $this->routePrefix, 'edit', $item[$this->identifierColumnKey]),
'endpointType' => $this->repository->getMorphClass(),
] + $columnsData + ($withImage && !array_key_exists('thumbnail', $columnsData) ? [
'thumbnail' => $item->defaultCmsImage(['w' => 100, 'h' => 100]),
Expand Down Expand Up @@ -1432,20 +1439,20 @@ protected function form($id, $item = null)
'translate' => $this->moduleHas('translations'),
'translateTitle' => $this->titleIsTranslatable(),
'permalink' => $this->getIndexOption('permalink'),
'createWithoutModal' => !$item->id && $this->getIndexOption('skipCreateModal'),
'createWithoutModal' => !$item[$this->identifierColumnKey] && $this->getIndexOption('skipCreateModal'),
'form_fields' => $this->repository->getFormFields($item),
'baseUrl' => $baseUrl,
'permalinkPrefix' => $this->getPermalinkPrefix($baseUrl),
'saveUrl' => $item->id ? $this->getModuleRoute($item->id, 'update') : moduleRoute($this->moduleName, $this->routePrefix, 'store', [$this->submoduleParentId]),
'saveUrl' => $item[$this->identifierColumnKey] ? $this->getModuleRoute($item[$this->identifierColumnKey], 'update') : moduleRoute($this->moduleName, $this->routePrefix, 'store', [$this->submoduleParentId]),
'editor' => Config::get('twill.enabled.block-editor') && $this->moduleHas('blocks') && !$this->disableEditor,
'blockPreviewUrl' => Route::has('admin.blocks.preview') ? URL::route('admin.blocks.preview') : '#',
'availableRepeaters' => $this->getRepeaterList()->toJson(),
'revisions' => $this->moduleHas('revisions') ? $item->revisionsArray() : null,
] + (Route::has($previewRouteName) && $item->id ? [
'previewUrl' => moduleRoute($this->moduleName, $this->routePrefix, 'preview', $item->id),
] + (Route::has($previewRouteName) && $item[$this->identifierColumnKey] ? [
'previewUrl' => moduleRoute($this->moduleName, $this->routePrefix, 'preview', $item[$this->identifierColumnKey]),
] : [])
+ (Route::has($restoreRouteName) && $item->id ? [
'restoreUrl' => moduleRoute($this->moduleName, $this->routePrefix, 'restoreRevision', $item->id),
+ (Route::has($restoreRouteName) && $item[$this->identifierColumnKey] ? [
'restoreUrl' => moduleRoute($this->moduleName, $this->routePrefix, 'restoreRevision', $item[$this->identifierColumnKey]),
] : []);

return array_replace_recursive($data, $this->formData($this->request));
Expand Down

0 comments on commit fce6465

Please sign in to comment.