Skip to content

Commit

Permalink
Fix page or link v6 (#78)
Browse files Browse the repository at this point in the history
* fix page or link

* fix disabling js error

* only support Backpack v6

---------

Co-authored-by: Cristian Tăbăcitu <[email protected]>
  • Loading branch information
pxpm and tabacitu authored Jul 1, 2023
1 parent f415585 commit a99feee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"backpack/crud": "^5.0",
"backpack/pagemanager": "^3.0|^2.0"
"backpack/crud": "^6.0",
"backpack/pagemanager": "^3.2"
},
"require-dev": {
"phpunit/phpunit" : "^9.0||^7.0",
Expand Down
2 changes: 1 addition & 1 deletion src/app/Http/Controllers/Admin/MenuItemCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function setup()
'model' => "\Backpack\MenuCRUD\app\Models\MenuItem",
]);
$this->crud->addField([
'name' => ['type', 'link', 'page_id'],
'name' => 'type,link,page_id',
'label' => 'Type',
'type' => 'page_or_link',
'page_model' => '\Backpack\PageManager\app\Models\Page',
Expand Down
58 changes: 33 additions & 25 deletions src/resources/views/fields/page_or_link.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

<?php
$field['allows_null'] = $field['allows_null'] ?? false;
[$type, $link, $page_id] = explode(',', $field['name']);
$field['configurationNames'] = [];
$field['name']['type'] = $field['name']['type'] ?? $field['name'][0] ?? 'type';
$field['name']['link'] = $field['name']['link'] ?? $field['name'][1] ?? 'link';
$field['name']['page_id'] = $field['name']['page_id'] ?? $field['name'][2] ?? 'page_id';
$field['configurationNames']['type'] = $type ?? 'type';
$field['configurationNames']['link'] = $link ?? 'link';
$field['configurationNames']['page_id'] = $page_id ?? 'page_id';
$field['options']['page_link'] = $field['options']['page_link'] ?? trans('backpack::crud.page_link');
$field['options']['internal_link'] = $field['options']['internal_link'] ?? trans('backpack::crud.internal_link');
$field['options']['external_link'] = $field['options']['external_link'] ?? trans('backpack::crud.external_link');
Expand All @@ -21,14 +23,14 @@

<div class="row" data-init-function="bpFieldInitPageOrLinkElement">
{{-- hidden placeholders for content --}}
<input type="hidden" value="{{ $entry->{$field['name']['page_id']} ?? '' }}" name="{{ $field['name']['page_id'] }}" />
<input type="hidden" value="{{ $entry->{$field['name']['link']} ?? '' }}" name="{{ $field['name']['link'] }}" />
<input type="hidden" value="{{ $entry->{$field['configurationNames']['page_id']} ?? '' }}" name="{{ $field['configurationNames']['page_id'] }}" />
<input type="hidden" value="{{ $entry->{$field['configurationNames']['link']} ?? '' }}" name="{{ $field['configurationNames']['link'] }}" />

<div class="col-sm-3">
{{-- type select --}}
<select
data-identifier="page_or_link_select"
name="{!! $field['name']['type'] !!}"
name="{!! $field['configurationNames']['type'] !!}"
@include('crud::fields.inc.attributes')
>

Expand All @@ -38,7 +40,7 @@

@foreach ($field['options'] as $key => $value)
<option value="{{ $key }}"
@if (isset($entry) && $key === $entry->{$field['name']['type']})
@if (isset($entry) && $key === $entry->{$field['configurationNames']['type']})
selected
@endif
>{{ $value }}</option>
Expand All @@ -47,15 +49,15 @@
</div>
<div class="col-sm-9">
{{-- page slug input --}}
<div class="page_or_link_value page_link {{ (isset($entry) && $entry->{$field['name']['type']} === 'page_link') || (isset($entry) && !$entry->{$field['name']['type']} && !$field['allows_null']) || (!isset($entry) && !$field['allows_null']) ? '' : 'd-none' }}">
<div class="page_or_link_value page_link {{ (isset($entry) && $entry->{$field['configurationNames']['type']} === 'page_link') || (isset($entry) && !$entry->{$field['configurationNames']['type']} && !$field['allows_null']) || (!isset($entry) && !$field['allows_null']) ? '' : 'd-none' }}">
<select
class="form-control"
for="{{ $field['name']['page_id'] }}"
for="{{ $field['configurationNames']['page_id'] }}"
required
>
@foreach ($field['pages'] as $page)
<option value="{{ $page->id }}"
@if (isset($entry) && $page->id === $entry->{$field['name']['page_id']})
@if (isset($entry) && $page->id === $entry->{$field['configurationNames']['page_id']})
selected
@endif
>{{ $page->name }}</option>
Expand All @@ -64,40 +66,46 @@ class="form-control"
</div>

{{-- internal link input --}}
<div class="page_or_link_value internal_link {{ isset($entry) && $entry->{$field['name']['type']} === 'internal_link' ? '' : 'd-none' }}">
<div class="page_or_link_value internal_link {{ isset($entry) && $entry->{$field['configurationNames']['type']} === 'internal_link' ? '' : 'd-none' }}">
<input
type="text"
class="form-control"
placeholder="{{ trans('backpack::crud.internal_link_placeholder', ['url', url(config('backpack.base.route_prefix').'/page')]) }}"
for="{{ $field['name']['link'] }}"
for="{{ $field['configurationNames']['link'] }}"
required
@if(isset($entry))
@if ($entry->{$field['configurationNames']['type']} !== 'internal_link' && $entry->{$field['configurationNames']['type']} !== 'page_link')
disabled="disabled"
@endif

@if (isset($entry) && $entry->{$field['name']['type']} !== 'internal_link')
@if ($entry->{$field['configurationNames']['type']} === 'internal_link' && $entry->{$field['configurationNames']['link']})
value="{{ $entry->{$field['configurationNames']['link']} }}"
@endif
@else
disabled="disabled"
@endif

@if (isset($entry) && $entry->{$field['name']['type']} === 'internal_link' && $entry->{$field['name']['link']})
value="{{ $entry->{$field['name']['link']} }}"
@endif
>
</div>

{{-- external link input --}}
<div class="page_or_link_value external_link {{ isset($entry) && $entry->{$field['name']['type']} === 'external_link' ? '' : 'd-none' }}">
<div class="page_or_link_value external_link {{ isset($entry) && $entry->{$field['configurationNames']['type']} === 'external_link' ? '' : 'd-none' }}">
<input
type="url"
class="form-control"
placeholder="{{ trans('backpack::crud.page_link_placeholder') }}"
for="{{ $field['name']['link'] }}"
for="{{ $field['configurationNames']['link'] }}"
required
@if(isset($entry))
@if ($entry->{$field['configurationNames']['type']} !== 'external_link' && $entry->{$field['configurationNames']['type']} !== 'page_link')
disabled="disabled"
@endif

@if (isset($entry) && $entry->{$field['name']['type']} !== 'external_link')
@if ($entry->{$field['configurationNames']['type']} === 'external_link' && $entry->{$field['configurationNames']['link']})
value="{{ $entry->{$field['configurationNames']['link']} }}"
@endif
@else
disabled="disabled"
@endif

@if (isset($entry) && $entry->{$field['name']['type']} === 'external_link' && $entry->{$field['name']['link']})
value="{{ $entry->{$field['name']['link']} }}"
@endif
>
</div>
</div>
Expand Down

0 comments on commit a99feee

Please sign in to comment.