Altering an Inline Entity Form

Last updated on
21 October 2022

Inline Entity Forms can be altered by using the annotation plugin @InlineEntityFormAlter.

For this particular plugin you'll need to define which type of form are you going to alter:

  • entity_form: Perform alterations before an entity form is included in the IEF widget.
  • reference_form: Perform alterations before the reference form is included in the IEF widget. The reference form is used to add existing entities through an autocomplete field.
  • table_fields: Alter the fields used to represent an entity in the IEF table.

As for the previous examples, all you have to do is just to create an Annotation Plugin in your [modulename]/src/Plugin/FormAlter folder.

Entity forms

When you've to alter an Inline Entity Form of type "entity_form" you must also define the entity_type and bundle properties.
You can use "*" as a wildcard if you want to alter all the bundles for the defined entity type.

<?php

namespace Drupal\my_module\Plugin\FormAlter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\pluginformalter\Annotation\InlineEntityFormAlter;
use Drupal\pluginformalter\Plugin\FormAlterBase;

/**
 * Class IEFAlter.
 *
 * @InlineEntityFormAlter(
 *   id = "ief_form_alter",
 *   label = @Translation("IEF form alter."),
 *   type = "entity_form",
 *   entity_type = "node",
 *   bundle = "*"
 * )
 *
 * @package Drupal\sen_core\Plugin\FormAlter
 */
class IEFAlter extends FormAlterBase {

  /**
   * @inheritDoc
   */
  public function formAlter(array &$form, FormStateInterface &$form_state, $form_id) {
    $form['dummy_textfield'] = [
      '#title' => $this->t('Dummy textfield'),
      '#type' => 'textfield',
    ];
  }

}

Reference forms

<?php

namespace Drupal\my_module\Plugin\FormAlter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\pluginformalter\Annotation\InlineEntityFormAlter;
use Drupal\pluginformalter\Plugin\FormAlterBase;

/**
 * Class IEFAlter.
 *
 * @InlineEntityFormAlter(
 *   id = "ief_form_alter",
 *   label = @Translation("IEF form alter."),
 *   type = "reference_form",
 *   entity_type = "node",
 * )
 *
 * @package Drupal\sen_core\Plugin\FormAlter
 */
class IEFAlter extends FormAlterBase {

  /**
   * @inheritDoc
   */
  public function formAlter(array &$form, FormStateInterface &$form_state, $form_id) {
    $form['entity_id']['#description'] = $this->t('New autocomplete description');
  }

}

 Table fields

For the table_fields @InlineEntityForm plugin type, you can define any of the following properties:

  • parent_entity_type: The type of the parent entity.
  • parent_bundle: The bundle of the parent entity.
  • field_name: The name of the reference field on which IEF is operating.
  • entity_type: The type of the referenced entities.
  • allowed_bundles: Bundles allowed on the reference field.
<?php

namespace Drupal\my_module\Plugin\FormAlter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\pluginformalter\Annotation\InlineEntityFormAlter;
use Drupal\pluginformalter\Plugin\FormAlterBase;

/**
 * Class IEFAlter.
 *
 * @InlineEntityFormAlter(
 *   id = "ief_form_alter",
 *   label = @Translation("IEF form alter."),
 *   type = "table_fields",
 *   entity_type = "commerce_product",
 *   parent_entity_type = "node",
 * )
 *
 * @package Drupal\sen_core\Plugin\FormAlter
 */
class IEFAlter extends FormAlterBase {

  /**
   * @inheritDoc
   */
  public function formAlter(array &$form, FormStateInterface &$form_state, $form_id) {
    $form['commerce_stock'] = [
      'type' => 'field',
      'label' => t('Stock'),
      'weight' => 101,
    ];
  }

}

Help improve this page

Page status: No known problems

You can: