You are here

public static function ImageEffectsPxPerc::processImageEffectsPxPerc in Image Effects 8

Same name and namespace in other branches
  1. 8.3 src/Element/ImageEffectsPxPerc.php \Drupal\image_effects\Element\ImageEffectsPxPerc::processImageEffectsPxPerc()
  2. 8.2 src/Element/ImageEffectsPxPerc.php \Drupal\image_effects\Element\ImageEffectsPxPerc::processImageEffectsPxPerc()

Processes a 'image_effects_px_perc' form element.

Parameters

array $element: The form element to process.

\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/ImageEffectsPxPerc.php, line 62

Class

ImageEffectsPxPerc
Implements a form element for a quantity either in pixels or percentage.

Namespace

Drupal\image_effects\Element

Code

public static function processImageEffectsPxPerc(array &$element, FormStateInterface $form_state, array &$complete_form) {

  // Make sure element properties are set.
  $element += [
    '#title' => NULL,
    '#description' => NULL,
    '#states' => NULL,
    '#size' => 4,
    '#maxlength' => 4,
  ];

  // Determine UoM and value.
  if (strpos($element['#default_value'], '%') !== FALSE) {
    $uom = 'perc';
    $val = str_replace('%', '', $element['#default_value']);
  }
  else {
    $uom = 'px';
    $val = $element['#default_value'];
  }

  // Form elements.
  $element['c0'] = [
    '#type' => 'fieldset',
    '#title' => $element['#title'],
    '#attributes' => [
      'class' => [
        'fieldgroup',
        'form-composite',
      ],
    ],
    '#description' => $element['#description'],
  ];
  $element['c0']['c1'] = [
    '#type' => 'fieldset',
    '#attributes' => [
      'class' => [
        'container-inline',
        'fieldgroup',
        'form-composite',
      ],
    ],
  ];
  $element['c0']['c1']['value'] = [
    '#type' => 'number',
    '#default_value' => $val,
    '#size' => $element['#size'],
    '#maxlength' => $element['#maxlength'],
  ];
  $element['c0']['c1']['uom'] = [
    '#type' => 'radios',
    '#default_value' => $uom,
    '#options' => [
      'px' => t('px'),
      'perc' => t('%'),
    ],
  ];
  return $element;
}