Skip to content

Commit

Permalink
Merge pull request #3132 from Laravel-Backpack/move-show-operation-se…
Browse files Browse the repository at this point in the history
…tup-in-its-own-method

[4.2] Move Show operation setup in its own method
  • Loading branch information
tabacitu authored Dec 14, 2021
2 parents 9d76778 + 557ff95 commit cd9b1b4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
51 changes: 40 additions & 11 deletions src/app/Http/Controllers/Operations/ShowOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ protected function setupShowDefaults()

$this->crud->operation('show', function () {
$this->crud->loadDefaultOperationSettingsFromConfig();

if (! method_exists($this, 'setupShowOperation')) {
$this->autoSetupShowOperation();
}
});

$this->crud->operation('list', function () {
Expand Down Expand Up @@ -70,18 +74,49 @@ public function show($id)

// get entry ID from Request (makes sure its the last ID for nested resources)
$id = $this->crud->getCurrentEntryId() ?? $id;
$setFromDb = $this->crud->get('show.setFromDb');

// get the info for that entry
$this->data['entry'] = $this->crud->getEntry($id);
// get the info for that entry (include softDeleted items if the trait is used)
if ($this->crud->get('show.softDeletes') && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->crud->model))) {
$this->data['entry'] = $this->crud->getModel()->withTrashed()->findOrFail($id);
} else {
$this->data['entry'] = $this->crud->getEntry($id);
}

$this->data['crud'] = $this->crud;
$this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.preview').' '.$this->crud->entity_name;

// set columns from db
if ($setFromDb) {
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
return view($this->crud->getShowView(), $this->data);
}

/**
* Default behaviour for the Show Operation, in case none has been
* provided by including a setupShowOperation() method in the CrudController.
*/
protected function autoSetupShowOperation()
{
// guess which columns to show, from the database table
if ($this->crud->get('show.setFromDb')) {
$this->crud->setFromDb(false, true);
}

// if the model has timestamps, add columns for created_at and updated_at
if ($this->crud->get('show.timestamps') && $this->crud->model->usesTimestamps()) {
$this->crud->column($this->crud->model->getCreatedAtColumn())->type('datetime');
$this->crud->column($this->crud->model->getUpdatedAtColumn())->type('datetime');
}

// if the model has SoftDeletes, add column for deleted_at
if ($this->crud->get('show.softDeletes') && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->crud->model))) {
$this->crud->column($this->crud->model->getDeletedAtColumn())->type('datetime');
}

// remove the columns that usually don't make sense inside the Show operation
$this->removeColumnsThatDontBelongInsideShowOperation();
}

protected function removeColumnsThatDontBelongInsideShowOperation()
{
// cycle through columns
foreach ($this->crud->columns() as $key => $column) {

Expand Down Expand Up @@ -113,13 +148,7 @@ public function show($id)
}
}

// remove preview button from stack:line
$this->crud->removeButton('show');

// remove bulk actions colums
$this->crud->removeColumns(['blank_first_column', 'bulk_actions']);

// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
return view($this->crud->getShowView(), $this->data);
}
}
11 changes: 11 additions & 0 deletions src/config/backpack/operations/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@
// Define the size/looks of the content div for all CRUDs
// To override per Controller use $this->crud->setShowContentClass('class-string')
'contentClass' => 'col-md-8',

// Automatically add all columns from the db table?
'setFromDb' => true,

// Automatically add created_at and updated_at columns, if model has timestamps?
'timestamps' => true,

// If model has SoftDeletes, allow the admin to access the Show page for
// soft deleted items & add a deleted_at column to ShowOperation?
'softDeletes' => false,

];

0 comments on commit cd9b1b4

Please sign in to comment.