0

This is a decoupled drupal 8 back end with an angular front end connected over jsonapi and other custom restful inputs.

We have a coupon content type in out Drupal csm. It is exactly what it sounds like, a coupon.

We have a content type "products" and you can assign a coupon to that product when creating or editing said product.

We have a taxonomy term list of product categories and sub categories.

Category A
    Sub Category 1
    Sub Category 2
Category B
    Sub Category 3
    Sub Category 4
    Sub Category 5

An admin can go into a taxonomy term, and add a coupon to an individual taxonomy term, or to a product content type.

This is where it gets crazy.

We have a listing of coupons, on that list we want to give the admin the ability to display a "featured product" and ideally, in the CMS they have a field where the only options are either a taxonomy term or a product content type that said coupon has been applied to.

Product A (Product)                  # (content-type)
Product F (Product)                  # (content-type)
Category B (Category)                # (product taxonomy parent (weight 0) term)
subCategory 1 (Sub Category)         # (product taxonomy sub parent (weight 1) term)

only products ( a content type ) or taxonomy terms with this coupon are shown.

We will then need to build a link pointing to the correct front end page. The category list, the sub category list, or the actual product.

Thoughts on how to achieve this with no custom code? If the only way to do this is with custom code, what's the best way to approach the task?

Thanks a million and 1.

[EDIT to change output options.]

0

There are some caveats in your question.

Caveat—no info to pick from

One caveat is marking "featured" items, narrowing down your pick from an already narrowed down list. This means that you have to save a new Coupon twice to get to the Featured option, the first time you save you pick the terms or products, and then you edit again to pick the "featured" one from the list you narrowed down previously. You could probably come up with some Ajax voodoo to do it all in one Save, but that is out of scope here and not very straightforward.

I'll assume the double-save is acceptable, that it fits with your project processes.

Caveat—Views don't mingle entities

To solve this without the custom code you need Views, but Views only ever list one type of entity (unless they're related, which in your care they aren't).

I'll assume it's ok to use two Fields then to mark your "featured", one to list terms, and the other for Products, and you'll have to rely on your admins to only select one of those, treat them like a single field even when it's not. (You ccould combine them with custom code, of course.)

Use Entity reference Views Display

Create a Taxonomy term Views type with Entity reference Display and limit to your Category vocabulary. You'll have to use Parent term Relationship and Contextual filters to show both levels.

Then use the usual Views magic to Contextually filter out only those terms that are shared with the node currently being edited (this is your Coupon node that has already been saved with chosen Categories and Products).

Create a new Entity Reference Field on Coupon CT and make it use the Views you created previously as a Widget.

Then repeat the procedure for Products. Create a Views Display listing Products, but limit Contextually to the Entity reference you're using on your Coupon, so that when editing a Coupon only Products that have already been chosen show up to be selected as "featured". When you have it working create a new ER field on Coupon using that ER Display as a widget.

This question is too complex to answer step by step, try looking at similar answers for each part, maybe here: How do I create a list of Nodes within a list of taxonomy terms, within a node? and Entity Reference widget selctor in view referring same reference

Links

I assume the links you mention are the links to the related Featured term or Product, showing on the Coupon list.

You can get this easily in Views by adding a Relationship (on the Coupon Views) to the ER "Featured" term, and then "Taxonomy Name" Field that uses that Relationship to show the related Category link.

Repeat similarly to show related "Featured" Product: add the ER "Featured" Product Relationship, and then "Title" Field that uses that Relationship. It will show the "featured" Product (the optin to make it a link instead of plain text are inside the Field settings).

Only custom code will give polished experience

This is perhaps a bit crude but you can always polish with custom code, in various steps along the way, or even the entire set of features, depending on your needs and resources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.