Problem/Motivation

The 'variables' GiftcardTransaction base field definitions seem very out of place.

'comment' is a string containing replacement characters, like '@order_id' and 'variables' holds the "real" values of these replacement characters (e.g. "123" for order ID 123).

Both fields can not be filled by the user through the GiftcardTransaction form, and they are only used through the "OrderEventSubscriber", which fills them with either:

          'comment' => 'Used on order with id @order_id.',
          'variables' => ['@order_id' => $order->id()],

or

          'comment' => 'Bought @product.',
          'variables' => ['@product' => $purchased_entity->label()],

Steps to reproduce

Proposed resolution

Remove the "variables" base field entirely and discuss whether to keep 'comment' or not.
Let the user add a comment via Form if we want to keep it.

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

grevil created an issue. See original summary.

rhovland’s picture

The comments field is definitely useful. Think of it like how commerce_log works. It keeps a list of transactions with details about what happened. The log can be what order a gift card was used on. It can be a gift card was purchased.

What is missing is when a transaction is added by an admin using the "add transaction" button, the comment is blank. That's not good. The admin should be able to enter a comment about the transaction they were creating.

The transaction entity actually already stores the order id associated with the transaction in reference_id and reference_type so an additional field would be redundant.

I think we can safely drop the variables field and keep what is already being recorded in the comments but doing it the correct way like this:

'comment' => $this->t('Used on order with id @order_id.', ['@order_id' => $order->id()]),
'comment' => $this->t('Bought @product.' ['@product' => $purchased_entity->label()]),

Obviously the code that consumes the comment field will have to be adjusted because it currently combines the comment with the variables such as in getComment().

anybody’s picture

Hi and thanks for the feedback. I'm also unsure if comment is really essential. Still it's not worth a long discussion and I'm fine to keep it, if it helps someone. It's much personal preference and learnings, I also like ways to keep (admin) notes for entities... still it doesn't have to be within the entity itself.

Despite that, I totally agree that variables should be removed. A static string would be fine as comment and can / should be generated at the time of creation.

grevil’s picture

Title: Refactor 'comment' and 'variables' GiftcardTransaction base field definitions » Remove 'variables' GiftcardTransaction base field definition and make "comment" accessible via Form
Issue summary: View changes

Updated the issue summary and title.

rhovland’s picture

Assigned: Unassigned » rhovland

If comments on transactions could happen at a separate time from when the transaction was being created I could see it being apart from the entity. But it is part of the creation of the transaction and it makes sense there. Think of it like a comment on a payment (not a payment method) where it's a snapshot in time of something that happened, with additional relevant information attached to it.

If we added comments for gift cards then it would be inappropriate to put it on the giftcard entity.

I'm going to try to tackle this issue and get some code put together for review.

rhovland’s picture

Please verify if I have done the translation part in the event subscriber correctly. I used commerce core as a guide.

Comment now shows up on the admin form.

Going to write an update hook next.

rhovland’s picture

Status: Active » Needs review

Update hook written.

The update fills in the variables and resaves the comment. Once that is done it removes the variables field from the database. I have to read the variables directly from the database because the field no longer exists in the entity.

I tested this update hook on a copy of our production database and examined the table contents before and after and all comments were correctly updated.

grevil’s picture

Assigned: rhovland » anybody
Status: Needs review » Postponed (maintainer needs more info)

@rhovland thanks for your work on this!

The translation logic is not perfect yet. The "comment" base field is exposed to the user UI, so we can't simply pass a t string as its parameter. The user won't ever be able to do that via UI.

Theoretically, we need to set the comment base field definition translatable and then pass "normal" markup to the field, when using it programmatically.
But I am a bit unsure about this, as we never want to translate the entity itself, but only want to translate the comment. So maybe the current code is the way to do it. But in this case we can't expose the comment field in the form. What do you think @anybody?

EDIT: Note, that I haven't checked the update hook yet.

grevil’s picture

Assigned: anybody » Unassigned
Status: Postponed (maintainer needs more info) » Needs work

Just talked with @anybody internally. He thinks, that comments should generally not get translated in this context (when set by user). Although I feel like this is a special case, where the comment gets hardcoded as well.

So I guess we should use FormattableMarkup instead of $this->t(). Otherwise, this works great! Just looked over the update hook code and tested it. Works great and adjusts the DB and config accordingly. Thanks!

Back to NW for final change.

anybody’s picture

Yeah in general this is just my 2 cents. We should not be too strict with this, it won't harm too much if it's not perfect yet.

rhovland’s picture

Status: Needs work » Needs review

Rebased and changed to using FormattableMarkup

anybody’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @rhovland! If you agree I think this is ready for RTBC! :)

rhovland’s picture

Status: Reviewed & tested by the community » Fixed

I reviewed the code again.
Ensured usage of FormattableMarkup followed the security recommendations in that class' comments.
Tested on a copy of a production site.

Merged.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • rhovland committed 8f684c9e on 2.x
    task: #3518763 Remove 'variables' GiftcardTransaction base field...
grevil’s picture

Nice! I'll release alpha 3.

Status: Fixed » Closed (fixed)

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