You are here

CnpjWidget.php in Brazilian IDs 8

File

src/Plugin/Field/FieldWidget/CnpjWidget.php
View source
<?php

namespace Drupal\brazilian_ids\Plugin\Field\FieldWidget;

use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextfieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Field widget for CNPJ number input. It can be used with normal text fields.
 *
 * @FieldWidget(
 *   id = "brazilian_ids_cnpj",
 *   label = @Translation("CNPJ"),
 *   field_types = {
 *     "string"
 *   }
 * )
 */
class CnpjWidget extends StringTextfieldWidget {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'size' => 18,
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $form = parent::settingsForm($form, $form_state);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element['value'] = $element + array(
      '#type' => 'brazilian_ids_cnpj',
      '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
      '#size' => $this
        ->getSetting('size'),
      '#placeholder' => $this
        ->getSetting('placeholder'),
      '#attributes' => [
        'class' => [
          'js-text-full',
          'text-full',
        ],
      ],
    );
    return $element;
  }

}

Classes

Namesort descending Description
CnpjWidget Field widget for CNPJ number input. It can be used with normal text fields.