diff --git a/core/includes/form.inc b/core/includes/form.inc index ae60c90..9d3d506 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -685,6 +685,15 @@ function form_type_select_value($element, $input = FALSE) { } /** + * Determines the value for a textfield form element. + * + * @deprecated Use \Drupal\Core\Render\Element\Textfield::valueCallback(). + */ +function form_type_textfield_value(&$element, $input, &$form_state) { + return Element\Textfield::valueCallback($element, $input, $form_state) +} + +/** * Determines the value for form's token value. * * @param $element diff --git a/core/lib/Drupal/Core/Render/Element/FormElement.php b/core/lib/Drupal/Core/Render/Element/FormElement.php index 82a4bd2..210af9a 100644 --- a/core/lib/Drupal/Core/Render/Element/FormElement.php +++ b/core/lib/Drupal/Core/Render/Element/FormElement.php @@ -16,21 +16,10 @@ * * @ingroup plugin_api */ -abstract class FormElement extends RenderElement { +abstract class FormElement extends RenderElement implements FormElementInterface { /** - * Determines how user input is mapped to an element's #value property. - * - * @param array $element - * An associative array containing the properties of the element. - * @param mixed $input - * The incoming input to populate the form element. If this is FALSE, - * the element's default value should be returned. - * @param array $form_state - * An associative array containing the current state of the form. - * - * @return mixed - * The value to assign to the element. + * {@inheritdoc} */ public static function valueCallback(&$element, $input, &$form_state) { return NULL; diff --git a/core/lib/Drupal/Core/Render/Element/FormElementInterface.php b/core/lib/Drupal/Core/Render/Element/FormElementInterface.php new file mode 100644 index 0000000..992d94b --- /dev/null +++ b/core/lib/Drupal/Core/Render/Element/FormElementInterface.php @@ -0,0 +1,31 @@ + $value) { if (isset($names[$name])) { $form['palette'][$name] = array( - '#type' => 'textfield', + '#type' => 'color_palette', '#title' => String::checkPlain($names[$name]), - '#value_callback' => 'color_palette_color_value', '#default_value' => $value, - '#size' => 8, - '#attributes' => array('dir' => 'ltr'), ); } } @@ -281,33 +279,10 @@ function template_preprocess_color_scheme_form(&$variables) { /** * Determines the value for a palette color field. * - * @param array $element - * The form element whose value is being populated. - * @param string|bool $input - * The incoming input to populate the form element. If this is FALSE, - * the element's default value should be returned. - * @param array $form_state - * A keyed array containing the current state of the form. - * - * @return string - * The data that will appear in the $form_state['values'] collection for this - * element. Return nothing to use the default. + * @deprecated Use \Drupal\color\Element\ColorPalette::valueCallback(). */ function color_palette_color_value($element, $input = FALSE, $form_state = array()) { - // If we suspect a possible cross-site request forgery attack, only accept - // hexadecimal CSS color strings from user input, to avoid problems when this - // value is used in the JavaScript preview. - if ($input !== FALSE) { - // Start with the provided value for this textfield, and validate that if - // necessary, falling back on the default value. - $value = form_type_textfield_value($element, $input, $form_state); - if (!$value || !isset($form_state['complete form']['#token']) || color_valid_hexadecimal_string($value) || \Drupal::csrfToken()->validate($form_state['values']['form_token'], $form_state['complete form']['#token'])) { - return $value; - } - else { - return $element['#default_value']; - } - } + return ColorPalette::valueCallback($element, $input, $form_state); } /** diff --git a/core/modules/color/src/Element/ColorPalette.php b/core/modules/color/src/Element/ColorPalette.php new file mode 100644 index 0000000..d147412 --- /dev/null +++ b/core/modules/color/src/Element/ColorPalette.php @@ -0,0 +1,49 @@ + 8, + '#attributes' => array('dir' => 'ltr'), + ) + parent::getInfo(); + } + + /** + * {@inheritdoc} + */ + public static function valueCallback(&$element, $input, &$form_state) { + // If we suspect a possible cross-site request forgery attack, only accept + // hexadecimal CSS color strings from user input, to avoid problems when this + // value is used in the JavaScript preview. + if ($input !== FALSE) { + // Start with the provided value for this textfield, and validate that if + // necessary, falling back on the default value. + $value = parent::valueCallback($element, $input, $form_state); + if (!$value || !isset($form_state['complete form']['#token']) || color_valid_hexadecimal_string($value) || \Drupal::csrfToken()->validate($form_state['values']['form_token'], $form_state['complete form']['#token'])) { + return $value; + } + else { + return $element['#default_value']; + } + } + } + +}