Skip to content

Commit a3b0dd0

Browse files
authored
Merge pull request #4520 from Laravel-Backpack/load-viewNamespaces-into-crud-instead-of-config
2 parents b820a2c + e49f9d4 commit a3b0dd0

File tree

8 files changed

+85
-12
lines changed

8 files changed

+85
-12
lines changed

src/app/Library/CrudPanel/CrudButton.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,9 @@ public function getHtml($entry = null)
273273
private function getViewPathsWithFallbacks()
274274
{
275275
$type = $this->name;
276-
277276
$paths = array_map(function ($item) use ($type) {
278277
return $item.'.'.$type;
279-
}, config('backpack.crud.view_namespaces.buttons'));
278+
}, $this->crud()->getViewNamespacesFor('buttons'));
280279

281280
return array_merge([$this->content], $paths);
282281
}

src/app/Library/CrudPanel/CrudFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function getViewWithNamespace()
146146
public function getNamespacedViewWithFallbacks()
147147
{
148148
$type = $this->type;
149-
$namespaces = config('backpack.crud.view_namespaces.filters');
149+
$namespaces = $this->crud()->getViewNamespacesFor('filters');
150150

151151
if ($this->viewNamespace != 'crud::filters') {
152152
$namespaces = array_merge([$this->viewNamespace], $namespaces);

src/app/Library/CrudPanel/CrudPanel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Backpack\CRUD\app\Library\CrudPanel\Traits\FakeFields;
1515
use Backpack\CRUD\app\Library\CrudPanel\Traits\Fields;
1616
use Backpack\CRUD\app\Library\CrudPanel\Traits\Filters;
17+
use Backpack\CRUD\app\Library\CrudPanel\Traits\HasViewNamespaces;
1718
use Backpack\CRUD\app\Library\CrudPanel\Traits\HeadingsAndTitles;
1819
use Backpack\CRUD\app\Library\CrudPanel\Traits\Input;
1920
use Backpack\CRUD\app\Library\CrudPanel\Traits\Macroable;
@@ -38,7 +39,7 @@
3839
class CrudPanel
3940
{
4041
// load all the default CrudPanel features
41-
use Create, Read, Search, Update, Delete, Input, Errors, Reorder, Access, Columns, Fields, Query, Buttons, AutoSet, FakeFields, FakeColumns, AutoFocus, Filters, Tabs, Views, Validation, HeadingsAndTitles, Operations, SaveActions, Settings, Relationships;
42+
use Create, Read, Search, Update, Delete, Input, Errors, Reorder, Access, Columns, Fields, Query, Buttons, AutoSet, FakeFields, FakeColumns, AutoFocus, Filters, Tabs, Views, Validation, HeadingsAndTitles, Operations, SaveActions, Settings, Relationships, HasViewNamespaces;
4243
// allow developers to add their own closures to this object
4344
use Macroable;
4445

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4+
5+
trait HasViewNamespaces
6+
{
7+
private $viewNamespaces = [];
8+
9+
/**
10+
* Return all the view namespaces including the ones stored in the laravel config files.
11+
*
12+
* @param string $domain (eg. fields, filters, buttons)
13+
* @return array
14+
*/
15+
public function getViewNamespacesFor(string $domain)
16+
{
17+
$viewNamespacesFromConfig = $this->getViewNamespacesFromConfigFor($domain);
18+
19+
return array_unique(array_merge($viewNamespacesFromConfig, $this->viewNamespaces[$domain] ?? []));
20+
}
21+
22+
/**
23+
* Adds multiple namespaces to a given domain.
24+
*
25+
* @param string $domain (eg. fields, filters, buttons)
26+
* @param array $viewNamespaces
27+
* @return void
28+
*/
29+
public function addViewNamespacesFor(string $domain, array $viewNamespaces)
30+
{
31+
foreach ((array) $viewNamespaces as $viewNamespace) {
32+
$this->addViewNamespaceFor($domain, $viewNamespace);
33+
}
34+
}
35+
36+
/**
37+
* Add a new view namespace for a given domain.
38+
*
39+
* @param string $domain (eg. fields, filters, buttons)
40+
* @param string $viewNamespace
41+
* @return void
42+
*/
43+
public function addViewNamespaceFor(string $domain, string $viewNamespace)
44+
{
45+
$this->viewNamespaces[$domain][] = $viewNamespace;
46+
}
47+
48+
/**
49+
* Return the array of view namespaces for backpack components from the Laravel config files.
50+
* It uses the default `backpack.crud.view_namespaces` key or a custom provided key.
51+
*
52+
* @param string $domain
53+
* @param mixed $customConfigKey
54+
* @return array
55+
*/
56+
private function getViewNamespacesFromConfigFor(string $domain, mixed $customConfigKey = null)
57+
{
58+
return config($customConfigKey ?? 'backpack.crud.view_namespaces.'.$domain) ?? [];
59+
}
60+
61+
/**
62+
* Return all the view namespaces using a developer provided config key.
63+
* Allow developer to use view namespaces from other config keys.
64+
*
65+
* @param string $domain (eg. fields, filters, buttons)
66+
* @param string $viewNamespacesFromConfigKey
67+
* @return array
68+
*/
69+
public function getViewNamespacesWithFallbackFor(string $domain, string $viewNamespacesFromConfigKey)
70+
{
71+
$viewNamespacesFromConfig = $this->getViewNamespacesFromConfigFor($domain, $viewNamespacesFromConfigKey);
72+
73+
return array_unique(array_merge($viewNamespacesFromConfig, $this->getViewNamespacesFor($domain)));
74+
}
75+
}

src/app/Library/CrudPanel/Traits/Search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ private function getCellViewName($column)
313313
// including the configured view_namespaces
314314
$columnPaths = array_map(function ($item) use ($column) {
315315
return $item.'.'.$column['type'];
316-
}, config('backpack.crud.view_namespaces.columns'));
316+
}, $this->getViewNamespacesFor('columns'));
317317

318318
// but always fall back to the stock 'text' column
319319
// if a view doesn't exist

src/app/Library/CrudPanel/Traits/Views.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,17 @@ public function getUpdateContentClass()
290290
* @param bool|string $viewNamespace Optional override, to use this namespace instead of the viewstack.
291291
* @return string
292292
*/
293-
public static function getFirstFieldView($viewPath, $viewNamespace = false)
293+
public function getFirstFieldView($viewPath, $viewNamespace = false)
294294
{
295295
// if a definite namespace was given, use that one
296296
if ($viewNamespace) {
297297
return $viewNamespace.'.'.$viewPath;
298298
}
299-
300299
// otherwise, loop through all the possible view namespaces
301300
// until you find a view that exists
302301
$paths = array_map(function ($item) use ($viewPath) {
303302
return $item.'.'.$viewPath;
304-
}, config('backpack.crud.view_namespaces.fields'));
303+
}, $this->getViewNamespacesFor('fields'));
305304

