프로그램 개발(분석, 설계, 코딩, 배포)/2. 개발

Make custom configurations Translatable in Drupal

3604 2025. 1. 25. 16:52
728x90

출처: https://jaykandari.medium.com/make-custom-configurations-translatable-in-drupal-b57b7fdd71a5

 
 

Requires content_translation module enabled.

Start by creating a custom module create a folder “mymodule” and inside that module a new file: mymodule.info.yml

name: 'My module'
description: 'Demonstrates making custom configs translatable.'
type: module
core_version_requirement: ^8.8 || ^9

Create a mymodule.routing.yml file.

mymodule.configure:
 path: ‘/admin/config/mymodule/config’
 defaults:
 _form: ‘Drupal\mymodule\Form\MyModuleConfigForm’
 _title: ‘My Module configurations’
 requirements:
 _permission: ‘administer site configuration’
 options:
 _admin_route: TRUE

Lets create a mymodule.links.menu.yml file:

mymodule.configure:
 title: ‘My module configurations’
 description: ‘Administer my module configurations.’
 route_name: mymodule.configure
 parent: system.admin_config

Create a task file mymodule.links.task.yml)so that the Translate tab is available on your route:

mymodule.configure:
 base_route: mymodule.configure
 title: ‘My module settings’
 route_name: mymodule.configure

Create a schema file: ./config/schema/mymodule.schema.yml

mymodule.settings:
 type: config_object
 label: ‘My module settings’
 mapping:
   name:
     type: label
     label: ‘Placeholder text’

Let config_translation know which configuration & schema to map. Create a mymodule.config_translation.yml file

mymodule.configure:
  title: 'My module settings'
  base_route_name: mymodule.configure
  names:
    - mymodule.settings

Let's create a default configuration file. ./config/install/mymodule.settings.yml :

name: '@JayKandari'

Finally, Let’s create a config form: ./src/Form/MyModuleConfigForm.php

<?phpnamespace Drupal\mymodule\Form;use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;/**
 * Mymodule config form.
 */class MyModuleConfigForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'mymodule_config_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['mymodule.settings'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('mymodule.settings');    $form['name'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Name'),
      '#default_value' => $config->get('name') ?? '@JayKandari',
    ];    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValues();
    $this->config('mymodule.settings')
      ->set('name', $form_state->getValue('name'))
      ->save();    return parent::submitForm($form, $form_state);
  }}

You are Done!!

Now go to /admin/config/mymodule/config to access your module configuration form.

And goto /admin/config/mymodule/config/translate to add config translations to your configurations.

Thanks for reading.

728x90
반응형