function course_uc_form_alter in Course 8.2
Same name and namespace in other branches
- 8.3 modules/course_uc/course_uc.module \course_uc_form_alter()
- 6 modules/course_uc/course_uc.module \course_uc_form_alter()
- 7.2 modules/course_uc/course_uc.module \course_uc_form_alter()
- 7 modules/course_uc/course_uc.module \course_uc_form_alter()
- 3.x modules/course_uc/course_uc.module \course_uc_form_alter()
Implements hook_form_alter().
Hide the add to cart button when the user satisfied the requirement.
File
- modules/
course_uc/ course_uc.module, line 8
Code
function course_uc_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, 'uc_product_add_to_cart') !== FALSE) {
global $user;
$node = \Drupal\node\Entity\Node::load($form['nid']['#value']);
if (course_node_is_course($node)) {
$course = course_get_course($node, $user);
// Viewing a course product.
if ($node->sell_price > 0 && !course_uc_check_purchased($user->uid, $node->uid) && !course_uc_payment_is_first($course)) {
// Has a price, not purchased, not the first object.
if (arg(2) != 'course-object') {
$form['#access'] = FALSE;
}
}
else {
if (course_enrollment_check($node
->id(), $user->uid) || !($node->sell_price > 0) && variable_get('course_access_bypass_checkout', 1)) {
// User already enrolled, or course is free and bypass is enabled.
$form['#access'] = FALSE;
}
}
}
elseif ($courseObject = course_get_course_object('payment', $node
->id())) {
// Viewing a product that is part of another course.
$course = course_get_course($courseObject
->getNode());
if ($course) {
foreach ($course
->getObjects() as $courseObject) {
if ($courseObject
->getComponent() == 'payment' && $courseObject
->getInstanceId() == $node
->id()) {
if ($courseObject
->getFulfillment($user)
->isComplete()) {
// User already purchased.
$form['#access'] = FALSE;
}
}
}
}
}
// If there are any other blockers (like date based access) then do not
// display the add to cart button as it would be a method to enroll,
// bypassing the restrictions.
if (course_node_is_course($node)) {
$blockers = course_access_course('enroll', $node, $user, FALSE, TRUE);
foreach ($blockers as $key => $blocker) {
if (!$blocker['success'] && $key != 'course_must_purchase') {
$form['#access'] = FALSE;
}
}
if (!isset($form['#access']) || !empty($form['#access'])) {
$instances = field_info_instances('course_enrollment', $node->course['enrollment_type']);
foreach ($instances as $field_name => $instance) {
$field = field_info_field($field_name);
if (!empty($instance['settings']['course_enrollment_user_field'])) {
// At least one field must be shown when enrolling. Display the user
// enrollment form.
$enrollment = entity_create('course_enrollment', array(
'nid' => $node
->id(),
'uid' => $user->uid,
'type' => $node->course['enrollment_type'],
));
$form['course_enrollment'] = array();
$form['course_enrollment']['#tree'] = TRUE;
$form['course_enrollment']['#parents'] = array(
'course_enrollment',
);
field_attach_form('course_enrollment', $enrollment, $form['course_enrollment'], $form_state);
$form['#submit'] = array(
'uc_product_add_to_cart_form_submit',
'course_uc_add_to_cart_form_submit',
);
break;
}
}
}
}
}
if ($form_id == 'uc_cart_view_form') {
if (variable_get('course_uc_restrict_qty', 1)) {
foreach ($form['items'] as $key => &$item) {
if (is_numeric($key) && isset($item['nid'])) {
$node = \Drupal\node\Entity\Node::load($item['nid']['#value']);
if (course_node_is_course($node)) {
$item['qty']['#disabled'] = TRUE;
}
}
}
}
}
}