This line crashes:

 $this->getFormDisplay($form_state)->buildForm($this->entity, $form, $form_state);

because getFormDisplay() returns NULL.

Comments

joachim created an issue. See original summary.

3li’s picture

Did you find any solution for this? I am having the same issue.

joachim’s picture

I've not been working on the 'add license' form lately. As its use case is an admin user creating a license by hand for another user, I figure it's not part of the MVP.

Kazanir’s picture

The license creation form is broken until we figure out what to do with it. Licenses need to be full content entities, meaning that we really want to inherit from ContentEntityForm, but we cannot because it assumes the bundle to be pre-chosen. I got stuck on both solution paths to this:

1. Replicate enough of ContentEntityForm to get the thing to work despite inheriting from FormBase.
2. Use conditional logic to inherit from ContentEntityForm but only call the methods that depend on the $entity object once the bundle has been selected. This ran into other DI and routing issues.

Bojan was skeptical of option #2, but we're in a hard spot here since ContentEntityForm does *so much* stuff and we can't farm the job out to a payment gateway plugin for our entity form -- licenses are complex content entities not simple payment transactions. :/

So, a solution here is TBD. Don't use the creation form for now. Licenses are really supposed to be created via triggers during checkout, which is what we're working on next.

joachim’s picture

If I understand correctly, the idea is that this is a form that you get shown when you click 'Add license' in the license admin UI?

In which case, why can't it be a 2-step process, with a page to pick the bundle and then a form, the same as when you create a node? That would simplify *everything*.

quietone’s picture

FYI, ran into this today trying to create a license, in order to understand how to migrate Ubercart 6 file downloads and roles. #2893391: [Ubercart D6] Migrate product features

joachim’s picture

If you're using Migrate, then this form shouldn't be involved, as Migrate creates entities via the API.

joachim’s picture

> In which case, why can't it be a 2-step process, with a page to pick the bundle and then a form, the same as when you create a node? That would simplify *everything*.

I am now heavily in favour of this. I don't think there's any need to have AJAX in this form, as it's admin-facing only.

For the cart form, the type of license will be dictacted by the product, and so already set. No need to AJAX there either.

So the fix here is to rip out lots of the code in the form classes and reduce them to fairly slim child classes of the ContentEntityForm class.

krystalcode’s picture

I've created a patch with a solution that follows the pattern used for content creation. You get a page listing all license types that you have access to, you click a link and you get to the Add license form for the selected type. The bundle is pre-selected so the form builder is able to create the entity. There is no need for a separate form, we just use the default inheriting ContentEntityForm - less code to maintain. I think it's straightforward and familiar. Let me know what you think.

There's probably a few more things to work on on the add/edit forms to consider them functional, but it's probably better to address them on separate issues; I'll create new issues if I work on them.

I've also fixed a small typo on the LicenseForm that would break form submission; the license still does not have a label though but again this is a separate concern.

krystalcode’s picture

Status: Active » Needs review
joachim’s picture

Status: Needs review » Needs work

Thanks for the patch. Lots of things cleaned up I see!

However, I think we can do this with less code: AdminHtmlRouteProvider does a bunch of stuff such as declare routes and use a common controller if we define the link templates. See for instance getAddPageRoute():

  protected function getAddPageRoute(EntityTypeInterface $entity_type) {
    if ($entity_type->hasLinkTemplate('add-page') && $entity_type->getKey('bundle')) {
      $route = new Route($entity_type->getLinkTemplate('add-page'));
      $route->setDefault('_controller', EntityController::class . '::addPage');
      $route->setDefault('_title_callback', EntityController::class . '::addTitle');
      $route->setDefault('entity_type_id', $entity_type->id());
      $route->setRequirement('_entity_create_any_access', $entity_type->id());

      return $route;
    }
  }

And getAddFormRoute() does the same.

That removes the need for the controller and at least some of the route declarations, and the theme too -- I don't think we especially need admin UI to be themeable to that extent.

(Sorry, I feel bad for saying we should take out so much code... but it makes the module more maintainable in the long run!)

joachim’s picture

PS. As a follow-up issue, we should add validation somewhere as the product reference field must match a) a product with a license in the first place, and b) a product whose license configuration matches what the admin is trying to create. But that's not a priority -- admins should have some clue of what they're doing! ;)

heddn’s picture

Is this outdated now? I see where a lot of this is already done in the code.

joachim’s picture

Yup, a fair bit of this has been done is separate issues.

heddn’s picture

Status: Needs work » Closed (outdated)

Let's close as outdated then. And we can open up new bugs as we find them then, yes?