Comments

joachim created an issue. See original summary.

Kazanir’s picture

StatusFileSize
new4.9 KB

Start of a patch for this.

joachim’s picture

Status: Active » Needs work
  1. +++ b/commerce_license.workflows.yml
    @@ -0,0 +1,43 @@
    +license_default:
    +  id: license_standard
    

    These two should match. I would go with license_default, as Commerce uses 'order_default'.

  2. +++ b/commerce_license.workflows.yml
    @@ -0,0 +1,43 @@
    +    suspended:
    +      label: Suspended
    +    expired:
    +      label: Expired
    +    revoked:
    +      label: Revoked
    

    I think it would be a good idea to add comments to describe some of these.

    Eg, what's the difference between suspended and revoked? And what does pending signify?

bojanz’s picture

Let's use "new" instead of "created", it's what Commerce went with.

@joachim
Pending is for remote licenses that are awaiting activation (provisioning call in progress).

chrisrockwell’s picture

Does this mean the status field on the license entity goes away, becomes a 0|1, or should be varchar to mirror what state machine would hold?

joachim’s picture

> Does this mean the status field on the license entity goes away, becomes a 0|1, or should be varchar to mirror what state machine would hold?

Good point. I think that for now it should go away. Orders don't have a boolean status field, only their workflow state.

If we find that queries to get active licenses are slow, we can add it back in, but for now let's keep things simple :)

Off the top of my head, I think most queries to fetch licenses don't need to be particularly performant, as licenses get loaded in queue jobs when they need to activate or expire.

joachim’s picture

Status: Needs work » Needs review
StatusFileSize
new5.21 KB

Here's a patch with the various changes from above, and also some docs for the pending state.

This just adds the workflow and the field -- making it do something I think is for over at #2879263: License creation/destruction logic in sync with cart/order lifecycle.

As it's had a few reviews already, I think this is RTBC, and tweaks can come in follow-ups.

joachim’s picture

Status: Needs review » Needs work

Whoaaaa just spotted a few things to fix in this.

joachim’s picture

Status: Needs work » Needs review
StatusFileSize
new5.08 KB

Further changes:

- remove param from License::getWorkflowID()
- add getWorkflowID() to license interface
- remove unrelated addition of getType() to interface: filed #2898618: rename getType() and add it to LicenseInterface for this
- doc tweaks

joachim’s picture

Got a review from bojanz on IRC, made further fixes and tweaks:

- Fixed name of workflow
- remove transitions from pending to suspended and expire
- Added cancel state and transition for pending to go to
- rearranged transition code order in the yml file
- fixed license name in getWorkflowId

And I'm removing this, and filing a new feature request #2898654: add ability to license type plugin instances to specify a license workflow for it:

    // @TODO Make this part of plugin configuration?
    // Gotta think about how flexible this can be or become.

I figure we'll leave the getWorkflowId on the license type plugin base, as the code is there already.

joachim’s picture

Status: Needs review » Fixed

Going to commit this so I can keep going with other issues today...

  • joachim committed fd4e56e on 8.x-2.x authored by Kazanir
    Issue #2894862 by Kazanir, joachim: implement license workflow using...
jurgenhaas’s picture

Status: Fixed » Needs work

Just came across this while working on #2879276: Licensable order item types & fields and think that getWorkflowId can't be static while it has to access an instance of License when using $this. I've looked for other instances doing similar things and found this in the Order entity:

  public static function getWorkflowId(OrderInterface $order) {
    $workflow = OrderType::load($order->bundle())->getWorkflowId();
    return $workflow;
  }

This is working without $this because the order is being passed along as a parameter. I don't know how this is achieved but something similar should be used here as well.

joachim’s picture

You're quite right, getWorkflowId() is used as a callback on the field definition, given as a static callable.

I don't have the code in front of me right now, so not sure how we fix.

