You are here

public function EntityCreateController::addForm in Entity API 8.0

Provides the add form for an entity.

Parameters

string $entity_type_id: The entity type ID.

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match.

Return value

array The add form.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Thrown when the bundle parameter is invalid.

File

src/Controller/EntityCreateController.php, line 142
Contains \Drupal\entity\Controller\EntityCreateController.

Class

EntityCreateController
A generic controller for creating entities.

Namespace

Drupal\entity\Controller

Code

public function addForm($entity_type_id, RouteMatchInterface $route_match) {
  $entity_type = $this
    ->entityTypeManager()
    ->getDefinition($entity_type_id);
  $values = [];

  // Entities of this type have bundles, one was provided in the url.
  if ($bundle_key = $entity_type
    ->getKey('bundle')) {
    $bundle_name = $route_match
      ->getRawParameter($bundle_key);
    $bundles = $this->entityTypeBundleInfo
      ->getBundleInfo($entity_type_id);
    if (empty($bundle_name) || !isset($bundles[$bundle_name])) {

      // The bundle parameter is invalid.
      throw new NotFoundHttpException();
    }
    $values[$bundle_key] = $bundle_name;
  }
  $entity = $this
    ->entityTypeManager()
    ->getStorage($entity_type_id)
    ->create($values);
  return $this
    ->entityFormBuilder()
    ->getForm($entity, 'add');
}