0

Assume that I've created a view that outputs a list of cars. For each car, I display the name and the age of the car. This is all done with the Drupal UI. Now I'd like to add a simple button (Set as favorite) at the end of each line that when clicked saves the ID of the chosen car in a user's session variable. So my view table should look like the following:

Name Age Button
car1 23  'set as favorite'
car2 10  'set as favorite'

I am not asking about the session-variable part, but only how I can alter the view output to add a simple button to each row of the list output (I bet it'll be a form in each row).

I've done a lot of reading and I bet this could be done with hook_views_data_alter or some other hook_views_* but I'm not proceeding. I've already tried drop-buttons as suggested on Drupal 8 Add custom action buttons to a view, but that does not solve the things I need. I need the car's ID I want to save in a session variable, so basically I need a form functionality.

1 Answer 1

0

You probably can do this with just a Global custom text field and some hidden view fields.

E.g.:

  • Add car id to the view but exclude from view.
  • Move this to the top of the list.
  • Add a global custom text field for your Favorite button using the replacement values. Could be something like:

    <a class="btn btn-success" href="/add/favorite/{{ carId }}?destination=/view/cars">Add as Favorite</a>

Notes:

Classes are bootstrap specific.

Href is the code to set the favorite.

Destination string is Drupal way to circle back to view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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