diff --git a/modules/backend/formwidgets/ColorPicker.php b/modules/backend/formwidgets/ColorPicker.php index 10ac5820e..e2f8485f9 100644 --- a/modules/backend/formwidgets/ColorPicker.php +++ b/modules/backend/formwidgets/ColorPicker.php @@ -1,6 +1,8 @@ vars['name'] = $this->getFieldName(); $this->vars['value'] = $value = $this->getLoadValue(); - $this->vars['availableColors'] = $this->availableColors; + $this->vars['availableColors'] = $this->getAvailableColors(); $this->vars['allowEmpty'] = $this->allowEmpty; $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 + ])); + } + } } /** diff --git a/modules/backend/lang/en/lang.php b/modules/backend/lang/en/lang.php index d867786a1..0dd5ae42f 100644 --- a/modules/backend/lang/en/lang.php +++ b/modules/backend/lang/en/lang.php @@ -8,7 +8,8 @@ return [ 'field' => [ '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_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' => [ 'not_registered' => "A widget class name ':name' has not been registered", diff --git a/modules/backend/lang/nl/lang.php b/modules/backend/lang/nl/lang.php index df4102a8e..f796692ec 100644 --- a/modules/backend/lang/nl/lang.php +++ b/modules/backend/lang/nl/lang.php @@ -8,6 +8,7 @@ return [ '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_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' => [ 'not_registered' => "Een widget met klassenaam ':name' is niet geregistreerd",