You are here

function course_uc_form_alter in Course 3.x

Same name and namespace in other branches
  1. 8.3 modules/course_uc/course_uc.module \course_uc_form_alter()
  2. 8.2 modules/course_uc/course_uc.module \course_uc_form_alter()
  3. 6 modules/course_uc/course_uc.module \course_uc_form_alter()
  4. 7.2 modules/course_uc/course_uc.module \course_uc_form_alter()
  5. 7 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 19

Code

function course_uc_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'uc_product_add_to_cart') !== FALSE) {

    // We need to toggle the display of this button based on things like
    // enrollment, order, and cart status.
    \Drupal::service('page_cache_kill_switch')
      ->trigger();
    $config = Drupal::config('course_uc.settings');
    $user = Drupal::currentUser();
    $node = Node::load($form['nid']['#value']);
    if (course_node_is_course($node)) {
      $course = course_get_course($node, $user);

      // Viewing a course product.
      if ($node
        ->get('price')->value > 0 && !course_uc_check_purchased($user, $node) && !course_uc_payment_is_first($course)) {

        // Has a price, not purchased, not the first object.
        if (node_is_page($node)) {
          $form['#access'] = FALSE;
        }
      }
      else {
        if ($course
          ->isEnrolled($user) || !($node
          ->get('price')->value > 0) && $config
          ->get('bypass_checkout', 1)) {

          // User already enrolled, or course is free and bypass is enabled.
          $form['#access'] = FALSE;
        }
      }
    }
    elseif ($courseObject = course_get_course_object('ubercart', $course
      ->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() == 'ubercart' && $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 = \Drupal::moduleHandler()
        ->invokeAll('course_access', [
        'entity' => $course,
        'operation' => 'enroll',
        'account' => $user,
      ]);
      foreach ($blockers as $key => $blocker) {
        if ($blocker
          ->isForbidden() && $key != 'course_must_purchase') {
          $form['#access'] = FALSE;
        }
      }
      if (false && !isset($form['#access']) || !empty($form['#access'])) {

        /* @todo fix enrollment field integration with uc */
        $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 = \Drupal::service('entity_type.manager')
              ->getStorage('course_enrollment')
              ->create(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 (Drupal::config('course_uc.settings')
      ->get('restrict_qty', 1)) {
      foreach (Element::children($form['items']) as $key) {
        if (!empty($form['data'][$key]['nid']['#value'])) {
          $nid = $form['data'][$key]['nid']['#value'];
          $node = Node::load($nid);
          if (course_node_is_course($node)) {
            $form['items'][$key]['qty']['#disabled'] = TRUE;
          }
        }
      }
    }
  }
}