View source
<?php
namespace Drupal\opigno_module\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\RevisionableContentEntityBase;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Entity\Group;
use Drupal\group\Entity\GroupInterface;
use Drupal\opigno_group_manager\OpignoGroupContext;
use Drupal\opigno_learning_path\Entity\LPModuleAvailability;
use Drupal\opigno_learning_path\LearningPathAccess;
use Drupal\user\UserInterface;
use Drupal\user\Entity\User;
class OpignoModule extends RevisionableContentEntityBase implements OpignoModuleInterface {
use EntityChangedTrait;
protected $userAttempts = [];
protected $userActiveAttempt = [];
protected $userTrainingActiveAttempt = [];
protected $activities = [];
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'uid' => \Drupal::currentUser()
->id(),
];
}
public function getName() {
return $this
->get('name')->value;
}
public function setName($name) {
$this
->set('name', $name);
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getOwner() {
return $this
->get('uid')->entity;
}
public function getOwnerId() {
return $this
->get('uid')->target_id;
}
public function setOwnerId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
public function isPublished() {
return (bool) $this
->getEntityKey('status');
}
public function setPublished($published) {
$this
->set('status', $published ? TRUE : FALSE);
return $this;
}
public function getRandomization() {
return $this
->get('randomization')->value;
}
public function getBackwardsNavigation() {
return (bool) $this
->get('backwards_navigation')->value;
}
public function getAllowResume() {
return (bool) $this
->get('allow_resume')->value;
}
public function getRandomActivitiesCount() {
return $this
->get('random_activities')->value;
}
public function setRandomActivitiesCount($value) {
$this
->set('random_activities', $value);
return $this;
}
public function getRandomActivityScore() {
return $this
->get('random_activity_score')->value;
}
public function getSkillsActive() {
return $this
->get('skills_active')->value;
}
public function getModuleSkillsGlobal() {
return $this
->get('module_global')->value;
}
public function getTargetSkill() {
return $this
->get('skill_target')->value;
}
public function setRandomActivityScore($value) {
$this
->set('random_activity_score', $value);
return $this;
}
public function getModuleImage() {
$media = $this
->get('module_media_image')->entity;
if ($media) {
return $media
->get('field_media_image')->entity;
}
else {
return NULL;
}
}
public function getHideResults($answer = NULL) : bool {
$account = \Drupal::currentUser();
if ($account
->hasPermission('view module results')) {
return FALSE;
}
$result = (bool) $this
->get('hide_results')->value;
if (!$answer instanceof OpignoAnswer) {
return $result;
}
$user_module_status = $answer
->getUserModuleStatus();
if ($user_module_status instanceof UserModuleStatus && $user_module_status
->hasField('learning_path') && !empty($group = $user_module_status
->get('learning_path')->target_id) && LearningPathAccess::memberHasRole('user_manager', $account, $group)) {
return FALSE;
}
return $result;
}
public function getResultsOptions() {
$connection = \Drupal::service('database');
$select_query = $connection
->select('opigno_module_result_options', 'omro')
->fields('omro')
->condition('module_id', $this
->id())
->condition('module_vid', $this
->getRevisionId());
$options = $select_query
->execute()
->fetchAll();
return $options;
}
public function insertResultsOptions(FormStateInterface $form_state) {
$connection = \Drupal::service('database');
$insert_query = $connection
->insert('opigno_module_result_options')
->fields([
'module_id',
'module_vid',
'option_name',
'option_summary',
'option_summary_format',
'option_start',
'option_end',
]);
$form_values = $form_state
->getValues();
foreach ($form_values['results_options'] as $option) {
if (!empty($option['option_name'])) {
if (is_array($option['option_summary'])) {
$option['option_summary_format'] = $option['option_summary']['format'];
$option['option_summary'] = $option['option_summary']['value'];
}
$insert_query
->values([
'module_id' => $this
->id(),
'module_vid' => $this
->getRevisionId(),
'option_name' => $option['option_name'],
'option_summary' => $option['option_summary'],
'option_summary_format' => $option['option_summary_format'],
'option_start' => $option['option_start'],
'option_end' => $option['option_end'],
]);
}
}
$insert_query
->execute();
}
public function updateResultsOptions(FormStateInterface $form_state) {
$connection = \Drupal::service('database');
$connection
->delete('opigno_module_result_options')
->condition('module_id', $this
->id())
->condition('module_vid', $this
->getRevisionId())
->execute();
$this
->insertResultsOptions($form_state);
}
public function checkModuleAvailability() {
$availability = [
'open' => TRUE,
'message' => '',
];
$module_availability = 0;
$group_id = OpignoGroupContext::getCurrentGroupId();
$lp_module_availability = LPModuleAvailability::loadByProperties([
'group_id' => $group_id,
'entity_id' => $this
->id(),
]);
if ($lp_module_availability) {
$lp_module_availability = current($lp_module_availability);
$module_availability = $lp_module_availability
->getAvailability();
}
if ($module_availability) {
$open_date = $lp_module_availability
->getOpenDate();
$close_date = $lp_module_availability
->getCloseDate();
if ($open_date && $close_date) {
$quiz_open = \Drupal::time()
->getRequestTime() >= $open_date;
$quiz_closed = \Drupal::time()
->getRequestTime() >= $close_date;
}
if (isset($quiz_open) && isset($quiz_closed)) {
if (!$quiz_open || $quiz_closed) {
$message = '';
$config = \Drupal::config('opigno_module.settings');
if ($quiz_closed) {
$message = $config
->get('availability_closed_message');
}
elseif (!$quiz_open) {
$message = $config
->get('availability_unavailable_message');
}
if (\Drupal::moduleHandler()
->moduleExists('token')) {
$message = \Drupal::token()
->replace($message, [
'opigno_module' => $this,
]);
}
$availability = [
'open' => FALSE,
'message' => $message,
];
}
}
}
return $availability;
}
public function getModuleAttempts(AccountInterface $user, $range = NULL, $latest_cert_date = NULL, $finished = FALSE) {
$key = $this
->id() . '_' . $user
->id();
$key_base = $key;
if (isset($range)) {
$key .= '_' . $range;
}
if (array_key_exists($key, $this->userAttempts)) {
return $this->userAttempts[$key];
}
$status_storage = static::entityTypeManager()
->getStorage('user_module_status');
$query = $status_storage
->getQuery();
$query
->condition('module', $this
->id())
->condition('user_id', $user
->id());
if ($finished) {
$query
->condition('finished', 0, '>');
}
if ($latest_cert_date) {
$query
->condition('started', $latest_cert_date, '>=');
}
$status_ids = $query
->execute();
if ($status_ids) {
$status_entities = $status_storage
->loadMultiple($status_ids);
$this->userAttempts[$key_base] = $status_entities;
$max_id = max(array_keys($status_ids));
$this->userAttempts[$key_base . '_last'] = [
$max_id => $status_entities[$max_id],
];
$max_score = 0;
$best_entity = FALSE;
foreach ($status_entities as $entity) {
if ($entity
->getScore() >= $max_score) {
$max_score = $entity
->getScore();
$best_entity = $entity;
}
}
if (!$best_entity) {
$this->userAttempts[$key_base . '_best'] = [];
}
else {
$this->userAttempts[$key_base . '_best'] = [
$best_entity
->id() => $best_entity,
];
}
}
else {
$this->userAttempts[$key_base . '_best'] = [];
$this->userAttempts[$key_base . '_last'] = [];
}
return isset($this->userAttempts[$key]) ? $this->userAttempts[$key] : [];
}
public function getModuleActiveAttempt(AccountInterface $user, $activity_link_type = NULL) {
$key = $this
->id() . '_' . $user
->id();
if ($activity_link_type == 'flow') {
if (array_key_exists($key, $this->userActiveAttempt)) {
return $this->userActiveAttempt[$key];
}
OpignoGroupContext::removeActivityLinkType();
}
else {
if (array_key_exists($key, $this->userActiveAttempt) && !empty($this->userActiveAttempt[$key])) {
return $this->userActiveAttempt[$key];
}
elseif (!empty($this->userAttempts[$key . '_last'])) {
$last_attempt = reset($this->userAttempts[$key . '_last']);
$this->userActiveAttempt[$key] = $last_attempt;
return $last_attempt;
}
}
$status_storage = static::entityTypeManager()
->getStorage('user_module_status');
$query = $status_storage
->getQuery();
$module_statuses = $query
->condition('module', $this
->id())
->condition('user_id', $user
->id())
->condition('finished', 0)
->range(0, 1)
->execute();
$this->userActiveAttempt[$key] = !empty($module_statuses) ? $status_storage
->load(key($module_statuses)) : NULL;
return $this->userActiveAttempt[$key];
}
public function getBestScore(User $user) {
$user_attempts = $this
->getModuleAttempts($user, 'best');
$best_score = 0;
foreach ($user_attempts as $user_attempt) {
$actual_score = $user_attempt
->getAttemptScore();
if ($actual_score > $best_score) {
$best_score = $actual_score;
}
}
return $best_score;
}
function getUserScore(AccountInterface $account, $latest_cert_date = NULL) {
$which_score_keep = $this
->getKeepResultsOption();
$attempts = $this
->getModuleAttempts($account, 'last', $latest_cert_date);
if (!$attempts) {
return 0;
}
$last_attempt = end($attempts);
$score = 0;
switch ($which_score_keep) {
case 'newest':
$score = (int) $last_attempt
->getScore();
break;
case 'best':
case 'all':
$score = (int) $last_attempt
->calculateBestScore($latest_cert_date);
break;
}
$score = max(0, $score);
$score = min(100, $score);
return $score;
}
public function getTrainingActiveAttempt(AccountInterface $user, Group $group) {
$key = $group
->id() . '_' . $user
->id();
if (array_key_exists($key, $this->userTrainingActiveAttempt)) {
return $this->userTrainingActiveAttempt[$key];
}
$status_storage = static::entityTypeManager()
->getStorage('user_lp_status');
$query = $status_storage
->getQuery();
$training_statuses = $query
->condition('gid', $group
->id())
->condition('uid', $user
->id())
->condition('finished', 0)
->range(0, 1)
->execute();
$this->userTrainingActiveAttempt[$key] = !empty($training_statuses) ? $status_storage
->load(key($training_statuses)) : NULL;
return $this->userTrainingActiveAttempt[$key];
}
public function getModuleActivities($full = FALSE) {
if (empty($this->activities)) {
$db_connection = \Drupal::service('database');
$query = $db_connection
->select('opigno_activity', 'oa');
$query
->fields('oafd', [
'id',
'vid',
'type',
'name',
'usage_activity',
'skills_list',
'skill_level',
]);
$query
->fields('omr', [
'activity_status',
'weight',
'max_score',
'auto_update_max_score',
'omr_id',
'omr_pid',
'child_id',
'child_vid',
]);
$query
->addJoin('inner', 'opigno_activity_field_data', 'oafd', 'oa.id = oafd.id');
$query
->addJoin('inner', 'opigno_module_relationship', 'omr', 'oa.id = omr.child_id');
$query
->condition('oafd.status', 1);
$query
->condition('omr.parent_id', $this
->id());
if ($this
->getRevisionId()) {
$query
->condition('omr.parent_vid', $this
->getRevisionId());
}
$query
->condition('omr_pid', NULL, 'IS');
$query
->orderBy('omr.weight');
$query
->orderBy('omr.omr_id');
$result = $query
->execute();
foreach ($result as $activity) {
$this->activities[$activity->id] = $activity;
}
}
if ($full && !empty($this->activities)) {
$activity_ids = [];
foreach ($this->activities as $activity) {
$activity_ids[$activity->id] = $activity->id;
}
return OpignoActivity::loadMultiple($activity_ids);
}
return $this->activities;
}
public function getSuitableActivities($current_skills) {
if (empty($current_skills)) {
return [
0 => 0,
];
}
$activities = [];
$db_connection = \Drupal::service('database');
$query = $db_connection
->select('opigno_activity', 'oa');
$query
->fields('oafd', [
'id',
'vid',
'type',
'name',
'usage_activity',
'skills_list',
'skill_level',
]);
$query
->addJoin('inner', 'opigno_activity_field_data', 'oafd', 'oa.id = oafd.id');
$query
->condition('oafd.status', 1);
$query
->condition('oafd.skills_list', $current_skills, 'IN');
$query
->condition('oafd.usage_activity', 'global');
$result = $query
->execute();
foreach ($result as $activity) {
$activities[$activity->id] = $activity;
}
return $activities;
}
public function userAnswers(AccountInterface $user, UserModuleStatusInterface $attempt) {
$answers_storage = static::entityTypeManager()
->getStorage('opigno_answer');
$query = $answers_storage
->getQuery();
$answers = $query
->condition('module', $this
->id())
->condition('user_id', $user
->id())
->condition('user_module_status', $attempt
->id())
->execute();
return !empty($answers) ? $answers_storage
->loadMultiple($answers) : [];
}
public function getRandomActivity(UserModuleStatusInterface $attempt) {
$activities = $this
->getModuleActivities();
$activities_storage = static::entityTypeManager()
->getStorage('opigno_activity');
$randomization = $this
->getRandomization();
$random_count = $this
->getRandomActivitiesCount();
$answered_random = 0;
$user_answers = $this
->userAnswers(\Drupal::currentUser(), $attempt);
if (!empty($user_answers)) {
foreach ($user_answers as $answer) {
$answer_activity = $answer
->getActivity();
if (isset($activities[$answer_activity
->id()])) {
if ($randomization == 2) {
$answered_random++;
}
unset($activities[$answer_activity
->id()]);
}
}
}
if ($randomization == 2) {
$assigned_random = $activities;
$activities = [];
if (!empty($assigned_random) && $random_count > $answered_random) {
$required_random = $random_count - $answered_random;
$random_activities = array_rand($assigned_random, $required_random);
if (is_array($random_activities)) {
foreach ($random_activities as $random_activity) {
$activities[$random_activity] = $assigned_random[$random_activity];
}
}
else {
$activities[$random_activities] = $assigned_random[$random_activities];
}
}
}
return !empty($activities) ? $activities_storage
->load(array_rand($activities, 1)) : FALSE;
}
public function getKeepResultsOption() {
$keep_results_options = [
0 => 'best',
1 => 'newest',
2 => 'all',
];
$option = $this
->get('keep_results')->value;
return $keep_results_options[$option];
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Module entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Module entity.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setRequired(TRUE)
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue(NULL)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['module_media_image'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Image'))
->setDescription(t('Set here a module image'))
->setRevisionable(TRUE)
->setTranslatable(FALSE)
->setRequired(FALSE)
->setSetting('target_type', 'media')
->setSetting('handler', 'default')
->setSetting('handler_settings', [
'target_bundles' => [
'image',
],
])
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'media_thumbnail',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_browser_entity_reference',
'weight' => 26,
'settings' => [
'entity_browser' => 'media_entity_browser_groups',
'field_widget_display' => 'rendered_entity',
'field_widget_remove' => TRUE,
'open' => TRUE,
'selection_mode' => 'selection_append',
'field_widget_display_settings' => [
'view_mode' => 'image_only',
],
'field_widget_edit' => FALSE,
'field_widget_replace' => FALSE,
'third_party_settings' => [
'type' => 'entity_browser_entity_reference',
],
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['description'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Description'))
->setDefaultValue('')
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setRequired(FALSE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'text_long',
'weight' => 3,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the Module is published.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the Module was created.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the Module was last edited.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$fields['random_activity_score'] = BaseFieldDefinition::create('integer')
->setLabel(t('Random activity score'))
->setDescription(t('Score per each random activity.'))
->setRevisionable(TRUE)
->setDefaultValue(1);
$fields['allow_resume'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Allow resume'))
->setDescription(t('Allow users to leave this Module incomplete and then resume it from where they left off.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => 1,
]);
$fields['backwards_navigation'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Backwards navigation'))
->setDescription(t('Allow users to go back and revisit activities already answered.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => 2,
]);
$randomization_options = [
0 => t('No randomization'),
1 => t('Random order'),
2 => t('Random activities'),
];
$randomization_description = t("<strong>Random order</strong> - all questions display in random order") . '<br/>' . t("<strong>Random activities</strong> - specific number of activities are drawn randomly from this module's pool of questions");
$fields['randomization'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Randomize activities'))
->setDescription($randomization_description)
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setSetting('allowed_values', $randomization_options)
->setDefaultValue(0)
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 3,
]);
$fields['random_activities'] = BaseFieldDefinition::create('integer')
->setLabel(t('Number of random activities'))
->setDescription(t('The number of activities to be randomly selected each time someone takes this module.'))
->setRevisionable(TRUE)
->setDefaultValue(1)
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 4,
]);
$takes_options = [
t('Unlimited'),
];
for ($i = 1; $i < 10; $i++) {
$takes_options[$i] = $i;
}
$fields['takes'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Allowed number of attempts'))
->setDescription(t('The number of times a user is allowed to take this Module. <strong>Anonymous users are only allowed to take Module that allow an unlimited number of attempts.</strong>'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setSetting('allowed_values', $takes_options)
->setDefaultValue(0)
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 4,
]);
$fields['show_attempt_stats'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Display allowed number of attempts'))
->setDescription(t('Display the allowed number of attempts on the starting page for this Module.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => 5,
]);
$keep_results_options = [
0 => t('The best'),
1 => t('The newest'),
2 => t('All'),
];
$fields['keep_results'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Store results'))
->setDescription(t('These results should be stored for each user.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setSetting('allowed_values', $keep_results_options)
->setDefaultValue(2)
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 6,
]);
$fields['hide_results'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Do not display results at the end of the module'))
->setDescription(t('If you check this option, the correct answers won’t be displayed to the users at the end of the module.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(FALSE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => 2,
]);
$fields['badge_active'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Activate badge system for this module'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(FALSE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => 1,
]);
$fields['badge_name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 1,
]);
$fields['badge_description'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Badge description'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setRequired(FALSE)
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textarea',
'weight' => 2,
'settings' => [
'rows' => 3,
],
]);
$options = [
'finished' => 'Finished',
'success' => 'Success',
];
$fields['badge_criteria'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Badge criteria'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue('finished')
->setSetting('allowed_values', $options)
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 3,
]);
$fields['badge_media_image'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Badge image'))
->setRevisionable(TRUE)
->setTranslatable(FALSE)
->setSetting('target_type', 'media')
->setSetting('handler', 'default')
->setSetting('handler_settings', [
'target_bundles' => [
'image_png',
],
])
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'media_thumbnail',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_browser_entity_reference',
'weight' => 4,
'settings' => [
'entity_browser' => 'media_entity_browser_badge_images',
'field_widget_display' => 'rendered_entity',
'field_widget_remove' => TRUE,
'open' => TRUE,
'selection_mode' => 'selection_append',
'field_widget_display_settings' => [
'view_mode' => 'image_only',
],
'field_widget_edit' => FALSE,
'field_widget_replace' => FALSE,
'third_party_settings' => [
'type' => 'entity_browser_entity_reference',
],
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['skills_active'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Activate skills system for this module'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(FALSE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => 1,
]);
$term_storage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$target_skills = $term_storage
->loadTree('skills', 0, 1);
$options = [];
foreach ($target_skills as $row) {
$options[$row->tid] = $row->name;
}
$fields['skill_target'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Target skill'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setSetting('allowed_values', $options)
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 3,
]);
$fields['module_global'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Use all suitable activities from Opigno system'))
->setDescription(t('If checked then Opigno system will load all suitable activities from other trainings and not assigned activities.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(FALSE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => 4,
]);
return $fields;
}
}