+++ b/src/Entity/License.php
@@ -164,10 +171,19 @@ class License extends ContentEntityBase implements LicenseInterface {
+      ->setSetting('workflow_callback', ['\Drupal\commerce_license\Entity\License', 'getWorkflowId']);

Is there a way we can specify this has to pass in the entity? Or maybe state_machine module passes it anyway?

jurgenhaas’s picture

Something similar to this is used by the entity types Order and also Payment. Both are taking an entity as their only parameter so I guess we can assume that it is passed along. Yes, looking into StateItem::loadWorkflow(), that is passing the entity forward.

So, we now need to determine how to find out the correct workflow ID. Is this the workflow ID from the order item type?

joachim’s picture

No, it's the workflow that our license entity wants to use.

Where we had:

    $workflow = $this->getType()->getWorkflowId();

we just need to change $this to the given $license parameter.

jurgenhaas’s picture

Status: Needs work » Needs review
StatusFileSize
new2.02 KB

There is more to it. Chaning it to $license->getType() will then bring us into that getType() function which is also still unclear to me and according to #2898613: license type plugin instances are created with a license name passed in for $config, but nothing uses it we can remove that getType() function and handle everything inside of getWorkflowId().

That leaves the question how to get the license type. As far as I understand it, the license type is a plugin from \Drupal\commerce_license\Plugin\Commerce\LicenseType, so it can either be role or file at this point. So when a license gets created, one of those types/plugins must be associated with the license. Is that the bundle()? If so, the attached patch should solve this issue.

jurgenhaas’s picture

Re-rolled patch with dorgflow

joachim’s picture

Status: Needs review » Needs work

Thanks for the reroll! Patch format looks good now.

Just a few points:

  1. +++ src/Entity/License.php	(revision )
    @@ -89,14 +90,6 @@
    -  /**
    -   * {@inheritdoc}
    -   */
    -  public function getType() {
    -    $license_type_manager = \Drupal::service('plugin.manager.commerce_license_type');
    -    return $license_type_manager->createInstance($this->bundle());
    -  }
    

    We shouldn't be removing this here.

    If getType() was something the previous patch on this issue had added, which we now felt wasn't needed, then fine.

    But it was there before that patch, so removing it is out of this issue's scope.

    I'm going to say that for now, we should assume it's needed for things we don't know about yet, so getWorkflowId() should call it. We can think about removing it later on, probably when we're at beta and the code is reasonably complete, and we can then see whether anything else does call it.

  2. +++ src/Entity/LicenseInterface.php	(revision )
    @@ -38,11 +38,14 @@
    +   * Gets the workflow ID for the state field.
    

    Let's change this to:

    'Callback for the license state field's workflow_callback setting.'

    The docs I wrote here are based on my incorrect understanding.

jurgenhaas’s picture

Regarding 1: the getType function was introduced specifically for this, see #2898613: license type plugin instances are created with a license name passed in for $config, but nothing uses it which we closed in favour of this issue here as we realized that the two things relate to each other. I also cross checked and getType isn't used anywhere else and it hasn't been declared in the interface either such that nobody should even know about it. Of course, the git log shows that it really was only introduced a couple of days ago in this current context.

Regarding 2: will update, let's clarify your comment above first and then I re-roll the patch to address both topics.

joachim’s picture

> the getType function was introduced specifically for this

git blame src/Entity/License.php | ag getType

gets me:

^4439611 (Christian Sieber 2017-04-10 19:52:49 -0600  95)   public function getType() {

and:

git show fd4e56eefdbb2688c3ee0790e43bc5980d21f239 | ag getType
+    $workflow = $this->getType()->getWorkflowId();

gets me:

+    $workflow = $this->getType()->getWorkflowId();

We introduced a call to getType(), but it was already there.

jurgenhaas’s picture

Status: Needs work » Needs review
StatusFileSize
new2.67 KB
new1.81 KB

Agreed, I brought it back and also renamed it and put it into the interface as suggested in #2898618: rename getType() and add it to LicenseInterface

joachim’s picture

Status: Needs review » Fixed
StatusFileSize
new1.61 KB

I'm committing this patch which has the following:

- fix License::getWorkflowId to not access $this in static
- added $license param to LicenseInterface::getWorkflowId()
- added $license param to License::getWorkflowId()
- fixed docblock for LicenseInterface::getWorkflowId()
- added @see to docblock for LicenseInterface::getWorkflowId()

> Agreed, I brought it back and also renamed it and put it into the interface as suggested in #2898618: rename getType() and add it to LicenseInterface

The change to add it to the interface should be done as part of that other issue, not here. It's important to keep patches focussed on just the issue they are trying to fix, and not add in fixes that are unrelated. It helps a great deal when in the future we do the sort of git archaeology I did in comment #21.

  • joachim committed 8ff7d37 on 8.x-2.x authored by jurgenhaas
    Issue #2894862 by joachim, jurgenhaas: Follow-up - implement license...
jurgenhaas’s picture

Status: Fixed » Needs review
StatusFileSize
new900 bytes

OK, that makes sense. I've submitted a patch separately in the other issue.

However, this introduced some small syntax issues in the interface and I've fixed them in the hopefully last patch:

use Drupal\commerce_license\Entity\LicenseInterface;

This uses itself which isn't a great idea and phpcs complaints about it. At the same time I've also removed those that are no longer used:

-use Drupal\Core\Entity\RevisionLogInterface;
-use Drupal\Core\Entity\RevisionableInterface;
-use Drupal\Component\Utility\Xss;
-use Drupal\Core\Url;

Then, the following line has a missing leading backslash which phpcs doesn't like either:

* @see Drupal\state_machine\Plugin\Field\FieldType\StateItem

That's corrected too.

joachim’s picture

Status: Needs review » Fixed

Ah whoops!

Could you file a new issue for that please, as part of that is general cleanup?

jurgenhaas’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.