如何在 Drupal8 中向 "Site Infomation" 表单添加新的自定义字段
How to add a new custom field to "Site Infomation" Form in Drupal8
我想在 Drupal8 中向 "Site Infomation" 表单添加一个新的自定义字段。我尝试了很多答案,但没有得到正确的解决方案。有没有办法添加自定义字段。请建议。提前致谢。
考虑模块名称是 mymodule。
mymodule.services.yml 文件示例
在您的 mymodule.services.yml
中注册一个事件订阅者
services:
bssa.route_subscriber:
class: Drupal\bssa\Routing\RouteSubscriber
tags:
- { name: event_subscriber }
class : "Drupal\mymodule\Routing\RouteSubscriber" 根据此 class 创建一个 php 文件,如下所示。
扩展 RouteSubscriber 以实现新的字段形式
mymodule/src/Routing/RouteSubscriber.php
<?php
namespace Drupal\mymodule\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Listens to the dynamic route events.
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('system.site_information_settings'))
$route->setDefault('_form', 'Drupal\mymodule\Form\ExtendedSiteInformationForm');
}
}
现在在mymodule/src/Form/ExtendedSiteInformation.php中创建一个新表单来添加自定义字段
<?php
namespace Drupal\mymodule\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\system\Form\SiteInformationForm;
class ExtendedSiteInformationForm extends SiteInformationForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$site_config = $this->config('system.site');
$form = parent::buildForm($form, $form_state);
$form['site_information']['siteapikey'] = [
'#type' => 'textfield',
'#title' => t('Site API Key'),
'#default_value' => $site_config->get('siteapikey') ?: 'No API Key yet',
'#description' => t("Custom field to set the API Key"),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('system.site')
->set('siteapikey', $form_state->getValue('siteapikey'))
->save();
parent::submitForm($form, $form_state);
}
}
现在创建一个配置变量来保存 mymodule/config/schema/mymodule 中新字段的值。schema.yml
# We want to extend the system.site configuration
system.site:
mapping:
# Our field name is 'siteapikey'
siteapikey:
type: label
label: 'Site API Keys'
按照上述步骤清除缓存后,您将在 "Site Information" 表单中看到一个新字段 "Site API Key"。
我想在 Drupal8 中向 "Site Infomation" 表单添加一个新的自定义字段。我尝试了很多答案,但没有得到正确的解决方案。有没有办法添加自定义字段。请建议。提前致谢。
考虑模块名称是 mymodule。
mymodule.services.yml 文件示例
在您的 mymodule.services.yml
中注册一个事件订阅者services:
bssa.route_subscriber:
class: Drupal\bssa\Routing\RouteSubscriber
tags:
- { name: event_subscriber }
class : "Drupal\mymodule\Routing\RouteSubscriber" 根据此 class 创建一个 php 文件,如下所示。
扩展 RouteSubscriber 以实现新的字段形式 mymodule/src/Routing/RouteSubscriber.php
<?php
namespace Drupal\mymodule\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Listens to the dynamic route events.
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('system.site_information_settings'))
$route->setDefault('_form', 'Drupal\mymodule\Form\ExtendedSiteInformationForm');
}
}
现在在mymodule/src/Form/ExtendedSiteInformation.php中创建一个新表单来添加自定义字段
<?php
namespace Drupal\mymodule\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\system\Form\SiteInformationForm;
class ExtendedSiteInformationForm extends SiteInformationForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$site_config = $this->config('system.site');
$form = parent::buildForm($form, $form_state);
$form['site_information']['siteapikey'] = [
'#type' => 'textfield',
'#title' => t('Site API Key'),
'#default_value' => $site_config->get('siteapikey') ?: 'No API Key yet',
'#description' => t("Custom field to set the API Key"),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('system.site')
->set('siteapikey', $form_state->getValue('siteapikey'))
->save();
parent::submitForm($form, $form_state);
}
}
现在创建一个配置变量来保存 mymodule/config/schema/mymodule 中新字段的值。schema.yml
# We want to extend the system.site configuration
system.site:
mapping:
# Our field name is 'siteapikey'
siteapikey:
type: label
label: 'Site API Keys'
按照上述步骤清除缓存后,您将在 "Site Information" 表单中看到一个新字段 "Site API Key"。