View source
<?php
namespace Drupal\fontawesome_iconpicker\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class FontawesomeIconpicker extends WidgetBase {
public static function defaultSettings() {
return [
'size' => 60,
'placeholder' => '',
'type' => '',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = [];
$elements['type'] = [
'#type' => 'select',
'#title' => t('Type of Icon Picker'),
'#default_value' => $this
->getSetting('type'),
'#required' => TRUE,
'#options' => $this
->getIconPickerTypes(),
];
$elements['size'] = array(
'#type' => 'number',
'#min' => 0,
'#step' => 1,
'#title' => t('Field Size'),
'#description' => t('Select a field size.'),
'#default_value' => $this
->getSetting('size'),
);
$elements['placeholder'] = [
'#type' => 'textfield',
'#title' => t('Placeholder'),
'#default_value' => $this
->getSetting('placeholder'),
'#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
];
return $elements;
}
public function settingsSummary() {
$summary = [];
if (!empty($this
->getSetting('type'))) {
$label = $this
->getIconPickerTypes()[$this
->getSetting('type')];
$summary[] = t('Type: @type', [
'@type' => $label,
]);
}
if (!empty($this
->getSetting('placeholder'))) {
$summary[] = t('Placeholder: @placeholder', [
'@placeholder' => $this
->getSetting('placeholder'),
]);
}
if (!empty($this
->getSetting('size'))) {
$summary[] = t('Field size: @size', [
'@size' => $this
->getSetting('size'),
]);
}
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$type = $this
->getSetting('type');
switch ($type) {
case 'component':
$element['value'] = $element + [
'#type' => 'textfield',
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
'#size' => $this
->getSetting('size'),
'#icon' => $this
->getSetting('icon'),
'#placeholder' => $this
->getSetting('placeholder'),
'#maxlength' => $this
->getFieldSetting('max_length'),
'#attributes' => [
'data-iconpicker' => '',
'data-placement' => 'bottomRight',
'class' => [
'fontawesome-iconpicker-element',
'form-control',
'js-fontawesome-iconpicker-is-component',
],
],
'#attached' => [
'library' => [
'fontawesome_iconpicker/fontawesome-iconpicker',
],
],
];
break;
case 'default':
case 'input_search':
default:
$element['value'] = $element + [
'#type' => 'textfield',
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
'#size' => $this
->getSetting('size'),
'#icon' => $this
->getSetting('icon'),
'#placeholder' => $this
->getSetting('placeholder'),
'#maxlength' => $this
->getFieldSetting('max_length'),
'#attributes' => [
'data-input-search' => $type == 'input_search' ? 'true' : 'false',
'data-iconpicker' => '',
'class' => [
'fontawesome-iconpicker-element',
],
],
'#attached' => [
'library' => [
'fontawesome_iconpicker/fontawesome-iconpicker',
],
],
];
break;
}
return $element;
}
private function getIconPickerTypes() {
return [
'default' => $this
->t('Default'),
'component' => $this
->t('As a bootstrap component'),
'input_search' => $this
->t('Input as a search box'),
];
}
}