acquia_commercemanager-8.x-1.122/modules/acm_sku/src/Plugin/Field/FieldWidget/SKUFieldWidget.php
modules/acm_sku/src/Plugin/Field/FieldWidget/SKUFieldWidget.php
<?php
namespace Drupal\acm_sku\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'sku_wiget' widget.
*
* @FieldWidget(
* id = "sku_widget",
* label = @Translation("SKU Reference Widget"),
* field_types = {
* "sku"
* }
* )
*/
class SKUFieldWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'size' => 60,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = [];
$elements['size'] = [
'#type' => 'number',
'#title' => t('Size of textfield'),
'#default_value' => $this->getSetting('size'),
'#required' => TRUE,
'#min' => 1,
];
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = t('Textfield size: @size', ['@size' => $this->getSetting('size')]);
return $summary;
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['value'] = $element + [
'#type' => 'textfield',
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
'#size' => $this->getSetting('size'),
'#maxlength' => $this->getFieldSetting('max_length'),
];
return $element;
}
}
