You are here

public function GroupController::addForm in Group 8

Same name and namespace in other branches
  1. 2.0.x src/Entity/Controller/GroupController.php \Drupal\group\Entity\Controller\GroupController::addForm()

Provides the group creation form.

Parameters

\Drupal\group\Entity\GroupTypeInterface $group_type: The type of group to create.

Return value

array A group submission form.

File

src/Entity/Controller/GroupController.php, line 86

Class

GroupController
Returns responses for Group routes.

Namespace

Drupal\group\Entity\Controller

Code

public function addForm(GroupTypeInterface $group_type) {
  $wizard_id = 'group_creator';
  $store = $this->privateTempStoreFactory
    ->get($wizard_id);
  $store_id = $group_type
    ->id();

  // See if the group type is configured to ask the creator to fill out their
  // membership details. Also pass this info to the form state.
  $extra['group_wizard'] = $group_type
    ->creatorMustCompleteMembership();
  $extra['group_wizard_id'] = $wizard_id;

  // Pass the group type and store ID to the form state as well.
  $extra['group_type'] = $group_type;
  $extra['store_id'] = $store_id;

  // See if we are on the second step of the form.
  $step2 = $extra['group_wizard'] && $store
    ->get("{$store_id}:step") === 2;

  // Group form, potentially as wizard step 1.
  if (!$step2) {
    $storage = $this
      ->entityTypeManager()
      ->getStorage('group');

    // Only create a new group if we have nothing stored.
    if (!($entity = $store
      ->get("{$store_id}:entity"))) {
      $values['type'] = $group_type
        ->id();
      $entity = $storage
        ->create($values);
    }
  }
  else {

    // Create an empty group membership that does not yet have a group set.
    $values = [
      'type' => $group_type
        ->getContentPlugin('group_membership')
        ->getContentTypeConfigId(),
      'entity_id' => $this
        ->currentUser()
        ->id(),
    ];
    $entity = $this
      ->entityTypeManager()
      ->getStorage('group_content')
      ->create($values);
  }

  // Return the entity form with the configuration gathered above.
  return $this
    ->entityFormBuilder()
    ->getForm($entity, 'add', $extra);
}