0

I'm looking to update the author_uid for an Entity Registration in Drupal 7 programatically. I want to replace all anonymous users with the UID of 0 with another UID lets say 2 upon saving the registration. I have tried three different solutions thus far to no avail.

Solution 1) Update the author_uid in the $form_state prior to saving the registration form.

<?php
function registration_form_alterations_form_alter(&$form, &$form_state, $form_id) {

    if ($form_id == "registration_form") {
        dpm($form);
        dpm($form_state);
        $form['#submit'][] = 'registration_form_alterations_custom_submit';

    }
}

function registration_form_alterations_custom_submit($form, &$form_state) {
    //  submit handler code
    dpm($form_state);
}

This didn't work because author_uid isn't available to be altered in form_state to the best of my knowledge.

Solution 2) Update via hook_entity_presave. I'm not sure this will work because I can't seem to isolate the actual registration entity to update the author_id in hook_entity_presave.

Solution 3) Use rules... Which doens't allow you to update the author_uid. A rules message screen comes back with a message about the value not being writable/editable.

Does anyone have any additional thoughts on this. Sorry I know its a bit of a weird request, but we have a user that wants anonymous registrations reassigned to them so that they can manage additional details associated with the registrations.

  • (2) seems the best way to go; could you elaborate on the problem(s) you faced there? – Stefanos Petrakis Nov 24 '18 at 20:59
1

(a) hook_entity_presave() should work, giving you access to the entity, before it is persisted, e.g.:

/**
 * Implements hook_entity_presave().
 */
function registration_alterations_entity_presave($entity, $type) {
  if ($type === 'registration' && $entity->author_uid === 0)) {
    $entity->author_uid = variable_get('registration_alterations_anonymous_registrations_reassign_uid');
  }
}

(b) Alternatively, you could use hook_field_attach_submit(), which is also triggered inside registration_form_submit() and would give you access to the submitted form in case you needed it. This would look similar to (a):

/**
 * Implements hook_field_attach_submit().
 */
function registration_alterations_field_attach_submit($entity_type, $entity, $form, &$form_state) {
  if ($entity_type === 'registration' && $entity->author_uid === 0)) {
    $entity->author_uid = variable_get('registration_alterations_anonymous_registrations_reassign_uid');
  }
}

Good luck!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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