You are here

public static function Cpf::preRenderCpf in CPF 8.2

Same name and namespace in other branches
  1. 8 src/Element/Cpf.php \Drupal\cpf\Element\Cpf::preRenderCpf()

Prepares a #type 'cpf' render element for input.html.twig.

Parameters

array $element: An associative array containing the properties of the element. Properties used: #title, #value, #description, #size, #maxlength, #placeholder, #required, #attributes.

Return value

array The $element with prepared variables ready for input.html.twig.

File

src/Element/Cpf.php, line 100

Class

Cpf
Provides a form input element for entering an CPF number.

Namespace

Drupal\cpf\Element

Code

public static function preRenderCpf(array $element) {
  $element['#attributes']['type'] = 'cpf';
  $element['#maxlength'] = $element['#mask'] ? self::MAX_LENGTH_CANONICAL_MASK : self::MAX_LENGTH_DIGITS_MASK;
  $data['cpf']['mask_plugin']['elements'][$element['#id']] = [
    'id' => $element['#id'],
    'mask' => $element['#mask'] ? self::CANONICAL_MASK : self::DIGITS_MASK,
  ];
  $element['#attached'] = [
    'library' => [
      'cpf/cpf',
    ],
    'drupalSettings' => $data,
  ];
  $attributes = [
    'id',
    'name',
    'value',
    'size',
    'maxlength',
    'placeholder',
  ];
  Element::setAttributes($element, $attributes);
  static::setAttributes($element, [
    'form-cpf',
  ]);
  return $element;
}