course_content.module in Course 8.2
File
modules/course_content/course_content.module
View source
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
function course_content_form_node_type_form_alter(&$form, FormStateInterface $form_state) {
$node = $form_state
->getFormObject()
->getEntity();
$form['course']['course_content_enabled'] = array(
'#title' => t('Use as course content'),
'#type' => 'checkbox',
'#default_value' => $node
->getThirdPartySetting('course_content', 'use'),
'#description' => t('This content type will be used as %course content.', array(
'%course' => 'Course',
)),
);
$form['#entity_builders'][] = 'course_content_form_node_type_edit_form_builder';
}
function course_content_form_node_type_edit_form_builder($entity_type, NodeType $node_type, &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('course_content_enabled')) {
$node_type
->setThirdPartySetting('course_content', 'use', $form_state
->getValue('course_content_enabled'));
return;
}
$node_type
->unsetThirdPartySetting('course_content', 'use');
}
function course_content_node_access(NodeInterface $node, $op, $account) {
if ($op == 'view') {
if (($courseObject = course_get_course_object('content', $node
->id())) && $courseObject
->getOption('private')) {
if ($courseObject
->getFulfillment($account)
->id()) {
return \Drupal\Core\Access\AccessResult::allowed()
->setCacheMaxAge(0);
}
else {
return \Drupal\Core\Access\AccessResult::forbidden()
->setCacheMaxAge(0);
}
}
}
}