Skip to content

Commit ae0e44c

Browse files
committed
Pass custom widget namespace to ajax calls
1 parent 36b1e49 commit ae0e44c

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@
4242
"AsyncWidget": "Arrilot\\Widgets\\AsyncFacade"
4343
}
4444
}
45+
},
46+
"scripts": {
47+
"test": "phpunit"
4548
}
4649
}

src/Controllers/WidgetController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ public function showWidget(Request $request)
2828
: $factory->decryptWidgetParams($request->input('params', ''));
2929

3030
$decodedParams = json_decode($widgetParams, true);
31+
32+
$params = $decodedParams ?: [];
33+
array_unshift($params, $widgetName);
3134

32-
return call_user_func_array([$factory, $widgetName], $decodedParams ?: []);
35+
return call_user_func_array([$factory, 'run'], $params);
3336
}
3437

3538
/**

src/Factories/AbstractWidgetFactory.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ abstract class AbstractWidgetFactory
3535
*/
3636
public $widgetName;
3737

38+
/**
39+
* Custom widget namespace of the widget being called if is set.
40+
*
41+
* @var string|null
42+
*/
43+
public $customWidgetNamespace;
44+
3845
/**
3946
* Array of widget parameters excluding the first one (config).
4047
*
@@ -120,9 +127,12 @@ protected function instantiateWidget(array $params = [])
120127

121128
if (preg_match('#^(.*?)::(.*?)$#', $str, $m)) {
122129
$rootNamespace = $this->app->get('arrilot.widget-namespaces')->getNamespace($m[1]);
130+
$this->customWidgetNamespace = $m[1];
123131
$str = $m[2];
132+
} else {
133+
$this->customWidgetNamespace = null;
124134
}
125-
135+
126136
$this->widgetName = $this->parseFullWidgetNameFromString($str);
127137
$this->widgetFullParams = $params;
128138
$this->widgetConfig = (array) array_shift($params);
@@ -206,4 +216,16 @@ public function decryptWidgetParams($params)
206216
{
207217
return $this->app->make('encrypter')->decrypt($params);
208218
}
219+
220+
/**
221+
* Get current widget name with optional custom widget namespace.
222+
*
223+
* @return string
224+
*/
225+
public function getWidgetNameWithCustomNamespace()
226+
{
227+
return $this->customWidgetNamespace
228+
? $this->customWidgetNamespace . '::' . $this->widgetName
229+
: $this->widgetName;
230+
}
209231
}

src/Factories/JavascriptFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function constructAjaxCall($encryptParams = true)
9595
$encodedParams = json_encode($this->widgetFactory->widgetFullParams);
9696
$queryParams = [
9797
'id' => WidgetId::get(),
98-
'name' => $this->widgetFactory->widgetName,
98+
'name' => $this->widgetFactory->getWidgetNameWithCustomNamespace(),
9999
'params' => $encryptParams ? $this->widgetFactory->encryptWidgetParams($encodedParams) : $encodedParams,
100100
];
101101
if (!$encryptParams) {

0 commit comments

Comments
 (0)