StringMiconWidget.php in Micon 8
File
src/Plugin/Field/FieldWidget/StringMiconWidget.php
View source
<?php
namespace Drupal\micon\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\micon\Entity\Micon;
class StringMiconWidget extends WidgetBase {
public static function defaultSettings() {
return array(
'packages' => [],
) + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['packages'] = array(
'#type' => 'checkboxes',
'#title' => t('Icon Packages'),
'#default_value' => $this
->getSetting('packages'),
'#description' => t('The icon packages that should be made available in this field. If no packages are selected, all will be made available.'),
'#options' => Micon::loadActiveLabels(),
);
return $element;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['value'] = $element + [
'#type' => 'micon',
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
'#packages' => $this
->getSetting('packages'),
];
return $element;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$enabled_packages = array_filter($this
->getSetting('packages'));
if ($enabled_packages) {
$enabled_packages = array_intersect_key(Micon::loadActiveLabels(), $enabled_packages);
$summary[] = $this
->t('With icon packages: @packages', array(
'@packages' => implode(', ', $enabled_packages),
));
}
else {
$summary[] = $this
->t('With icon packages: @packages', array(
'@packages' => 'All',
));
}
return $summary;
}
}