Skip to main content
Home
Drupal life hacks

Main navigation

  • Drupal
  • React
  • WP
  • Contact
  • About
User account menu
  • Log in

Breadcrumb

  1. Home

Adding a Class to Support the Standard Installation Profile in Drupal

By admin, 13 April, 2024

In Drupal, if you're working with a custom installation profile and you want to add a form class to it, you should follow the standard module structure. Here's how you can do it:

1. Create a custom module within your installation profile directory.
2. Place your form class file within the module's `src/Form` directory.
3. Define the namespace for your form class as per Drupal coding standards.
4. Ensure that your module is enabled in the Drupal installation.

Here's an example of how your directory structure would look:

```
profiles/
 my_profile/
   modules/
     custom/
       my_custom_module/
         src/
           Form/
             MyCustomForm.php
         my_custom_module.info.yml
         my_custom_module.module
```

In this structure:

- `MyCustomForm.php`: Contains your custom form class.
- `my_custom_module.info.yml`: Provides metadata about your module.
- `my_custom_module.module`: Contains hooks and other PHP code for your module.

Your form class file `MyCustomForm.php` might look like this:

```php
<?php

namespace Drupal\my_custom_module\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* My custom form.
*/
class MyCustomForm extends FormBase {

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

 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state) {
   // Build your form elements here.
   $form['#attributes']['class'][] = 'my-custom-class';

   return $form;
 }

 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state) {
   // Handle form submission.
 }

}
```

After adding your form class, ensure that you clear the cache so that Drupal recognizes your new module and form class. You can then use this form class in your installation profile or any other part of your Drupal site as needed.

Tags

  • #Drupal Planet

Comments

About text formats

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Powered by Drupal