2

I'm sure I've seen this documented somewhere but can't for the life of me find it. After login I need to check the user's profile for an empty field (e.g. phone number) and redirect them to a reminder to complete that field if empty. I guess this can be done via an intermediary page but I'm sure I saw a better solution somewhere. Any help?

1 Answer 1

2

If you have a front end form for user registration then you should mark the fields as required in the CMS, you can then make use of Crafts form validation.

I had a project where we imported user data that didn't contain all required fields so needed to redirect users, after they signed in for the first time, to their profile page if data was missing.

I achieved this using a Plugin that contains a service that set a "status" for the current user. The service checked for several different fields and their values and also checked that the user had entered all required fields.

public function setUserStatus( $user ){
    // Get whether user has agreed to current years terms
    $userAgreedToTerms = ( $user->getContent()->agreedCurrentYearsRules == 1 );

    // Use craft validate() function to find ask if user has completed all required fields in profile
    $userHasCompletedProfile = $user->validate() && craft()->content->validateContent( $user );

    if ( $userAgreeToTerms ){
      $userStatus = 'AgreedToTerms';
    }
}

I then used the results of these checks to set a cookie with the "status" of the user.

// Sets a cookie which is used to determine where the user should be re-directed to
$cookie = new HttpCookie( 'userStatus', $userStatus, [ 'expire' => time() + 10 ] );
craft()->request->getCookies()->add( $cookie->name, $cookie );

The function was run from the template using:

{{ craft.pluginName.setUserStatus( currentUser ) }}

And the result of the check was used to redirect the user to the template I needed.

{% set userStatus = craft.request.getCookie( 'userStatus' ) %}

{% if ( userStatus == 'needsToPay' ) and ( site_area != 'payment' ) %}
  {% redirect 'payment' %}
{% endif %}

Hopefully this provides you with enough to create something that works for your case.

1
  • That's really helpful Luke, thanks very much!
    – Ian Ebden
    May 24, 2016 at 18:13

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.