Laravel-Backpack/PageManager

Unable to switch page template on page creation

Dakaa opened this issue · 3 comments

Dakaa commented

Bug report

What I did: Trying to create new page with a different template.

What I expected to happen: Different form inputs after switching template.

What happened: After the page refresh, it still loads the default template.

What I've already tried to fix it: Clueless.

Backpack, Laravel, PHP, DB version: 3.4, 7.2, MySQL 5.7

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

Dakaa commented

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

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

    return parent::create();
}`

Thanks for your fix @Dakaa .

However, I think you're either using an old version of the select_page_template field, in which case I recommend you upgrade to the latest:

<!-- Select template field. Used in Backpack/PageManager to redirect to a form with different fields if the template changes. A fork of the select_from_array field with an extra ID and an extra javascript file. -->
  <div @include('crud::inc.field_wrapper_attributes') >

    <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
  </div>


{{-- ########################################## --}}
{{-- 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 --}}
{{-- ########################################## --}}

Or you're using Backpack/Demo as a starting point for your project (which had this bug), which is not recommended. Take note it's always better to start from scratch. Unless you know what you're doing, in which case go ahead :-)

Cheers!