Laravel-Backpack/PageManager

Can't change template when creating new page

chriswright-interconnect opened this issue · 15 comments

Bug report

What I did:

Create a new page from the PageManager CRUD and select a template.

What I expected to happen:

Load the selected template fields.

What happened:

The default (top) template loads but when I try to choose a new template the correct fields won't load and the select input reverts to the top template. The url is updated but the form doesn't load.

What I've already tried to fix it:

Overriding the create method via extending the controller.

public function create($template = false)
{
    if (request()->has('template')) {
        $template = request('template');
    }
    $this->addDefaultPageFields($template);
    $this->useTemplate($template);

    return parent::create();
}

This fixes the issue when I edit the method in Vendor but the issue remains in my extended PageController. Everything else in the controller works but this.

Backpack 3.4 , Laravel 5.7.17, PHP 7.1.3, DB version:

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication mediums:

  • Bug Reports, Feature Requests - Github Issues (here);
  • Quick help (How do I do X) - Gitter Chatroom;
  • Long questions (I have done X and Y and it won't do Z wtf) - Stackoverflow, using the backpack-for-laravel tag;

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

--
Justin Case
The Backpack Robot

Hi @chriswright-interconnect ,

Can you please try something else: what if you delete (or rename) your resources/view/vendor/backpack/crud/fields/page_template.blade.php file? That will make PermissionManager use the new version of the file, which should have a fix for this.

Thanks, cheers!

Hi @tabacitu , thanks for the reply.

I've not actually got the page_template file.blade.php file, I have the select_page_template file.blade.php , has it been updated?

Removing the select_page_template.blade.php file hasn't fixed the issue. It seems to load the selected template fields but the previous template fields remain on page (with the top template still in the select input).

Cheers.

@chriswright-interconnect , that's what I meant - select_page_template, sorry.

Hmm... how about both of these:

  1. Make sure the file is deleted.
  2. Run composer update to get the latest PageManager.
  3. Run php artisan view:clear to make sure your Laravel install is loading the new views.

Does this fix it?

@tabacitu , tried what you suggested but still no luck getting it to work.

Could there be something wrong with my install? I've noticed that the select_page_template file gets published to 'resources\views\vendor\backpack\crud\fields\select page_template.php' but the file here is not used over vendor. It's only used when I manually move it to 'resources\views\vendor\pagemanager\fields\select_page_template.blade.php'.

I believe the file is up to date but here it is;

`

<label>{{ $field['label'] }}</label>
<select
        class="form-control"
        id="select_template"

@foreach ($field as $attribute => $value)
    @if (!is_array($value))
        {{ $attribute }}="{{ $value }}"
    @endif
@endforeach
>

@if (isset($field['allows_null']) && $field['allows_null']==true)
    <option value="">-</option>
@endif

@if (count($field['options']))
    @foreach ($field['options'] as $key => $value)
        <option value="{{ $key }}"
                @if (isset($field['value']) && $key==$field['value'])
                selected
                @endif
        >{{ $value }}</option>
        @endforeach
        @endif
        </select>

        @if (isset($field['hint']))
            <p class="help-block">{!! $field['hint'] !!}</p>
        @endif

{{-- ########################################## --}}
{{-- Extra CSS and JS for this particular field --}}
{{-- If a field type is shown multiple times on a form, the CSS and JS will only be loaded once --}}
@if ($crud->checkIfFieldIsFirstOfItsType($field, $fields))

{{-- FIELD JS - will be loaded in the after_scripts section --}}
@push('crud_fields_scripts')
    <!-- select_template crud field JS -->
    <script>
        function redirect_to_new_page_with_template_parameter() {
            var new_template = $("#select_template").val();
            var current_url = "{{ Request::url() }}";

            window.location.href = strip_last_template_parameter(current_url)+'?template='+new_template;
        }

        function strip_last_template_parameter(url) {
            // if it's a create or edit link with a template parameter
            if (url.indexOf("/create/") > -1 || url.indexOf("/edit/") > -1)
            {
                // remove the last parameter of the url
                var url_array = url.split('/');
                url_array = url_array.slice(0, -1);
                var clean_url = url_array.join('/');

                return clean_url;
            }

            return url;
        }

        jQuery(document).ready(function($) {
            $("#select_template").change(function(e) {
                var select_template_confirmation = confirm("@lang('backpack::pagemanager.change_template_confirmation')");
                if (select_template_confirmation == true) {
                    redirect_to_new_page_with_template_parameter();
                } else {
                    // txt = "You pressed Cancel!";
                }
            });

        });
    </script>
@endpush

@endif
{{-- End of Extra CSS and JS --}}
{{-- ########################################## --}}`

I am experiencing this issue too, with freshly installed pagemanager, i can't change my template to 'about us'. Even when the link changed after selecting it, it still load the default 'services' template.

Update: So i play around with the src code, and i found out that even when create?template=about_us is set in the url, the PageCrudController@create can't detect the parameter (i don't know why). Because PageCrudController@create default parameter is 'false', the parameter passed to $this->addDefaultPageFields($template) and $this->useTemplate($template) was 'false' and therefore create this issue.

