LPManagedLink.php in Opigno Learning path 8
File
src/Entity/LPManagedLink.php
View source
<?php
namespace Drupal\opigno_learning_path\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\group\Entity\Group;
class LPManagedLink extends ContentEntityBase {
public static function createWithValues($learning_path_id, $parent_content_id, $child_content_id, $required_score = 0) {
$values = [
'learning_path_id' => $learning_path_id,
'parent_content_id' => $parent_content_id,
'child_content_id' => $child_content_id,
'required_score' => $required_score,
];
return parent::create($values);
}
public function getLearningPathId() {
return $this
->get('learning_path_id')->target_id;
}
public function setLearningPathId($learning_path_id) {
$this
->set('learning_path_id', $learning_path_id);
return $this;
}
public function getLearningPath() {
return $this
->get('learning_path_id')->entity;
}
public function setLearningPath(Group $learning_path) {
if ($learning_path
->getGroupType()
->id() != 'learning_path') {
return FALSE;
}
$this
->setLearningPathId($learning_path
->id());
return $this;
}
public function getParentContentId() {
return $this
->get('parent_content_id')->target_id;
}
public function setParentContentId($parent_content_id) {
$this
->set('parent_content_id', $parent_content_id);
return $this;
}
public function getParentContent() {
return $this
->get('parent_content_id')->entity;
}
public function setParentContent(LPManagedContent $parent_content) {
$this
->setParentContentId($parent_content
->id());
return $this;
}
public function getChildContentId() {
return $this
->get('child_content_id')->target_id;
}
public function setChildContentId($child_content_id) {
$this
->set('child_content_id', $child_content_id);
return $this;
}
public function getChildContent() {
return $this
->get('child_content_id')->entity;
}
public function setChildContent(LPManagedContent $child_content) {
$this
->setChildContentId($child_content
->id());
return $this;
}
public function getRequiredScore() {
return $this
->get('required_score')->value;
}
public function setRequiredScore($required_score) {
$this
->set('required_score', $required_score);
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['learning_path_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel('Learning Path')
->setDescription('The learning path containing this link')
->setCardinality(1)
->setSetting('target_type', 'group')
->setSetting('handler_settings', [
'target_bundles' => [
'learning_path' => 'learning_path',
],
]);
$fields['parent_content_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel('The parent learning path content')
->setDescription('The parent learning path content')
->setCardinality(1)
->setSetting('target_type', 'learning_path_content');
$fields['child_content_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel('The child learning path content')
->setDescription('The child learning path content')
->setCardinality(1)
->setSetting('target_type', 'learning_path_content');
$fields['required_score'] = BaseFieldDefinition::create('integer')
->setLabel('Required score')
->setDescription('The required score to go from parent to child content');
return $fields;
}
public static function loadByProperties($properties) {
return \Drupal::entityTypeManager()
->getStorage('learning_path_link')
->loadByProperties($properties);
}
}