Add ability to use model method to define availableColors (#3704)
Credit to @vanmil. Documented in https://github.com/octobercms/docs/pull/314
This commit is contained in:
parent
1032fd0cc1
commit
38bf854051
|
|
@ -1,6 +1,8 @@
|
||||||
<?php namespace Backend\FormWidgets;
|
<?php namespace Backend\FormWidgets;
|
||||||
|
|
||||||
use Backend\Classes\FormWidgetBase;
|
use Backend\Classes\FormWidgetBase;
|
||||||
|
use Lang;
|
||||||
|
use ApplicationException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Color picker
|
* Color picker
|
||||||
|
|
@ -78,10 +80,36 @@ class ColorPicker extends FormWidgetBase
|
||||||
{
|
{
|
||||||
$this->vars['name'] = $this->getFieldName();
|
$this->vars['name'] = $this->getFieldName();
|
||||||
$this->vars['value'] = $value = $this->getLoadValue();
|
$this->vars['value'] = $value = $this->getLoadValue();
|
||||||
$this->vars['availableColors'] = $this->availableColors;
|
$this->vars['availableColors'] = $this->getAvailableColors();
|
||||||
$this->vars['allowEmpty'] = $this->allowEmpty;
|
$this->vars['allowEmpty'] = $this->allowEmpty;
|
||||||
$this->vars['showAlpha'] = $this->showAlpha;
|
$this->vars['showAlpha'] = $this->showAlpha;
|
||||||
$this->vars['isCustomColor'] = !in_array($value, $this->availableColors);
|
$this->vars['isCustomColor'] = !in_array($value, $this->vars['availableColors']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the appropriate list of colors.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getAvailableColors()
|
||||||
|
{
|
||||||
|
if (is_array($this->availableColors)) {
|
||||||
|
return $this->availableColors;
|
||||||
|
} elseif (is_string($this->availableColors) && !empty($this->availableColors)) {
|
||||||
|
if ($this->model->methodExists($this->availableColors)) {
|
||||||
|
return $this->availableColors = $this->model->{$this->availableColors}(
|
||||||
|
$this->formField->fieldName,
|
||||||
|
$this->formField->value,
|
||||||
|
$this->formField->config
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw new ApplicationException(Lang::get('backend::lang.field.colors_method_not_exists', [
|
||||||
|
'model' => get_class($this->model),
|
||||||
|
'method' => $this->availableColors,
|
||||||
|
'field' => $this->formField->fieldName
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,8 @@ return [
|
||||||
'field' => [
|
'field' => [
|
||||||
'invalid_type' => 'Invalid field type used :type.',
|
'invalid_type' => 'Invalid field type used :type.',
|
||||||
'options_method_invalid_model' => "The attribute ':field' does not resolve to a valid model. Try specifying the options method for model class :model explicitly.",
|
'options_method_invalid_model' => "The attribute ':field' does not resolve to a valid model. Try specifying the options method for model class :model explicitly.",
|
||||||
'options_method_not_exists' => "The model class :model must define a method :method() returning options for the ':field' form field."
|
'options_method_not_exists' => "The model class :model must define a method :method() returning options for the ':field' form field.",
|
||||||
|
'colors_method_not_exists' => "The model class :model must define a method :method() returning html color HEX codes for the ':field' form field."
|
||||||
],
|
],
|
||||||
'widget' => [
|
'widget' => [
|
||||||
'not_registered' => "A widget class name ':name' has not been registered",
|
'not_registered' => "A widget class name ':name' has not been registered",
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ return [
|
||||||
'invalid_type' => 'Ongeldig type veld: :type.',
|
'invalid_type' => 'Ongeldig type veld: :type.',
|
||||||
'options_method_invalid_model' => "Het attribuut ':field' levert geen geldig model op. Probeer de opties methode expliciet te specifieren voor modelklasse :model.",
|
'options_method_invalid_model' => "Het attribuut ':field' levert geen geldig model op. Probeer de opties methode expliciet te specifieren voor modelklasse :model.",
|
||||||
'options_method_not_exists' => 'De modelklasse :model moet de methode :method() definiëren met daarin opties voor het veld ":field".',
|
'options_method_not_exists' => 'De modelklasse :model moet de methode :method() definiëren met daarin opties voor het veld ":field".',
|
||||||
|
'colors_method_not_exists' => 'De modelklasse :model moet de methode :method() definiëren met daarin html HEX kleurcodes voor het veld ":field".',
|
||||||
],
|
],
|
||||||
'widget' => [
|
'widget' => [
|
||||||
'not_registered' => "Een widget met klassenaam ':name' is niet geregistreerd",
|
'not_registered' => "Een widget met klassenaam ':name' is niet geregistreerd",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue