1

I'm loading a field collection item form inside of a formatter. Below is the function loading the form, which works. However I do not want to call the normal field collection submitForm handler, I want to override the submit handler with my own. I've tried attaching my submit handler, but the field collection submit handler still continues getting called.

I don't want to use a form_alter, I'm trying to keep this in the FormBase or ContentEntityForm class. Does anyone know how I can override the field collection submit handler?

<?php
public function getFCForm(FieldItemInterface $item, $fc_name) {
    $entity = $item->getFieldCollectionItem();
    $displays = EntityViewDisplay::collectRenderDisplays([$entity], 'full');
    $display = $displays[$entity->bundle()];
    foreach ($display->getComponents() as $name => $options) {
      $fields_by_weight[$options['weight']] = $name;
    }
    ksort($fields_by_weight);

    //load fc entity
    $fieldCollection = \Drupal\field_collection\Entity\FieldCollection::load($fc_name);


    $field_collection_item = \Drupal::entityTypeManager()
      ->getStorage('field_collection_item')
      ->create([
        'field_name' => $fieldCollection->id(),
        'host_type' => 'node',
        'revision_id' => 0,
      ]);

    $form = \Drupal::service('entity.form_builder')->getForm($field_collection_item);
    $form['#prefix'] = '<div id="inline_fc_form">';
    $form['#suffix'] = '</div>';
    $form['actions']['submit']['#submit']=['::ajaxFormSubmitHandler'];
    $form['actions']['submit']['#ajax_processed'] = TRUE;
    $form['actions']['submit']['#value'] = t('wtf');
    $form['#validate'] = ['::validateForm'];
    $form['#submit'] = ['::ajaxFormSubmitHandler'];

    //custom submit handler not being called?

    return $form;
  }
1
  • This doesn't work, overriding submit handlers once you've got the form from the entity form builder. You don't want a form alter hook, so another option is to override the entity form and set the custom form class in the entity type, see drupal.stackexchange.com/questions/238723/…
    – 4uk4
    Oct 23, 2018 at 20:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.