Streamline the process of filling a widget with its config options
This commit is contained in:
parent
6c441b92d4
commit
932a40a025
|
|
@ -80,6 +80,26 @@ abstract class WidgetBase
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfers config values stored inside the $config property directly
|
||||
* on to the root object properties. If no properties are defined
|
||||
* all config will be transferred if finds a match.
|
||||
* @param array $properties
|
||||
* @return void
|
||||
*/
|
||||
protected function fillFromConfig($properties = null)
|
||||
{
|
||||
if ($properties === null) {
|
||||
$properties = array_keys((array) $this->config);
|
||||
}
|
||||
|
||||
foreach ($properties as $property) {
|
||||
if (property_exists($this, $property)) {
|
||||
$this->{$property} = $this->getConfig($property, $this->{$property});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the widget, called by the constructor and free from its parameters.
|
||||
* @return void
|
||||
|
|
|
|||
|
|
@ -40,13 +40,14 @@ class ReportContainer extends WidgetBase
|
|||
* @var array A list of default widgets to load.
|
||||
* This structure could be defined in the widget configuration file (for example config_report_container.yaml).
|
||||
* Example YAML structure:
|
||||
*
|
||||
* defaultWidgets:
|
||||
* trafficOverview:
|
||||
* class: RainLab\GoogleAnalytics\ReportWidgets\TrafficOverview
|
||||
* sortOrder: 1
|
||||
* configuration:
|
||||
* title: 'Traffic overview'
|
||||
* ocWidgetWidth: 10
|
||||
* trafficOverview:
|
||||
* class: RainLab\GoogleAnalytics\ReportWidgets\TrafficOverview
|
||||
* sortOrder: 1
|
||||
* configuration:
|
||||
* title: 'Traffic overview'
|
||||
* ocWidgetWidth: 10
|
||||
*/
|
||||
public $defaultWidgets = [];
|
||||
|
||||
|
|
@ -74,9 +75,19 @@ class ReportContainer extends WidgetBase
|
|||
*/
|
||||
public function __construct($controller)
|
||||
{
|
||||
parent::__construct($controller, []);
|
||||
$configFile = 'config_' . snake_case($this->alias) . '.yaml';
|
||||
$path = $controller->getConfigPath($configFile);
|
||||
if (File::isFile($path)) {
|
||||
$config = $this->makeConfig($configFile);
|
||||
}
|
||||
else {
|
||||
$config = [];
|
||||
}
|
||||
|
||||
parent::__construct($controller, $config);
|
||||
|
||||
$this->bindToController();
|
||||
$this->applyConfigToSelf();
|
||||
$this->fillFromConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -90,26 +101,6 @@ class ReportContainer extends WidgetBase
|
|||
parent::bindToController();
|
||||
}
|
||||
|
||||
/**
|
||||
* Most widgets store their configuration inside the $config class
|
||||
* property, this container stores it against the root object.
|
||||
*/
|
||||
protected function applyConfigToSelf()
|
||||
{
|
||||
$configFile = 'config_' . snake_case($this->alias) . '.yaml';
|
||||
|
||||
$path = $this->controller->getConfigPath($configFile);
|
||||
if (!File::isFile($path)) return;
|
||||
|
||||
$config = $this->makeConfig($configFile);
|
||||
|
||||
foreach ($config as $field => $value) {
|
||||
if (property_exists($this, $field)) {
|
||||
$this->$field = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders this widget along with its collection of report widgets.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue