Commit a2172f20 authored by Jordan White's avatar Jordan White
Browse files

Add submission handler

parent 2781412b
Showing with 45 additions and 12 deletions
+45 -12
......@@ -266,6 +266,7 @@ function compose_inline_entity_form_reference_form_alter(&$reference_form, &$for
'#type' => 'submit',
'#value' => t('Select'),
'#name' => 'ief-reference-add-existing-' . $entity_id,
'#ief_row_delta' => $i,
'#limit_validation_errors' => [$reference_form['#parents']],
'#id' => 'entity-id-'. $entity_id,
'#attributes' => [
......@@ -282,8 +283,8 @@ function compose_inline_entity_form_reference_form_alter(&$reference_form, &$for
],
],
];
$reference_form['entity_table'][$i]['button']['#submit'][] = 'Drupal\compose\Plugin\Field\FieldWidget\ComposeWidget::addExistingSubmit';
$reference_form['entity_table'][$i]['button']['#submit'][] = 'Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex::submitCloseRow';
InlineEntityFormComplex::addSubmitCallbacks($reference_form['entity_table'][$i]['button']);
$i++;
}
......
......@@ -86,20 +86,13 @@ class ComposeWidget extends InlineEntityFormComplex implements ContainerFactoryP
if ($entity_id !== NULL && $entity_type !== NULL) {
$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$entity = $storage->load($entity_id);
$entities = &$form_state->get(['inline_entity_form', $ief_id, 'entities']);
$entities = $form_state->get(['inline_entity_form', $ief_id, 'entities']);
// Determine the correct weight of the new element.
$weight = 0;
if ($entities) {
$weight = max(array_keys($entities)) + 1;
}
$entities[] = [
'entity' => $entity,
'weight' => $weight,
'form' => NULL,
'needs_save' => FALSE,
];
$element['entities'][] = [
'#entity' => $entity,
'#label' => $entity->label(),
......@@ -113,9 +106,6 @@ class ComposeWidget extends InlineEntityFormComplex implements ContainerFactoryP
// Pass along the IEF form id, field name and entity type.
$element['entities']['#form_id'] = $ief_id;
$form_state->set(['inline_entity_form', $ief_id, 'entities'], $entities);
$form_state->setCached(TRUE);
}
return $element;
......@@ -165,4 +155,46 @@ class ComposeWidget extends InlineEntityFormComplex implements ContainerFactoryP
}
}
}
/**
* Button #submit callback: Add existing Compose buttons.
*
* @param $form
* The complete parent form.
* @param $form_state
* The form state of the parent form.
*/
public static function addExistingSubmit($form, FormStateInterface $form_state) {
$element = inline_entity_form_get_element($form, $form_state);
$ief_id = $element['#ief_id'];
$triggering_element = $form_state->getTriggeringElement();
$button_name = $triggering_element['#name'];
$instance = $form_state->get(['inline_entity_form', $ief_id, 'instance']);
$entity_type = $instance->getSetting('target_type');
if (strpos($button_name, 'ief-reference-add-existing-') !== FALSE) {
$entity_id = str_replace('ief-reference-add-existing-', '', $button_name);
}
if ($entity_id !== NULL && $entity_type !== NULL) {
$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$entity = $storage->load($entity_id);
$entities = &$form_state->get(['inline_entity_form', $ief_id, 'entities']);
// Determine the correct weight of the new element.
$weight = 0;
if ($entities) {
$weight = max(array_keys($entities)) + 1;
}
$entities[] = [
'entity' => $entity,
'weight' => $weight,
'form' => NULL,
'needs_save' => FALSE,
];
$form_state->set(['inline_entity_form', $ief_id, 'entities'], $entities);
$form_state->setRebuild();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment