public static function Course::baseFieldDefinitions in Course 3.x
Same name and namespace in other branches
- 8.3 src/Entity/Course.php \Drupal\course\Entity\Course::baseFieldDefinitions()
- 8.2 src/Entity/Course.php \Drupal\course\Entity\Course::baseFieldDefinitions()
Provides base field definitions for an entity type.
Implementations typically use the class \Drupal\Core\Field\BaseFieldDefinition for creating the field definitions; for example a 'name' field could be defined as the following:
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'));
By definition, base fields are fields that exist for every bundle. To provide definitions for fields that should only exist on some bundles, use \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions().
The definitions returned by this function can be overridden for all bundles by hook_entity_base_field_info_alter() or overridden on a per-bundle basis via 'base_field_override' configuration entities.
Parameters
\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition. Useful when a single class is used for multiple, possibly dynamic entity types.
Return value
\Drupal\Core\Field\FieldDefinitionInterface[] An array of base field definitions for the entity type, keyed by field name.
Overrides EditorialContentEntityBase::baseFieldDefinitions
See also
\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()
\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()
File
- src/
Entity/ Course.php, line 264
Class
- Course
- Defines the Course entity class.
Namespace
Drupal\course\EntityCode
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$config = Drupal::config('course.settings.enrollment');
// Get course outline plugins.
/* @var $pluginManager CourseOutlinePluginManager */
$pluginManager = Drupal::service('plugin.manager.course.outline');
$outlines = array_column($pluginManager
->getDefinitions(), 'label', 'id');
// Get enrollment bundles.
$ebundles = array_column(CourseEnrollmentType::loadMultiple(), 'label', 'id');
$fields['outline'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Outline display'))
->setDescription(t('This controls the presentation of the course objects.'))
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'options_select',
])
->setDisplayConfigurable('form', TRUE)
->setRevisionable(TRUE)
->setSetting('allowed_values', $outlines);
$fields['enrollment_type'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Enrollment type'))
->setDescription(t("The enrollment type's fields, if required for enrollment, will be presented to the user before starting the course."))
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'options_select',
])
->setDefaultValue($config
->get('default_enrollment_type'))
->setDisplayConfigurable('form', TRUE)
->setRevisionable(TRUE)
->setSetting('allowed_values', $ebundles);
$fields['credits'] = BaseFieldDefinition::create('float')
->setRevisionable(TRUE)
->setLabel(t('Credits'))
->setDescription(t('For more advanced crediting, use the <a href=":link">Course credit</a> module.', array(
':link' => Url::fromUri('https://drupal.org/project/course_credit')
->toString(),
)))
->setDisplayOptions('form', [
'type' => 'number',
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
// This is a simple credit hours field. If course_credit is enabled it used
// for storing the maximum credit of any credit instance.
if (!Drupal::moduleHandler()
->moduleExists('course_credit')) {
$fields['credits']
->setDisplayConfigurable('form', FALSE);
}
$fields['duration'] = BaseFieldDefinition::create('integer')
->setRevisionable(TRUE)
->setLabel(t('Duration'))
->setDescription(t('Length of time in seconds that a user can remain in the course. Leave blank for unlimited.<br/>For a better experience, install the <a href=":link">Time period</a> module.', array(
':link' => Url::fromUri('https://drupal.org/project/timeperiod')
->toString(),
)))
->setDisplayOptions('form', [
'type' => 'number',
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
if (Drupal::moduleHandler()
->moduleExists('timeperiod')) {
$form['course']['duration']['#type'] = 'timeperiod_select';
$form['course']['duration']['#units'] = array(
'86400' => array(
'max' => 30,
'step size' => 1,
),
'3600' => array(
'max' => 24,
'step size' => 1,
),
'60' => array(
'max' => 60,
'step size' => 1,
),
);
$form['course']['duration']['#description'] = t('Length of time that a user can remain in the course.');
}
$fields['external_id'] = BaseFieldDefinition::create('string')
->setLabel(t('External learning application course ID'))
->setDescription('If using an external learning application, the ID of the external course.')
->setDisplayOptions('form', [
'type' => 'string_textfield',
])
->setDisplayConfigurable('form', TRUE);
if (FALSE && arg(2) == 'clone') {
// @todo not even going to try and make this work right now
$form['course']['clone_type'] = array(
'#title' => t('Course object cloning'),
'#description' => t('"New" will create new instances of all course objects.<br/>"Reference" will link supported content in the old course to the new course.<br/>"Clone" will copy supported course objects, otherwise create new ones.'),
'#type' => 'radios',
'#options' => array(
'clone' => 'Clone',
'reference' => 'Reference',
'new' => 'New',
),
'#default_value' => 'clone',
);
}
$fields['created'] = BaseFieldDefinition::create('created')
->setRevisionable(TRUE)
->setLabel('Created');
$fields['changed'] = BaseFieldDefinition::create('changed')
->setRevisionable(TRUE)
->setLabel('Changed');
$fields['course_date'] = BaseFieldDefinition::create('daterange')
->setDisplayConfigurable('form', TRUE)
->setRevisionable(TRUE)
->setDisplayOptions('form', [
'type' => 'daterange_default',
])
->setDisplayConfigurable('view', TRUE)
->setLabel('Course date');
$fields['uid']
->setLabel(t('Authored by'))
->setDescription(t('The username of the content author.'))
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE);
$fields['status']
->setRevisionable(TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 120,
])
->setDisplayConfigurable('form', TRUE);
return $fields;
}