I am building a site for a client that is effectively a job board with a messaging system. They want to know who the last person in their office was to view a specific application and to know who/when someone views specific pages/messages.

I am hoping for something like

set x = users.lastvisitor 
if currentuser != users.last vistor
insert currentuser & date into users.lastvisitor 

Can anyone point me in a direction? I am new to Craft so don't know how it handles things like this. I am using matrix blocks for the messages, is it possible to add an entry through a query instead of a form?

  • Which field layout contains the field? Is the field in your user or in the entry? There is actually a really simple way to achieve this – Robin Schambach Aug 28 '18 at 8:58
  • I am storing everything in users. What is the way? – Alex Carpenter Aug 28 '18 at 15:13
  • There is no "best" way. Each approach has advantages and disadvantages, you need to choose the solution that fits your needs – Robin Schambach Aug 28 '18 at 16:22

You can do a quick and dirty work around and just store the element in each request via Twig

{% set lastVisitor = user.lastvisitor.one() %}
{% if currentUser != null and currentUser.id != lastVisitor.id %}
    {# new user -> set the field value of user -> relate the currentUser #}
    {% do user.setFieldValue('lastVisitor', [currentUser.id]) %}
    {# save the element #}
    {% do craft.app.getElements().saveElement(user) %}
    {% if user.hasErrors %}
        {# print errors via user.getErrors() %}
    {% endif %}
{% endif %}

I would not recommend it even if it is possible. Those things are better in PHP than in Twig

| improve this answer | |
  • That is what I was looking for, thank you. Why would you not recommend it? Why are they better in php? – Alex Carpenter Aug 28 '18 at 16:57
  • Because Twig is not supposed to contain too much business logic. The idea to use an MVC pattern is to separate your logic and your templates. With that in mind It's not really clean to mix them together. Error handling and debugging is easier in PHP as well. Imagine someone looks at this project in about 3 years. Experienced developers / experienced Craft developers will always search for the logic in services or links to those functions in variables or controllers, not in templates – Robin Schambach Aug 28 '18 at 17:14
  • Ah good point. I didn't realize Craft was set up to deal with things that way. I thought php was only used for plugins. The few tutorials I have done have done everything in twig which made seem like it was better to keep everything possible in twig and only result to php for plugins for more complicated tasks. – Alex Carpenter Aug 28 '18 at 18:05
  • No most tutorials are about Twig because the minority of people is willing/able to use PHP but that doesn't mean it's the best way. – Robin Schambach Aug 28 '18 at 18:13
  • That is good to know, thank you. I will look more into implementing php in craft. – Alex Carpenter Aug 28 '18 at 18:19

There's a plugin called Entry Count that updates a counter record every time a visitor loads a page. You might be able to adapt that plugin to check to see if the user is logged in, and if so, record their user ID to the database instead of incrementing the hit counter. The value is displayed on the Craft entry list dashboard, you would just want to adapt that to display the username instead of the ID.

| improve this answer | |
  • Is there no simple way to save data without a form in twig? A plugin would require a lot of modifications because it isn't related to specific pages, it is related to users that are dynamically generated. I looked through the plugin and it seems simple enough, but nowhere near as succinct as saving an entry like with a form. I can do it with javascript but I was hoping there was something craft specific I wasn't aware of yet. – Alex Carpenter Aug 28 '18 at 1:03

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.