After browsing around this package's issues, i found out actually many people has complained this issue.(#75 and #74)

Moreover, based on my simple analysis, I believed it has nothing to do with the old select_page_template.blade.php file.

The problem is because PageCrudController@create is not accepting any parameter
(there is no $template = request('template'); as can be found in PageCrudController@edit).

It is similar to this fix, but that fix was only applied to PageCrudController@edit.

This would fix the issue

class PageCrudController extends CrudController
{
    bla bla bla

    // -----------------------------------------------
    // Overwrites of CrudController
    // -----------------------------------------------

    // Overwrites the CrudController create() method to add template usage.
    public function create($template = false)
    {
	//added this line
        $template = request('template');
		
        $this->addDefaultPageFields($template);
        $this->useTemplate($template);

        return parent::create();
    }

   bla bla bla
}

However, after I've made my own fix, I found that actually someone has already propose a similar fix here.
The only difference is that we dont need to check whether request has $template or not because it is set to 'false' by default.

Hi @drionix , thanks for the info.

I've tried this fix, I actually started out with this one too, but I still experience the same behaviour.

The weird thing is that if I change PageCrudController@create in the Vendor directory it works as expected, yet trying to override it in the app/http dir has no effect.
Other changes made in the override file work as expected too, just not the create method.

It seems weird that the override is not working on the create method, my pagemanager.php is configured correctly. Is there something else I'm missing?

Hi @drionix , thanks for the info.

I've tried this fix, I actually started out with this one too, but I still experience the same behaviour.

The weird thing is that if I change PageCrudController@create in the Vendor directory it works as expected, yet trying to override it in the app/http dir has no effect.
Other changes made in the override file work as expected too, just not the create method.

It seems weird that the override is not working on the create method, my pagemanager.php is configured correctly. Is there something else I'm missing?

Mine is working fine.. Just to make sure, you already override the controller class in pagemanager.php? Are you sure that your custom controller extends the vendor PageCrudController properly? And have you check that the routes (or rather custom routes) points to your custom controller correctly?

For us the fix from @chriswright-interconnect worked with a slight change; pass $template to the parent:

public function create($template = false)
{
    if (request()->has('template')) {
        $template = request('template');
    }
    $this->addDefaultPageFields($template);
    $this->useTemplate($template);

    return parent::create($template);
}

Hi guys,

I'm sorry you're still having issues. But I'm still not able to replicate the same issue, on the latest version:

### PHP VERSION:
PHP 7.2.7 (cli) (built: Jun 22 2018 06:27:50) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.7, Copyright (c) 1999-2018, by Zend Technologies

