在Drupal 8中,要在字段设置表单中添加复选框,可以按照以下步骤进行操作:
<?php
namespace Drupal\your_module\Form;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\field_ui\FieldUI;
use Drupal\field_ui\Form\FieldStorageConfigEditForm;
/**
* Implements the custom field settings form.
*/
class CustomFieldSettingsForm extends FieldStorageConfigEditForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
// Get the field definition.
$field_definition = $form_state->get('field_definition');
if (!$field_definition instanceof FieldDefinitionInterface) {
return $form;
}
// Add a checkbox element to the form.
$form['custom_checkbox'] = [
'#type' => 'checkbox',
'#title' => $this->t('Custom Checkbox'),
'#default_value' => $field_definition->getSetting('custom_checkbox'),
'#description' => $this->t('Enable this checkbox to enable custom functionality.'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
// Get the field definition.
$field_definition = $form_state->get('field_definition');
if (!$field_definition instanceof FieldDefinitionInterface) {
return;
}
// Save the checkbox value to the field settings.
$field_definition->setSetting('custom_checkbox', $form_state->getValue('custom_checkbox'));
}
}
<?php
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\field_ui\FieldUI;
use Drupal\field_ui\Form\FieldStorageConfigEditForm;
/**
* Implements hook_form_alter().
*/
function your_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Check if the form is the field settings form.
if (strpos($form_id, 'field_ui_field_config_edit_form') !== FALSE) {
// Get the field definition.
$field_definition = $form_state->get('field_definition');
if (!$field_definition instanceof FieldDefinitionInterface) {
return;
}
// Check if the field type is supported.
if ($field_definition->getType() == 'your_field_type') {
// Add a custom submit handler to save the field settings.
$form['actions']['submit']['#submit'][] = 'your_module_field_settings_submit';
}
}
}
/**
* Custom submit handler for the field settings form.
*/
function your_module_field_settings_submit(array &$form, FormStateInterface $form_state) {
// Get the field definition.
$field_definition = $form_state->get('field_definition');
if (!$field_definition instanceof FieldDefinitionInterface) {
return;
}
// Save the checkbox value to the field settings.
$field_definition->setSetting('custom_checkbox', $form_state->getValue('custom_checkbox'));
}
name: 'Your Module'
type: module
description: 'Custom module for Drupal 8.'
package: Custom
core_version_requirement: ^8 || ^9
dependencies:
- field_ui:field_ui
请注意,以上代码仅为示例,你需要根据你的实际需求进行修改和调整。
希望这个答案能够帮助到你!如果你需要更多关于Drupal 8的帮助,请随时提问。