mirror of
https://github.com/flarum/core.git
synced 2025-07-23 01:31:40 +02:00
Allow custom variables to be set on the client app
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
<?php namespace Flarum\Admin\Actions;
|
||||
|
||||
use Flarum\Support\ClientAction as BaseClientAction;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Flarum\Core\Groups\Permission;
|
||||
|
||||
class ClientAction extends BaseClientAction
|
||||
{
|
||||
@@ -20,4 +22,18 @@ class ClientAction extends BaseClientAction
|
||||
protected $translationKeys = [
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function render(Request $request, array $routeParams = [])
|
||||
{
|
||||
$view = parent::render($request, $routeParams);
|
||||
|
||||
$view->setVariable('config', $this->settings->all());
|
||||
$view->setVariable('locales', app('flarum.localeManager')->getLocales());
|
||||
$view->setVariable('permissions', Permission::map());
|
||||
|
||||
return $view;
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,6 @@
|
||||
<?php namespace Flarum\Api\Serializers;
|
||||
|
||||
use Flarum\Core;
|
||||
use Flarum\Core\Groups\Permission;
|
||||
|
||||
class ForumSerializer extends Serializer
|
||||
{
|
||||
@@ -33,9 +32,6 @@ class ForumSerializer extends Serializer
|
||||
];
|
||||
|
||||
if ($this->actor->isAdmin()) {
|
||||
$attributes['config'] = app('Flarum\Core\Settings\SettingsRepository')->all();
|
||||
$attributes['availableLocales'] = app('flarum.localeManager')->getLocales();
|
||||
$attributes['permissions'] = Permission::map();
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
|
@@ -26,13 +26,6 @@ class ClientView implements Renderable
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* An API response that should be preloaded into the page.
|
||||
*
|
||||
* @var null|array|object
|
||||
*/
|
||||
protected $document;
|
||||
|
||||
/**
|
||||
* The SEO content of the page, displayed in <noscript> tags.
|
||||
*
|
||||
@@ -47,6 +40,20 @@ class ClientView implements Renderable
|
||||
*/
|
||||
protected $layout;
|
||||
|
||||
/**
|
||||
* An API response that should be preloaded into the page.
|
||||
*
|
||||
* @var null|array|object
|
||||
*/
|
||||
protected $document;
|
||||
|
||||
/**
|
||||
* Other variables to preload into the page.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $variables = [];
|
||||
|
||||
/**
|
||||
* An array of JS modules to import before booting the app.
|
||||
*
|
||||
@@ -122,17 +129,6 @@ class ClientView implements Renderable
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an API response to be preloaded into the page. This should be a
|
||||
* JSON-API document.
|
||||
*
|
||||
* @param null|array|object $document
|
||||
*/
|
||||
public function setDocument($document)
|
||||
{
|
||||
$this->document = $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the SEO content of the page, to be displayed in <noscript> tags.
|
||||
*
|
||||
@@ -173,6 +169,28 @@ class ClientView implements Renderable
|
||||
$this->footStrings[] = $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an API response to be preloaded into the page. This should be a
|
||||
* JSON-API document.
|
||||
*
|
||||
* @param null|array|object $document
|
||||
*/
|
||||
public function setDocument($document)
|
||||
{
|
||||
$this->document = $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a variable to be preloaded into the app.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setVariable($name, $value)
|
||||
{
|
||||
$this->variables[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a JavaScript module to be imported before the app is booted.
|
||||
*
|
||||
@@ -210,10 +228,16 @@ class ClientView implements Renderable
|
||||
$data = array_merge($data, $this->getDataFromDocument($user));
|
||||
}
|
||||
|
||||
$view->data = $data;
|
||||
$view->session = $this->getSession();
|
||||
$view->app = [
|
||||
'preload' => [
|
||||
'data' => $data,
|
||||
'session' => $this->getSession(),
|
||||
'document' => $this->document
|
||||
]
|
||||
] + $this->variables;
|
||||
$view->bootstrappers = $this->bootstrappers;
|
||||
|
||||
$view->title = ($this->title ? $this->title . ' - ' : '') . $forum->data->attributes->title;
|
||||
$view->document = $this->document;
|
||||
$view->forum = $forum->data;
|
||||
$view->layout = app('view')->file($this->layout, ['forum' => $forum->data]);
|
||||
$view->content = $this->content;
|
||||
@@ -223,7 +247,6 @@ class ClientView implements Renderable
|
||||
|
||||
$view->head = implode("\n", $this->headStrings);
|
||||
$view->foot = implode("\n", $this->footStrings);
|
||||
$view->bootstrappers = $this->bootstrappers;
|
||||
|
||||
return $view->render();
|
||||
}
|
||||
|
Reference in New Issue
Block a user