### BACKPACK PACKAGES VERSION:
backpack/backupmanager             1.4.5              Admin interface for managing backups in Backpack, on Laravel 5.2+
backpack/base                      1.0.4              Laravel Backpack's base package, which offers admin authentication and a blank admin panel using AdminLTE
backpack/crud                      3.5.10             Quickly build an admin interface for your Eloquent models, using Laravel 5. Build a CMS in a matter of minutes.
backpack/generators                1.2.5              Generate files for laravel projects
backpack/logmanager                2.3.24             An interface to preview, download and delete Laravel log files.
backpack/menucrud                  1.0.13             An admin panel for menu items, using Backpack\CRUD on Laravel 5.
backpack/newscrud                  2.1.9              An admin panel for news with categories and tags, using Backpack\CRUD on Laravel 5.
backpack/pagemanager               1.1.27             Create admin panels for presentation websites on Laravel, using page templates and Backpack\CRUD.
backpack/permissionmanager         4.0.0              Users and permissions management interface for Laravel 5 using Backpack CRUD.
backpack/settings                  2.1.2              Application settings interface for Laravel 5 using Backpack CRUD.
laravel/framework                  v5.6.39            The Laravel Framework.

### MYSQL VERSION:
mysql  Ver 14.14 Distrib 5.7.22, for osx10.13 (x86_64) using  EditLine wrapper

Let's get to the bottom of this, once and for all:

  1. Can any/all of you please run php artisan backpack:base:version and paste your output, to make sure we're talking about the same versions?

  2. @chriswright-interconnect , what change in the vendor PageCrudController makes it work for you? Here's the method as I see it right now in code:

    // Overwrites the CrudController create() method to add template usage.
    public function create($template = false)
    {
        $this->addDefaultPageFields($template);
        $this->useTemplate($template);

        return parent::create();
    }
  1. Does @drionix 's fix here - #80 fix it for anyone?

Thanks guys - and sorry you're having issues.

Hi guys,

I'm sorry you're still having issues. But I'm still not able to replicate the same issue, on the latest version:

### PHP VERSION:
PHP 7.2.7 (cli) (built: Jun 22 2018 06:27:50) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.7, Copyright (c) 1999-2018, by Zend Technologies

### BACKPACK PACKAGES VERSION:
backpack/backupmanager             1.4.5              Admin interface for managing backups in Backpack, on Laravel 5.2+
backpack/base                      1.0.4              Laravel Backpack's base package, which offers admin authentication and a blank admin panel using AdminLTE
backpack/crud                      3.5.10             Quickly build an admin interface for your Eloquent models, using Laravel 5. Build a CMS in a matter of minutes.
backpack/generators                1.2.5              Generate files for laravel projects
backpack/logmanager                2.3.24             An interface to preview, download and delete Laravel log files.
backpack/menucrud                  1.0.13             An admin panel for menu items, using Backpack\CRUD on Laravel 5.
backpack/newscrud                  2.1.9              An admin panel for news with categories and tags, using Backpack\CRUD on Laravel 5.
backpack/pagemanager               1.1.27             Create admin panels for presentation websites on Laravel, using page templates and Backpack\CRUD.
backpack/permissionmanager         4.0.0              Users and permissions management interface for Laravel 5 using Backpack CRUD.
backpack/settings                  2.1.2              Application settings interface for Laravel 5 using Backpack CRUD.
laravel/framework                  v5.6.39            The Laravel Framework.

### MYSQL VERSION:
mysql  Ver 14.14 Distrib 5.7.22, for osx10.13 (x86_64) using  EditLine wrapper

Let's get to the bottom of this, once and for all:

  1. Can any/all of you please run php artisan backpack:base:version and paste your output, to make sure we're talking about the same versions?
  2. @chriswright-interconnect , what change in the vendor PageCrudController makes it work for you? Here's the method as I see it right now in code:
    // Overwrites the CrudController create() method to add template usage.
    public function create($template = false)
    {
        $this->addDefaultPageFields($template);
        $this->useTemplate($template);

        return parent::create();
    }
  1. Does @drionix 's fix here - #80 fix it for anyone?

