2

If there is a unlimited user text field named "user_log_field", How can I add a new value for it?

I'm planning to log the IP and location for each user every time they login. Is using an unlimited user text field a good idea for this purpose?

3
  • Hi, As far as I can understand you would like to implement some kind of statistics on your website for authenticated users. And you want to keep a record somewhere in your database about that ? Could you please rephrase the question ? Regards, Oleg Commented Mar 13, 2017 at 10:04
  • Hi Oleg Videnov, Sorry for my broken English, and thanks for your reply. I'm planning to keep record the IP and location for some special users. I want to use an unlimited user text field to record it. Every time the user login, I add a new value (which value is his IP and location) for this multi-value field. I just I don't know how to add a new value for the multi-value field that is mainly I'm asking. Thanks a lot!
    – user71719
    Commented Mar 14, 2017 at 3:33
  • I understand. I will try to prototype some snippet for you if I have some time left tomorrow and will share. Regards Commented Mar 14, 2017 at 17:36

2 Answers 2

4

Architectually speaking, it would be much better if you use custom table for this functionality.You can take a look for inspiration (or even use) the following module:

https://www.drupal.org/project/uiplog

From the description of the project: User IP Log is a simple module that logs the IP address of users on login event to table {uiplog}.

Updated answer: Since you want to do it yourself. You can implement a hook_user_login in your custom module. https://api.drupal.org/api/drupal/core%21modules%21user%21user.api.php/function/hook_user_login/8.2.x

Inside you can just paste the following code.

Assumptions/ notes :

  • This is simple text field is from the type: "Text (plain)". If you choose any other option I am not sure if this snippet would work for you.
  • The machine name of the field is field_field_1. Not the best name but you could quite easily change that on line 5.

I have added quite a few sanity checks that I think would be helpful on the long run.

// Load the current user.
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());

// The machine name of the field.
$field_name = 'field_field_1';
// Sanity check against the machine name of the field.
if ($user->hasField($field_name)) {
  $field_config = \Drupal\field\Entity\FieldStorageConfig::loadByName('user', $field_name);
  // Checks if has unlimited cardinality, otherwise the appendItem will throw
  // weird error.
  if ($field_config->getCardinality() == '-1') {
    // This is a fancy way to return $_SERVER['REMOTE_ADDR'].
    // Keep in mind that on localhost, this will return ::1.
    $a = \Drupal::request()->getClientIp();
    $user->$field_name->appendItem(\Drupal::request()->getClientIp());
  }
  $user->save();
}

Regards,

Oleg

3
  • Thanks you so much! But I want to make it by myself which easier to work with other models.
    – user71719
    Commented Mar 14, 2017 at 10:50
  • Please, let me know if the following solution works for you. Best regards, Oleg Commented Mar 16, 2017 at 11:29
  • @DHH, I have updated the answer, could you tell me if that works for you. Commented Mar 16, 2017 at 15:12
0

An unlimited user text field would be best in my opinion. The way I would implement it would be to write a hook to alter the user login form. In this hook you would assign an IP address to your user field and save it.

Edit: This is the best option, in the absence of a module wich already provides this functionality. If a module already exists, use it.

3
  • I don't know about this. We should be very wary of attaching fields to the user account entity as it is loaded often, and could cause performance issues for logged in users. In drupal 7, I would say Message module and Rules module is a site builder way to log that information, but Oleg's answer above is probably better than that.
    – mradcliffe
    Commented Mar 13, 2017 at 11:51
  • Hey @Kevin Nowaczyk question is, how to add a new value for the unlimited user text field programmatically?
    – user71719
    Commented Mar 14, 2017 at 3:28
  • drupal.stackexchange.com/questions/135597/… Commented Mar 14, 2017 at 14:44

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.