You are here

public static function EntityconnectFormUtils::entityFormAlter in Entity connect 8.2

Add the entityconnect button(s) to the form.

It's done here since we only have access to the actual widget element in hook_field_widget_form_alter().

Parameters

array $form: The form to add to.

\Drupal\Core\Form\FormStateInterface $form_state: The state of the form.

1 call to EntityconnectFormUtils::entityFormAlter()
entityconnect_form_alter in ./entityconnect.module
Implements hook_form_alter().

File

src/EntityconnectFormUtils.php, line 49

Class

EntityconnectFormUtils
Contains form alter, callbacks and utility methods for entityconnect.

Namespace

Drupal\entityconnect

Code

public static function entityFormAlter(array &$form, FormStateInterface $form_state) {

  // Get the applicable entity reference fields from the form.
  $ref_fields = static::getReferenceFields($form, $form_state);

  // Attach our custom process callback to each entity reference element.
  if ($ref_fields) {
    foreach ($ref_fields as $field) {

      // Merge our #process callback with the defaults.
      $elementInfo = \Drupal::service('element_info');
      $form[$field]['#process'] = array_merge($elementInfo
        ->getInfoProperty($form[$field]['#type'], '#process', []), [
        [
          '\\Drupal\\entityconnect\\EntityconnectWidgetProcessor',
          'process',
        ],
      ]);

      // Add our #validate callback to the entity form.
      // This prevents the exception on entityconnect elements caused by
      // submitting the form without using the entityconnect buttons.
      $form['#validate'] = !isset($form['#validate']) ? [] : $form['#validate'];
      array_unshift($form['#validate'], [
        '\\Drupal\\entityconnect\\EntityconnectFormUtils',
        'validateForm',
      ]);
    }
  }
}