Thanks guys - and sorry you're having issues.

Based on your screen recording, it seems like you don't really understand the issue.
To replicate the issue, try this:

  1. In the "Add page" section, try changing the template from "Services" to "About Us"
  2. Click OK on the alert
  3. Look at the generated template (should load the "about us" template, but what's happening is the loaded template is still the "services" template)

Maybe this could help you understand easier
https://streamable.com/zse7p

I just realized the fix I tried to pull previously only works if it is written in source code directly. If you fix this issue by customizing the controller (extending the parent @create method), @pestopancake solution works better because my fix didn't pass $template variable back to parent class.

The difference of my solution and @pestopancake solution is only in the return parent::create(); part as @pestopancake propose return parent::create($template);

The solution from @pestopancake works either when it is extended or written directly in source code. My previous pull request only work if written directly in source code. And I think @pestopancake solution is better so i made a new pull request after adding his solution.

Here is the pull request #81

PS: Sorry I can't include the php artisan backpack:base:version output as I am using Windows machine and that command failed because command prompt doesn't understand grep command. But I am using the latest version installed <1 month ago, fresh from the start, not upgraded from previous version.

Hi @tabacitu. Sorry for my late reply, I've been on other projects. Here's the info you've asked for;

PHP VERSION:

PHP 7.2.10 (cli) (built: Sep 13 2018 00:47:25) ( NTS MSVC15 (Visual C++ 2017) x6
4 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

BACKPACK PACKAGES VERSION:

backpack/base 1.0.4 Laravel Backpac...
backpack/crud 3.5.14 Quickly build a...
backpack/generators 1.2.6 Generate files ...
backpack/menucrud 1.0.13 An admin panel ...
backpack/pagemanager 1.1.27 Create admin pa...
laravel/framework v5.7.22 The Laravel Fra...

MYSQL VERSION:

mysql Ver 15.1 Distrib 10.3.9-MariaDB, for Win64 (AMD64), source revision ca26f
91bcaa21933147974c823852a2e1c2e2bd7

The method I'm using to fix the problem in the source is;

`public function create($template = false)
{

    $template = request('template');

    $this->addDefaultPageFields($template);
    $this->useTemplate($template);

    return parent::create();
}`

This is the same as proposed by @drionix above. However this is not working in my custom PageCrudController (which extends the base).

Thanks for looking into to this as well @drionix, the screen recording you provided above is the same issue I am having.

Hi @tabacitu. Sorry for my late reply, I've been on other projects. Here's the info you've asked for;

PHP VERSION:

PHP 7.2.10 (cli) (built: Sep 13 2018 00:47:25) ( NTS MSVC15 (Visual C++ 2017) x6
4 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

BACKPACK PACKAGES VERSION:

backpack/base 1.0.4 Laravel Backpac...
backpack/crud 3.5.14 Quickly build a...
backpack/generators 1.2.6 Generate files ...
backpack/menucrud 1.0.13 An admin panel ...
backpack/pagemanager 1.1.27 Create admin pa...
laravel/framework v5.7.22 The Laravel Fra...

MYSQL VERSION:

mysql Ver 15.1 Distrib 10.3.9-MariaDB, for Win64 (AMD64), source revision ca26f
91bcaa21933147974c823852a2e1c2e2bd7

The method I'm using to fix the problem in the source is;

`public function create($template = false)
{

    $template = request('template');

    $this->addDefaultPageFields($template);
    $this->useTemplate($template);

    return parent::create();
}`

This is the same as proposed by @drionix above. However this is not working in my custom PageCrudController (which extends the base).

Thanks for looking into to this as well @drionix, the screen recording you provided above is the same issue I am having.

Try return parent::create($template);
Should fix it

Fixed by @drionix in #81

Just merged it - works like a charm!