2

I want to extend some fields in a config form of a module with another module in Drupal 8. I created a config form in a module (e.g. custom_1) and now I want to add some more fields from another module (e.g. custom_2). custom_1 has a dependency on custom_2. I tried hook_form_alter() but it didn't resolve the issue.

Is there any other way to achieve this in Drupal 8?

3

Solution:

I came out to this solution:

I created a custom_1.routing.yml file and write the following code in it.

#Router settings for custom service page
custom_form.service_config:
path: '/admin/config/services/custom'
defaults:
  _form: '\Drupal\custom_1\Form\CustomConfigForm'
  _title: 'Custom Config Form'
  _description: 'Custom Configuration form.'
requirements:
  _permission: 'administer custom'

I created a CustomConfigForm.php file in custom_1/src/Form/CustomConfigForm.php.

    namespace Drupal\custom_1\Form;

    use Drupal\Core\Form\ConfigFormBase;
    use Drupal\Core\Form\FormStateInterface;

    class CustomConfigForm extends ConfigFormBase {
      public funtion getFormId() {
          return 'custom_form_service_config_form'; 
      }
      public function getEditableConfigNames() {
          return ['custom_form.service_config'];  
      }
      public function buildForm(array $form, FormStateInterface $form_state) {
          // Form code here
         return buildform($form, $form_state);
      }
      public function submitForm(array &$form, FormStateInterface $form_state) {
         // Submit code here
      }
    }

Now, I have to merge some more form fields from another module custom_2. To achieve this, I followed the following steps. I created a custom_2.routing.yml file and write the following code in it.

#Router settings for custom service page
custom_form.service_config:
path: '/admin/config/services/custom'
defaults:
  _form: '\Drupal\custom_2\Form\CustomTwoConfigForm'
  _title: 'Custom Config Form'
  _description: 'Custom Configuration form.'
requirements:
  _permission: 'administer custom'

I created a CustomTwoConfigForm.php file in custom_2/src/Form/CustomTwoConfigForm.php.

    namespace Drupal\custom_2\Form;

    use Drupal\Core\Form\FormStateInterface;
    use Drupal\custom_1\Form\CustomConfigForm;

    class CustomTwoConfigForm extends CustomConfigForm  {
      public funtion getFormId(){
          return 'custom_form_service_config_form'; 
      }
      public function getEditableConfigNames(){
          return ['custom_form.service_config'];  
      }
      public function buildForm(array $form, FormStateInterface $form_state) {
           $form = parent::buildform($form, $form_state);
          // Form code here
         return parent::buildform($form, $form_state);
      }
      public function submitForm(array &$form, FormStateInterface $form_state) {
         // Submit code here
      }
    }

This resolve the issue for me without using hook_form_alter().

Thanks

  • I would honestly advise you to use hook_form_alter until it has been officially deprecated in favor of a cleaner method. Why did hook_form_alter not work? Also, I am not sure you want to call parent::buildForm twice on $form, only once. The buildFrom method should be named buildForm, or is it intentionally buildFrom? The naming is confusing because its easily read as buildForm (used a lot in D8). This is why hook_form_alter for editing existing forms is still a cleaner option. – Kevin Jun 28 '16 at 14:05
  • @Kevin: In the above code parent::buildForm called twice in custom_2 module because the first parent::bulidFrom will get all the fields from custom_1 module and the second return the current form. I have been facing problem in saving fields during form submission when used hook_form_alter. As drupal 8 is using symfony architecture, that is the other reason why I used this approach instead of hook_form_alter. – Shrikant D Jun 30 '16 at 8:31
  • It just doesn't look maintainable and in the end does the same thing hook_form_alter would do. Add some fields, and add validate/submit handlers, and call them. Until core provides a way to form alter that is more in line with the OOP parts of Form API, I would keep it simple. – Kevin Jun 30 '16 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.