Commerce License Pay to Publish

Image

See: https://www.drupal.org/sandbox/dangreenman/3075767

This module provides a License type plugin for enabling the purchase of license options during content creation, which limit viewer access based on role or user id.

Access control via acl's are defined in a hidden permissions_by_term taxonomy field controlled by the purchased license.

Viewer access restrictions remain independent from normal content publishing controls. The content creator can publish or unpublish their content as per usual regardless of licensing status, which is summarised on the content editing form along with relist options.

Alternative cart and checkout views are included for displaying purchased license information during checkout.

Installation

Install as you would normally install a contributed Drupal module. Visit https://www.drupal.org/docs/8/extending-drupal-8/installing-drupal-8-mo… for further information.

Dependencies

Depends upon the Permissions by Term module to manage acl access control on the target content via a taxonomy field controlled by the license.

Configuration

  1. Add at least two taxonomy terms defining your desired access control options.
    (Go to admin/structure/taxonomy/manage/pay_to_publish/overview).

    Suggested configuration: Define two options: One as only accessible to the content creator and site administrators (Unpublished) and the other accessible to all site users (Published).

    1. Click 'Add term'.
    2. Set the Name to 'Unpublished'.
    3. Only check Allowed roles: Administrator.
    4. Set a second term with the Name 'Published'.
    5. Check all Allowed roles: Anonymous user, Authenticated user, Administrator.
  2. Add a store if you have not already done so.
    (Go to /store/add).
  3. Add products using the 'Pay to publish' product type. A Title of 'Pay to publish' is recommended. Ensure your new product type is published. (Go to /admin/commerce/products).
  4. Edit your product and add product variations. (Go to /admin/commerce/products).
    1. In the product variations settings select the 'Pay to Publish' License Type.
    2. Leave the machine field name on target entity as the default option (field_pay_to_publish), unless you would like this product License to control a different field on the target content-type.

      This field is used to reference the 'permissions_by_term' taxonomy vocabulary suggested above or another field of your choosing.

      Be careful, your site will fail if this field does not exist or is set incorrectly. Look it up under Structure->Content types if unsure.

    3. Leave the value to set on target field as the default option '2', unless you have reason for an alternative. This is the value this license will set upon the target entity field while active. For the suggested taxonomy defined above this will set the field to option '2' from the taxonomy list (Published).

      If the license is revoked or cancelled, this field will be set to the default value defined by the target entity type. We will be defining this a 1 (Unpublished).

      Be careful, your site will fail if an incompatible value is specified. eg. You must specify the index value for taxonomy list options, not the label.

    4. Select 'Rolling interval' for the License Expiration.
    5. Choose you desired interval before the license expires. ie. 1 week.
    6. Ensure your variation is published and click 'Save'.
      Rinse and repeat creating more product variation until all of your desired listing options are defined.
  5. Add a content type to be controlled by a Pay to Publish license. Define as you normally would.
    1. Under the 'Commerce Pay to Publish' settings, select the available product license options available for purchase on this content type.
    2. Click 'Save and manage fields'. Then click 'Add field'.
    3. 'Entity Reference: field_pay_to_publish' is already available under 'Re-use an existing field', select that. Click 'Save and continue'.

      -Or-

      Select a new field type of Reference Content. Set the Label to 'Pay to publish'.
      Fields defined with a machine name of 'field_pay_to_publish' are hidden by default and only made visible to site administrators.

      Select 'Taxonomy term' for 'Type of item to reference' and set the allowed number of values to '1'. Click 'Save field settings'

    4. Enter a label. ie. 'Pay to publish' and select 'Required field'.
    5. Set the Default Value to 'Unpublished'.
    6. Select 'Default' for your Reference method and select the 'Pay to publish' vocabulary. Click 'Save settings'.
    7. Click on 'Manage form display' and move the 'Pay to publish' field to under the title, so it will appear for administrators alongside the Product list options. Disable the field under 'Manage display' so it is not visible to users.
  6. Go to /admin/people/permissions and under 'Commerce License Pay to Publish' set your desired administrator access.

    • 'Relist Commerce Pay to Publish' enables the chosen role to relist content by purchasing a new list option.
    • 'Administer Commerce Pay to Publish' allows administrators to skip the checkout when creating licensed content-types.

Your site is now ready to provide purchasable licensed content.
Go to /node/add and select your new licensed content type for publication.

Acknowledgements

Concept inspired by (and some code ported from):

Drupal 7: commerce_node_checkout
(https://www.drupal.org/project/commerce_node_checkout)

Drupal 8: commerce_license_entity_field
(https://www.drupal.org/project/commerce_license_entity_field)