CourseObjectUbercart.php in Course 3.x
File
modules/course_uc/src/Plugin/course/CourseObject/CourseObjectUbercart.php
View source
<?php
namespace Drupal\course_uc\Plugin\course\CourseObject;
use Drupal;
use Drupal\Core\Form\FormStateInterface;
use Drupal\course\Entity\CourseObject;
use function drupal_render;
use function node_view;
use function uc_product_types;
class CourseObjectUbercart extends CourseObject {
public function hasPolling() {
return TRUE;
}
function getTakeType() {
return 'content';
}
function take() {
$user = Drupal::currentUser();
$bypass = Drupal::config('course_uc.settings')
->get('bypass_checkout');
if (!($this
->getNode()->sell_price > 0) && $bypass) {
$this
->getFulfillment($user)
->setComplete(1)
->save();
}
if ($this
->getFulfillment($user)
->isComplete()) {
return "You've already purchased this activity.";
}
else {
$node_view = node_view($this
->getNode());
$atc = t('You must purchase this course before proceeding.') . \Drupal::service('renderer')
->render($node_view['display_price']) . \Drupal::service('renderer')
->render($node_view['add_to_cart']);
if (!$atc) {
return "Something isn't right...";
}
else {
return [
'#markup' => $atc,
];
}
}
}
public function optionsForm(&$form, FormStateInterface $form_state) {
parent::optionsForm($form, $form_state);
$form['node']['instance']['#title'] = t('Product to check');
$form['node']['instance']['#description'] .= '</br>' . t('Use an existing ubercart-enabled product for checking payment. Defaults to this course.');
}
public function optionsSubmit(&$form, FormStateInterface $form_state) {
if (isset($form_state['values']['instance'])) {
if (!$form_state['values']['instance']) {
$form_state['values']['instance'] = $this
->getCourse()
->id();
}
}
parent::optionsSubmit($form, $form_state);
}
public function createInstance($node = NULL) {
if (!$this
->getInstanceId()) {
$this
->setOption('instance', $this
->getCourse()
->id());
}
}
function getNodeTypes() {
return uc_product_types();
}
function getCloneAbility() {
return t('Payment objects will be created as a reference to the new course');
}
public function freeze() {
return TRUE;
}
function thaw($ice) {
return $this
->getCourse()
->id();
}
}