You are here

public function GroupContentController::createForm in Group 8

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

Provides the group content creation form.

Parameters

\Drupal\group\Entity\GroupInterface $group: The group to add the group content to.

string $plugin_id: The group content enabler to add content with.

Return value

array A group content creation form.

File

src/Entity/Controller/GroupContentController.php, line 303

Class

GroupContentController
Returns responses for GroupContent routes.

Namespace

Drupal\group\Entity\Controller

Code

public function createForm(GroupInterface $group, $plugin_id) {

  /** @var \Drupal\group\Plugin\GroupContentEnablerInterface $plugin */
  $plugin = $group
    ->getGroupType()
    ->getContentPlugin($plugin_id);
  $wizard_id = 'group_entity';
  $store = $this->privateTempStoreFactory
    ->get($wizard_id);
  $store_id = $plugin_id . ':' . $group
    ->id();

  // See if the plugin uses a wizard for creating new entities. Also pass this
  // info to the form state.
  $config = $plugin
    ->getConfiguration();
  $extra['group_wizard'] = $config['use_creation_wizard'];
  $extra['group_wizard_id'] = $wizard_id;

  // Pass the group, plugin ID and store ID to the form state as well.
  $extra['group'] = $group;
  $extra['group_content_enabler'] = $plugin_id;
  $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;

  // Content entity form, potentially as wizard step 1.
  if (!$step2) {

    // Figure out what entity type the plugin is serving.
    $entity_type_id = $plugin
      ->getEntityTypeId();
    $entity_type = $this
      ->entityTypeManager()
      ->getDefinition($entity_type_id);
    $storage = $this
      ->entityTypeManager()
      ->getStorage($entity_type_id);

    // Only create a new entity if we have nothing stored.
    if (!($entity = $store
      ->get("{$store_id}:entity"))) {
      $values = [];
      if (($key = $entity_type
        ->getKey('bundle')) && ($bundle = $plugin
        ->getEntityBundle())) {
        $values[$key] = $bundle;
      }
      $entity = $storage
        ->create($values);
    }

    // Use the add form handler if available.
    $operation = 'default';
    if ($entity_type
      ->getFormClass('add')) {
      $operation = 'add';
    }
  }
  else {

    // Create an empty group content entity.
    $values = [
      'type' => $plugin
        ->getContentTypeConfigId(),
      'gid' => $group
        ->id(),
    ];
    $entity = $this
      ->entityTypeManager()
      ->getStorage('group_content')
      ->create($values);

    // Group content entities have an add form handler.
    $operation = 'add';
  }

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