You are here

public static function OpignoModule::baseFieldDefinitions in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Entity/OpignoModule.php \Drupal\opigno_module\Entity\OpignoModule::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 RevisionableContentEntityBase::baseFieldDefinitions

See also

\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()

\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()

File

src/Entity/OpignoModule.php, line 803

Class

OpignoModule
Defines the Module entity.

Namespace

Drupal\opigno_module\Entity

Code

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;
}