0

I am creating a new content type and one of the fields I d like to add owner.

I ve chosen reference - user as the field type Type of item to reference = user

How do I make the default value the name of the current user?

Many Thanks

2
  • 2
    Content types already has an author field that will automatically populate with the user creating the node. Can't you use that and save creating another field?
    – Leigh
    Apr 26, 2019 at 17:01
  • unfortunately not as I want to keep the created on and have a second one that may be the same or different, would the repopulate module help with this maybe?
    – user71404
    Apr 26, 2019 at 22:28

1 Answer 1

0

You can set default values in a hook_ENTITY_TYPE_create:

/**
 * Implements hook_ENTITY_TYPE_create() for 'node'.
 */
function mymodule_node_create(\Drupal\node\NodeInterface $node) {
  if ($node->getType() == 'my_content_type') {
    $node->field_user->target_id = \Drupal::currentUser()->id();
  }
}

Your Answer

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