You are here

public function GroupContentForm::complete in Group 2.0.x

Same name and namespace in other branches
  1. 8 src/Entity/Form/GroupContentForm.php \Drupal\group\Entity\Form\GroupContentForm::complete()

Completes the creation wizard by saving the target entity.

Please note that we are instantiating an entity form to replicate the first step and call the save method on that form. This is done to ensure that any logic in the save handler is actually run when the wizard completes.

Parameters

array $form: An associative array containing the structure of the form.

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

See also

\Drupal\group\Entity\Controller\GroupContentController::createForm()

File

src/Entity/Form/GroupContentForm.php, line 168

Class

GroupContentForm
Form controller for the group content edit forms.

Namespace

Drupal\group\Entity\Form

Code

public function complete(array &$form, FormStateInterface $form_state) {
  $wizard_id = $form_state
    ->get('group_wizard_id');
  $store = $this->privateTempStoreFactory
    ->get($wizard_id);
  $store_id = $form_state
    ->get('store_id');
  $entity = $store
    ->get("{$store_id}:entity");

  // Use the add form handler, if available, otherwise default.
  $operation = 'default';
  if ($entity
    ->getEntityType()
    ->getFormClass('add')) {
    $operation = 'add';
  }

  // Replicate the form from step 1 and call the save method.
  $form_object = $this->entityTypeManager
    ->getFormObject($entity
    ->getEntityTypeId(), $operation);
  $form_object
    ->setEntity($entity);
  $form_object
    ->save($form, $form_state);

  // Add the newly saved entity's ID to the group content entity.
  $property = $wizard_id == 'group_creator' ? 'gid' : 'entity_id';
  $this->entity
    ->set($property, $entity
    ->id());

  // We also clear the temp store so we can start fresh next time around.
  $store
    ->delete("{$store_id}:step");
  $store
    ->delete("{$store_id}:entity");
}