【问题标题】:How to add a new custom field to "Site Infomation" Form in Drupal8如何在 Drupal 8 中向“站点信息”表单添加新的自定义字段
【发布时间】:2019-06-30 02:00:03
【问题描述】:

我想在 Drupal8 的“站点信息”表单中添加一个新的自定义字段。我尝试了很多答案,但没有得到正确的解决方案。有什么方法可以添加自定义字段。请建议。提前谢谢。

【问题讨论】:

    标签: php drupal-8


    【解决方案1】:

    考虑模块名称是 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" 根据这个类创建一个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'
    

    按照上述步骤清除缓存后,您将在“站点信息”表单中看到一个新字段“站点 API 密钥”。

    【讨论】:

    • 我无法扩展配置模式,由于某种原因未检测到它。任何人都知道任何原因
    • 虽然这确实有效,但我似乎无法翻译新字段,即使它是正确的“标签”类型。仅显示“站点名称”和“标语”,它们也是“标签”类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 2020-06-21
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多