Php 如何将新自定义字段添加到";网站信息“;Drupal8格式
我想在Drupal8的“站点信息”表单中添加一个新的自定义字段。我试过许多答案,但没有得到正确的答案。有没有办法添加自定义字段。请建议。Thanx提前。考虑模块名为mymodule mymodule.services.yml文件的示例 在mymodule.services.yml中注册事件订阅服务器Php 如何将新自定义字段添加到";网站信息“;Drupal8格式,php,drupal-8,Php,Drupal 8,我想在Drupal8的“站点信息”表单中添加一个新的自定义字段。我试过许多答案,但没有得到正确的答案。有没有办法添加自定义字段。请建议。Thanx提前。考虑模块名为mymodule mymodule.services.yml文件的示例 在mymodule.services.yml中注册事件订阅服务器 services: bssa.route_subscriber: class: Drupal\bssa\Routing\RouteSubscriber tags: -
services:
bssa.route_subscriber:
class: Drupal\bssa\Routing\RouteSubscriber
tags:
- { name: event_subscriber }
类:“Drupal\mymodule\Routing\RouteSubscriber”根据该类创建一个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');
}
}
按照上述步骤清除缓存后,您将在“站点信息”表单中看到一个新字段“站点API密钥”。我无法扩展配置架构,因为某些原因,它未被检测到。任何人都知道任何原因虽然这确实有效,但我似乎无法翻译新字段,即使它的类型是正确的“label”。仅显示“站点名称”和“标语”,这两种名称也属于“标签”类型。
<?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);
}
}
# We want to extend the system.site configuration
system.site:
mapping:
# Our field name is 'siteapikey'
siteapikey:
type: label
label: 'Site API Keys'