Skip to main content
drupalreleases

Use direct release data with your AI assistant.

Learn more

Form Alterer

No security coverage Under active development 9 sites

The Form Alterer module provides an object-oriented way to modify forms in Drupal using the Plugin API. This approach offers a cleaner alternative to the traditional hook system for form alterations.

The Form Alterer module provides OOP plugin functionality to developers for easily altering forms in Drupal 9+. Typically, forms are altered via the hook system (i.e. hook_form_alter) in a module's *.module file. However, this is tedious, overly repetitive, and it really doesn't follow Drupal's new best practices for OOP design. Now, we have a module where we can alter forms via the Drupal Plugin API using an object-oriented approach.

Features

  • Provides a form_alterer_test module to illustrate functionality
  • Provides a new Attribute plugin that is discovered at Drupal\my_module\Plugin\FormAlterer
  • Provides a new YAML plugin that is discovered at my_module.form_alterers.yml
  • Plugins can provide a list of form_ids in the Plugin Definition to alter forms in different contexts
  • Plugins can provide a base form id in the list of form_ids to provide a more general form altering
  • Plugins can provide static_overrides in the Plugin Definition to alter the form(s) statically

Examples

Attribute Plugin

<?php

namespace Drupal\my_module\Plugin\FormAlterer;

use Drupal\Core\Form\FormStateInterface;
use Drupal\form_alterer\Attribute\FormAlterer;
use Drupal\form_alterer\Plugin\FormAlterer\FormAltererPluginBase;

/**
 * The test form_alterer plugin class.
 */
#[FormAlterer(
  id: 'test_form_alter',
  form_ids: [
    'node_page_edit_form',
  ]
)]
class MainContentPageFormAlterer extends FormAltererDefault {

  /**
   * {@inheritDoc}
   */
  public function alterForm(&$form, FormStateInterface $form_state, $form_id) {
    $form['#markup'] = 'Altered!';
  }

YAML Plugins

my_module_taxonomy_term_content_editor:
  label: "Form Alterer: Taxonomy Terms"
  execution_order: 2
  form_ids:
    - taxonomy_term_form
  static_overrides:
    # Show parent relations to admins only
    relations:
      '#access': TRUE
  access_roles:
    - content_editor

my_module_taxonomy_term:
  label: "Form Alterer: Taxonomy Terms"
  form_ids:
    - taxonomy_term_form
  static_overrides:
    # Hide parent relations
    relations:
      '#access': FALSE

my_module_media:
  label: "Form Alterer: Media"
  form_ids:
    - media_form
  static_overrides:
    # Hide the revision information.
    revision_information:
      '#access': FALSE

Supporting this Module

I am actively maintaining this module, but need help with putting in functional php tests.

I can be reached on the following networks to collaborate on this project:

LinkedIn Profile
Drupal Slack Channel

Ask your assistant about Form Alterer

Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
6
Tracked since
Jun 2024
Latest release
2 years ago
Releases (12 mo)
0
Maintenance
Dormant

Release Timeline

Releases

Version Type Core PHP Release date
1.1.0-alpha3 Pre-release 9–11 Sep 12, 2024
1.1.0-alpha2 Pre-release 9–11 Sep 12, 2024
1.1.0-alpha1 Pre-release 9–11 Sep 5, 2024
1.0.0-alpha2 Pre-release 9–11 Jun 17, 2024
1.0.0-alpha1 Pre-release 10 Jun 17, 2024
1.0.x-dev Dev 9–11 Jun 11, 2024