Created
September 24, 2014 12:35
-
-
Save chris-jamieson/b2ce50331cef3aee969b to your computer and use it in GitHub Desktop.
Chaplaincy events helper JS walkthrough for Adam
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* campus events (this is custom.js line 71) */ | |
| // first, select each views row on the campus events products view (first view as per email) | |
| $(".view-id-campus_events.view-display-id-page .views-row, .view-id-campus_events_products.view-display-id-page .views-row").each(function(index) { | |
| // examine the current URL.get the page id we are currently on (from the URL). Get only the query parameters (the bits after the "q" in the URL) - NB this means the function will fail if Drupal clean URLs is enabled. | |
| var pathname = window.location.search; | |
| // using the query parameters, get the page number by getting the value of the "page" parameter | |
| var pageNumber = pathname.split('page=')[1]; | |
| // if we haven't found anything, this is the first page (but it's zero-indexed, so it's page 0) | |
| if (typeof pageNumber === 'undefined') { | |
| pageNumber = 0; | |
| } | |
| // now we should get the views-row number | |
| // remember there will only be a max of 4 rows (because the view is paged every 4 rows) | |
| var viewsRowNumber; | |
| if ($(this).hasClass('views-row-1')) { | |
| viewsRowNumber = 1; | |
| } else if ($(this).hasClass('views-row-2')) { | |
| viewsRowNumber = 2; | |
| } else if ($(this).hasClass('views-row-3')) { | |
| viewsRowNumber = 3; | |
| } else if ($(this).hasClass('views-row-4')) { | |
| viewsRowNumber = 4; | |
| } | |
| // so now we know the page number and the row number, and we can use it to figure out which page on the second view we should direct the user to | |
| var pageCounter = ((pageNumber * 4) - 1) + viewsRowNumber; | |
| // armed with all this info, we're ready to rewrite the URL in the "read more" field for this views-row. We're going to take the existing target URL (e.g. "http://mychaplaincy.co.uk/?q=campus-events-products/region/24/university/38" since every views-row has this set exactly the same: link to the second view with the arguments for region and university filled in appropriately) and append some additional query parameters - a dummy "field_date_value" because the user has not had the opportunity to make use of this exposed filter yet (it only exists on the second view) and then the crucial "page" parameter | |
| // prepare the string to append | |
| var stringToAppend = '&field_date_value[value]&page=' + pageCounter; | |
| // append a string to the URL with the appropriate page number | |
| var existingHref = $(this).find('.read-more a').attr('href'); | |
| var newHref = existingHref + stringToAppend; | |
| // finally append the string to the href (i.e. take action in the DOM) | |
| $(this).find('.read-more a').attr('href', newHref); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment