public static function ImageEffectsColor::processImageEffectsColor in Image Effects 8
Same name and namespace in other branches
- 8.3 src/Element/ImageEffectsColor.php \Drupal\image_effects\Element\ImageEffectsColor::processImageEffectsColor()
- 8.2 src/Element/ImageEffectsColor.php \Drupal\image_effects\Element\ImageEffectsColor::processImageEffectsColor()
Processes a 'image_effects_color' form element.
Parameters
array $element: The form element to process. Properties used: '#allow_null' - if set to TRUE, a checkbox is displayed to set the color as a full transparency, In this case, color hex and opacity are hidden, and the value returned is NULL. '#allow_opacity' - if set to TRUE, a textfield is displayed to capture the 'opacity' value, as a percentage.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
array $complete_form: The complete form structure.
Return value
array The processed element.
File
- src/
Element/ ImageEffectsColor.php, line 85
Class
- ImageEffectsColor
- Implements a form element to enable capturing color information.
Namespace
Drupal\image_effects\ElementCode
public static function processImageEffectsColor(array &$element, FormStateInterface $form_state, array &$complete_form) {
// Make sure element properties are set.
$element += [
'#allow_null' => FALSE,
'#allow_opacity' => FALSE,
'#description' => NULL,
'#states' => NULL,
'#title' => t('Color'),
'#checkbox_title' => t('Transparent'),
];
// In case default value is transparent, set hex and opacity to default
// values (white, fully opaque) so that if transparency is unchecked,
// we have a starting value.
$transparent = empty($element['#default_value']) ? TRUE : FALSE;
$hex = $transparent ? '#FFFFFF' : Unicode::substr($element['#default_value'], 0, 7);
$opacity = $transparent ? 100 : ColorUtility::rgbaToOpacity($element['#default_value']);
$colorPlugin = \Drupal::service('plugin.manager.image_effects.color_selector')
->getPlugin();
if ($element['#allow_null'] || $element['#allow_opacity']) {
// More sub-fields are needed to define the color, wrap them in a
// container fieldset.
$element['container'] = [
'#type' => 'fieldset',
'#description' => $element['#description'],
'#title' => $element['#title'],
'#states' => $element['#states'],
];
// Checkbox for transparency.
if ($element['#allow_null']) {
$element['container']['transparent'] = [
'#type' => 'checkbox',
'#title' => $element['#checkbox_title'],
'#default_value' => $transparent,
];
}
// Color field.
$element['container']['hex'] = $colorPlugin
->selectionElement([
'#default_value' => $hex,
]);
// States management for color field.
$element['container']['hex']['#states'] = [
'visible' => [
':input[name="' . $element['#name'] . '[container][transparent]"]' => [
'checked' => FALSE,
],
],
];
// Textfield for opacity.
if ($element['#allow_opacity']) {
$element['container']['opacity'] = [
'#type' => 'number',
'#title' => t('Opacity'),
'#default_value' => $opacity,
'#maxlength' => 3,
'#size' => 2,
'#field_suffix' => '%',
'#min' => 0,
'#max' => 100,
'#states' => [
'visible' => [
':input[name="' . $element['#name'] . '[container][transparent]"]' => [
'checked' => FALSE,
],
],
],
];
}
}
else {
// No transparency or opacity, straight color field.
$options = $element;
$options['#default_value'] = $hex;
$element['hex'] = $colorPlugin
->selectionElement($options);
}
unset($element['#description'], $element['#title']);
return $element;
}