Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.2] Move Show operation setup in its own method #3132

Merged
merged 16 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');

tabacitu marked this conversation as resolved.
Show resolved Hide resolved
// 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' => true,
tabacitu marked this conversation as resolved.
Show resolved Hide resolved

];