Default "Campus" on New Family Block to the Campus of the Person Filling Out the Form

  • By Michael Garrison 2 Years Ago

 This will be a quick post, I just wanted to save a snippet that was recently brainstormed and hatched by a group in the Slack team...

 By default if there's only one campus, Rock will pre-fill that campus on the "New Family" block, logically enough. But once there are multiple campuses, it defaults to blank and you have to set it yourself. The request posed in Slack was to find if there was a way to set a default. The block doesn't have an option built-in for you to set this, so short of doing some development work, we'll have to do that with some JavaScript in the Post-HTML field on the block.

 You could set up a different page for each campus and accomplish it easily enough, by adding the following line to the Post-HTML field of the "New Family" block:

<script>$("select[id$='Campus'] option:eq(2)").prop('selected', true)</script>

(This would pre-select the second campus in the list)

 But that could get messy. Then someone pointed out that it would be nice to default it to the campus of the CurrentPerson (the person registering the new family) since the person doing the registration is probably registering a family for their own campus. That way it kind of self-customizes for each person. Slick!

 Here's a good, reliable way to do that: add the following snippet to the Post-HTML field of the "New Family" block on the built-in page (or any other page(s) you've added with the block on them)

{% assign personCampus = CurrentPerson | Campus %}
$( document ).ready(function() {
$("select[id$='Campus']").val("{{ personCampus.Id }}");

Note that in this case we're selecting based on the VALUE of the person's Campus Id, rather than the order it's listed in the input. This is more reliable in the event that a campus was deleted- if you delete campus #2 out of 3, the campus with Id=3 will be listed in position 2. But the value will be reliable, so that's what we use.


Rock on!

Spark Development Network
Flagstaff, AZ

Michael Garrison recently left his job in Architecture to become one of the "new guys" at Spark (the "Core Team"), but he's still helping out Christ's Church of Flagstaff and other non-profits with tech needs in his off-hours, trying to make computers do what computers do best, so that people are freed to do what we do best: relate with people!