Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.15k stars 891 forks source link

removeButton doesn't work #631

Closed terion-name closed 7 years ago

terion-name commented 7 years ago
"backpack/base": "^0.7.18",
"backpack/crud": "^3.2",
class VotesController extends AdminCrudController
{
    public function setup()
    {
        parent::setup();

        $this->declareList();
        $this->declareForm();
        $this->declareFilters();

        $this->crud->removeButton('create');
    }
2017-04-24 15 15 12
lloy0076 commented 7 years ago

If you override the addButton code from the Buttons trait, code which currently looks like this (see https://github.com/Laravel-Backpack/CRUD/blob/master/src/PanelTraits/Buttons.php#L43):

    /**

     * Add a button to the CRUD table view.
     * @param string $stack    Where should the button be visible? Options: top, line, bottom
     * @param string $name     The name of the button. Unique.
     * @param string $type     Type of button: view or model_function
     * @param string $content  The HTML for the button
     * @param string $position Where in the stack it should be placed: beginning or end

     */
    public function addButton($stack, $name, $type, $content, $position = false)
    {
        if ($position == false) {
            switch ($stack) {
                case 'line':
                    $position = 'beginning';
                    break;

                default:
                    $position = 'end';
                    break;
            }
        }

        switch ($position) {
            case 'beginning':
                $this->buttons->prepend(new CrudButton($stack, $name, $type, $content));
                break;

            default:
                $this->buttons->push(new CrudButton($stack, $name, $type, $content));
                break;
        }
    }

...and do something like dump($this->buttons) at the end of that, what does it show you?

If it is null then something seems to be wrong.

Also, I wonder if the $this->crud itself is the thing that happens to be null or unset?

That said, @tabacitu - should that removeButton function check to make sure that $this->buttons is not unset/null anyway (and maybe throw/log a sane message depending on if it should be allowed to be null?

terion-name commented 7 years ago

@lloy0076 I've figured out that at init stage buttons is null. They are filled when action dispatches. So I am forced to write something like this:

public function index()
    {
       $this->crud->removeButton('create');
        return parent::index();
    }

Not very convenient. I think that removeButton should be lazy, form a stack of calls and run them at action dispatch when it is needed

lloy0076 commented 7 years ago

What's your AdminCrudController?

I just brought up:

{                                                     
    "name": "laravel/laravel",                        
    "description": "The Laravel Framework.",          
    "keywords": ["framework", "laravel"],             
    "license": "MIT",                                 
    "type": "project",                                
    "require": {                                      
        "php": ">=5.6.4",                             
        "backpack/base": "^0.7.18",                   
        "backpack/crud": "^3.2",                      
        "backpack/permissionmanager": "^2.1",         
        "laravel/framework": "5.4.*",                 
        "laravel/tinker": "~1.0"                      
    },                                                
    "require-dev": {                                  
        "backpack/generators": "^1.1",                
        "fzaninotto/faker": "~1.4",                   
        "laracasts/generators": "dev-master as 1.1.4",
        "mockery/mockery": "0.9.*",                   
        "phpunit/phpunit": "~5.7"                     
    },                                                

...and made a Test controller and have:

<?php

namespace App\Http\Controllers\Admin;

use Backpack\CRUD\app\Http\Controllers\CrudController;

// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\TestRequest as StoreRequest;
use App\Http\Requests\TestRequest as UpdateRequest;

class TestCrudController extends CrudController
{

    public function setUp()
    {

        /*
        |--------------------------------------------------------------------------
        | BASIC CRUD INFORMATION
        |--------------------------------------------------------------------------
        */
        $this->crud->setModel('App\Models\Test');
        $this->crud->setRoute(config('backpack.base.route_prefix') . '/test');
        $this->crud->setEntityNameStrings('test', 'tests');

        /*
        |--------------------------------------------------------------------------
        | BASIC CRUD INFORMATION
        |--------------------------------------------------------------------------
        */

        $this->crud->setFromDb();

        $this->crud->removeButton('create');
// More PHP Code
// ...

...and it works for me?

terion-name commented 7 years ago

My base controller is simple as:

namespace Admin\Http\Controllers;

use Backpack\CRUD\app\Http\Controllers\CrudController;
use Admin\CrudPanel;
use App\Model\City;

/**
 * Class AdminCrudController
 * @package Admin\Http\Controllers
 * @property CrudPanel $crud
 */
class AdminCrudController extends CrudController
{
    /** @var City */
    protected $city;

    function __construct(City $city)
    {
        $this->city = $city;
        parent::__construct();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getQuery()
    {
        return clone $this->crud->query;
    }
}
lloy0076 commented 7 years ago

@terion-name - hmm, I've bought up:

Ignoring the fact my "city" model has a "name" and "phone_no" and "description" set of fields, this works for me.

I've tried to copy/paste what you have above but obviously don't know what your declareSTAR methods do.

terion-name commented 7 years ago

@lloy0076 declare methods simply populate forms and filters.

Very strange though... I don't get how similar code can work differently. Will dig further

lloy0076 commented 7 years ago

@terion-name - clutching at straws here - maybe PHP? Laravel version?

terion-name commented 7 years ago

7.1

tabacitu commented 7 years ago

Hmm... just tested it and it works as expected https://cl.ly/3p0k0P0s0t0x so I'll close the issue for now. If you guys find out what happened and discover it's a bug, feel free to reopen it.

Cheers!