306305
foreach ($paths as $path) {
307306
if (view()->exists($path)) {

src/app/Library/Widget.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,10 @@ public function getFinalViewPath()
133133
return $path;
134134
}
135135
}
136-
137136
$type = $this->type;
138137
$paths = array_map(function ($item) use ($type) {
139138
return $item.'.'.$type;
140-
}, config('backpack.base.component_view_namespaces.widgets'));
139+
}, app('crud')->getViewNamespacesWithFallbackFor('widgets', 'backpack.base.component_view_namespaces.widgets'));
141140

142141
foreach ($paths as $path) {
143142
if (view()->exists($path)) {
@@ -146,7 +145,7 @@ public function getFinalViewPath()
146145
}
147146
// if no view exists, in any of the directories above... no bueno
148147
if (! backpack_pro()) {
149-
throw new BackpackProRequiredException('Cannot find the widget view: '.$viewPath.'. Please check for typos.'.(backpack_pro() ? '' : ' If you are trying to use a PRO widget, please first purchase and install the backpack/pro addon from backpackforlaravel.com'), 1);
148+
throw new BackpackProRequiredException('Cannot find the widget view: '.$this->type.'. Please check for typos.'.(backpack_pro() ? '' : ' If you are trying to use a PRO widget, please first purchase and install the backpack/pro addon from backpackforlaravel.com'), 1);
150149
}
151150
abort(500, 'Cannot find the view for «'.$this->type.'» widget type. Please check for typos.');
152151
}

src/resources/views/crud/show.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
// including the configured view_namespaces
6262
$columnPaths = array_map(function($item) use ($column) {
6363
return $item.'.'.$column['type'];
64-
}, config('backpack.crud.view_namespaces.columns'));
64+
}, $crud->getViewNamespacesFor('columns'));
6565
6666
// but always fall back to the stock 'text' column
6767
// if a view doesn't exist

0 commit comments

Comments
 (0)