SearchApiExcludeEntityFieldWidget.php in Search API Exclude Entity 8
File
src/Plugin/Field/FieldWidget/SearchApiExcludeEntityFieldWidget.php
View source
<?php
namespace Drupal\search_api_exclude_entity\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
class SearchApiExcludeEntityFieldWidget extends WidgetBase {
public static function defaultSettings() {
return [
'field_label' => new TranslatableMarkup('Yes, exclude this entity from the search indexes.'),
] + parent::defaultSettings();
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element += [
'#type' => 'details',
'#group' => 'advanced',
];
$element['value'] = [
'#type' => 'checkbox',
'#default_value' => !empty($items[0]->value),
'#title' => $this
->getSetting('field_label'),
'#required' => !empty($element['#required']) ? $element['#required'] : false,
];
return $element;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['field_label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Checkbox label'),
'#description' => $this
->t('Text used as label next to the field checkbox.'),
'#default_value' => $this
->getSetting('field_label'),
];
return $elements;
}
public function settingsSummary() {
$summary = [];
$field_label = $this
->getSetting('field_label');
if (!empty($field_label)) {
$summary[] = $this
->t('Checkbox label: @field_label', [
'@field_label' => $field_label,
]);
}
return $summary;
}
}