You are here

public function SubgroupWizardController::addForm in Subgroup (Graph) 1.0.x

Provides the form for creating a subgroup in a group.

Parameters

\Drupal\group\Entity\GroupInterface $group: The group to create a subgroup in.

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

Return value

array The form array for either step 1 or 2 of the subgroup creation wizard.

1 string reference to 'SubgroupWizardController::addForm'
ggroup.routing.yml in ./ggroup.routing.yml
ggroup.routing.yml

File

src/Controller/SubgroupWizardController.php, line 106

Class

SubgroupWizardController
Returns responses for 'subgroup' GroupContent routes.

Namespace

Drupal\ggroup\Controller

Code

public function addForm(GroupInterface $group, GroupTypeInterface $group_type) {
  $plugin_id = "subgroup:{$group_type->id()}";
  $storage_id = "{$plugin_id}:{$group->id()}";
  $creation_wizard = $group
    ->getGroupType()
    ->getContentPlugin($plugin_id)
    ->getConfiguration()['use_creation_wizard'];

  // If we are on step one, we need to build a group form.
  if ($this->privateTempStoreFactory
    ->get("{$storage_id}:step") !== 2) {
    $this->privateTempStoreFactory
      ->set("{$storage_id}:step", 1);

    // Only create a new group if we have nothing stored.
    if (!($entity = $this->privateTempStoreFactory
      ->get("{$storage_id}:group"))) {
      $entity = Group::create([
        'type' => $group_type
          ->id(),
      ]);
    }
  }
  else {

    /** @var \Drupal\group\Plugin\GroupContentEnablerInterface $plugin */
    $plugin = $group
      ->getGroupType()
      ->getContentPlugin($plugin_id);
    $entity = GroupContent::create([
      'type' => $plugin
        ->getContentTypeConfigId(),
      'gid' => $group
        ->id(),
    ]);
    if (!$creation_wizard && ($entity = $this->privateTempStoreFactory
      ->get("{$storage_id}:group"))) {
      $entity
        ->save();
      $group
        ->addContent($entity, $plugin_id);

      // We also clear the private store so we can start fresh next time
      // around.
      $this->privateTempStoreFactory
        ->delete("{$storage_id}:step");
      $this->privateTempStoreFactory
        ->delete("{$storage_id}:group");
      return $this
        ->redirect('entity.group.canonical', [
        'group' => $entity
          ->id(),
      ]);
    }
  }

  // Return the form with the group and storage ID added to the form state.
  $extra = [
    'group' => $group,
    'storage_id' => $storage_id,
    'wizard' => $creation_wizard,
  ];
  return $this
    ->entityFormBuilder()
    ->getForm($entity, 'ggroup-form', $extra);
}