Crud: [4.0][Bug] Print button is missing from the Show view

Created on 15 Dec 2019  路  6Comments  路  Source: Laravel-Backpack/CRUD

Bug report

What I did

    $this->crud->enableExportButtons();

What I expected to happen

like version 3.6
after click the (show button) see page with (print button) to print the data in the page

What happened

(print button) not exists in the new version

What I've already tried to fix it

    $this->crud->enableExportButtons();

Backpack, Laravel, PHP, DB version

Bug SHOULD

Most helpful comment

To Maintain Genericity and to print each Item as per it's own layout. It did the following:

1)Created Print Operation

<?php

namespace App\Http\Controllers\Admin\Operations;

use Illuminate\Support\Facades\Route;

trait PrintOperation
{
    /**
     * Define which routes are needed for this operation.
     *
     * @param string $segment    Name of the current entity (singular). Used as first URL segment.
     * @param string $routeName  Prefix of the route name.
     * @param string $controller Name of the current CrudController.
     */
    protected function setupPrintRoutes($segment, $routeName, $controller)
    {
        Route::get($segment.'/{id}/print', [
            'as'        => $routeName.'.print',
            'uses'      => $controller.'@print',
            'operation' => 'print',
        ]);
    }

    /**
     * Add the default settings, buttons, etc that this operation needs.
     */
    protected function setupPrintDefaults()
    {
        $this->crud->allowAccess('print');

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

        $this->crud->operation(['list', 'show'], function () {
            // $this->crud->addButton('top', 'print', 'view', 'crud::buttons.print');
            $this->crud->addButton('line', 'print', 'view', 'crud::buttons.print');
        });
    }

    /**
     * Show the view for performing the operation.
     *
     * @return Response
     */
    public function print($id)
    {
        $this->crud->hasAccessOrFail('print');

        // prepare the fields you need to show
        $this->data['crud'] = $this->crud;
        $this->data['title'] = $this->crud->getTitle() ?? 'print '.$this->crud->entity_name;

        // load the view
        return view("crud::operations.print", $this->data);
    }
}

2)Created An Iframe Widget:

<iframe name="print_frame" onload="if(window.printval)window.frames['print_frame'].print();" width=0 height=0 frameborder=0 ></iframe>
@push('after_scripts')
<script type="text/javascript">
    window.printval = true;
</script>
@endpush

3)Created Print Button

@if ($crud->hasAccess('print'))
    <a href="{{ url($crud->route.'/'.$entry->getKey().'/print') }}" target="print_frame" class="btn btn-sm btn-link"><i class="fa fa-eye"></i> Print</a>
@endif

4)Added Widget & Operation in the Controller:

use \App\Http\Controllers\Admin\Operations\PrintOperation;
$this->data['widgets']['before_content'] = [
            [
                'type' => 'iframe',
            ],
        ];

This works like ajax printing ;)

All 6 comments

Press Ctrl P in the show page.

To Maintain Genericity and to print each Item as per it's own layout. It did the following:

1)Created Print Operation

<?php

namespace App\Http\Controllers\Admin\Operations;

use Illuminate\Support\Facades\Route;

trait PrintOperation
{
    /**
     * Define which routes are needed for this operation.
     *
     * @param string $segment    Name of the current entity (singular). Used as first URL segment.
     * @param string $routeName  Prefix of the route name.
     * @param string $controller Name of the current CrudController.
     */
    protected function setupPrintRoutes($segment, $routeName, $controller)
    {
        Route::get($segment.'/{id}/print', [
            'as'        => $routeName.'.print',
            'uses'      => $controller.'@print',
            'operation' => 'print',
        ]);
    }

    /**
     * Add the default settings, buttons, etc that this operation needs.
     */
    protected function setupPrintDefaults()
    {
        $this->crud->allowAccess('print');

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

        $this->crud->operation(['list', 'show'], function () {
            // $this->crud->addButton('top', 'print', 'view', 'crud::buttons.print');
            $this->crud->addButton('line', 'print', 'view', 'crud::buttons.print');
        });
    }

    /**
     * Show the view for performing the operation.
     *
     * @return Response
     */
    public function print($id)
    {
        $this->crud->hasAccessOrFail('print');

        // prepare the fields you need to show
        $this->data['crud'] = $this->crud;
        $this->data['title'] = $this->crud->getTitle() ?? 'print '.$this->crud->entity_name;

        // load the view
        return view("crud::operations.print", $this->data);
    }
}

2)Created An Iframe Widget:

<iframe name="print_frame" onload="if(window.printval)window.frames['print_frame'].print();" width=0 height=0 frameborder=0 ></iframe>
@push('after_scripts')
<script type="text/javascript">
    window.printval = true;
</script>
@endpush

3)Created Print Button

@if ($crud->hasAccess('print'))
    <a href="{{ url($crud->route.'/'.$entry->getKey().'/print') }}" target="print_frame" class="btn btn-sm btn-link"><i class="fa fa-eye"></i> Print</a>
@endif

4)Added Widget & Operation in the Controller:

use \App\Http\Controllers\Admin\Operations\PrintOperation;
$this->data['widgets']['before_content'] = [
            [
                'type' => 'iframe',
            ],
        ];

This works like ajax printing ;)

@tifaahmed you're right. It looks like we've mistakenly removed the Print button from the Show operation in the 4.0 rewrite. We'll fix this ASAP.

In the meantime - OMFG @karandatwani92 ! This is EXACTLY why I worked so hard at the 4.0 operations and widgets, so you can do that kind of stuff. I'm so glad you put that together, even though the documentation on doing this kind of thing is sparse at the moment. How do you feel about writing a short blog article / tutorial about the operation? Just a small intro and screenshots , in addition to the code, would be enough in my opinion. If you have a blog yourself, you could post there, please give us the link. If not, we could post it on backpackforlaravel.com in your name. Please reach out to [email protected] - I think many people would love to see this new operation you build. Plus, having a little publicity to your name couldn't hurt right? 馃槃

Cheers!

thanks for help my problem solved

@tabacitu I would love to work for backpackforlaravel.com. I will reach out to you soon over mail. Thanks :)

@karandatwani92 please do.

Let's leave this open until we fix it. The way I see it, the print button above is VERY nice but it shouldn't be the default feature. The default feature should be the exact same that was present in 3.6: a simple button like <a href="#" onclick="window.print()"><i class="fa fa-print"></i></a> that will print the current page.

Of course, because we've suffered a redesign, we have to test thoroughly and see what else needs changing so that the Print view looks good.

@pxpm can you please take a look and create a PR to fix this?

Was this page helpful?
0 / 5 - 0 ratings