public static function SitewideAlert::baseFieldDefinitions in Sitewide Alert 8
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/ SitewideAlert.php, line 207
Class
- SitewideAlert
- Defines the Sitewide Alert entity.
Namespace
Drupal\sitewide_alert\EntityCode
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
// Add the published field.
$fields += static::publishedBaseFieldDefinitions($entity_type);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of this Sitewide Alert.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', FALSE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('A brief description of this Sitewide Alert. Used for administrative reference only.'))
->setRevisionable(TRUE)
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -15,
])
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE)
->setRequired(TRUE);
$fields['status']
->setLabel(new TranslatableMarkup('Active'))
->setDescription(t('If selected this Sitewide Alert will be active and will show if all other conditions are met.'))
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => 99,
]);
$fields['style'] = BaseFieldDefinition::create('list_string')
->setLabel(new TranslatableMarkup('Alert Style'))
->setDescription(new TranslatableMarkup('The style of this alert. This mainly can be used to change the color of the alert.'))
->setSettings([
'allowed_values_function' => '\\Drupal\\sitewide_alert\\AlertStyleProvider::alertStyles',
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => -14,
])
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE)
->setRequired(TRUE);
$fields['dismissible'] = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Dismissible'))
->setDescription(new TranslatableMarkup('If selected, visitors will be able to dismiss the alert so it will not be seen again. This is per browser, and does not require the visitor to be logged in.'))
->setRequired(FALSE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => -10,
])
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
$fields['dismissible_ignore_before_time'] = BaseFieldDefinition::create('timestamp')
->setLabel(new TranslatableMarkup('Ignore Dismissals Before'))
->setDefaultValue(0)
->setDescription(new TranslatableMarkup('Ignore any dismissals made before this date. If you are editing an existing alert, and this is a major change to the content, you may want to ignore the fact that this alert was dismissed.'))
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
$fields['limit_to_pages'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Pages'))
->setDescription(new TranslatableMarkup('Specify pages by using their paths. Enter one path per line. The \'*\' character is a wildcard. An example path is /user/* for every user page. / is the front page.'))
->setTranslatable(FALSE)
->setRevisionable(TRUE)
->setSettings([
'default_value' => '',
])
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'string_textarea',
'weight' => -5,
'settings' => [
'rows' => 4,
],
])
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE)
->setRequired(FALSE);
$fields['limit_to_pages_negate'] = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Negate for listed pages'))
->setRequired(TRUE)
->setDefaultValue(FALSE)
->setDisplayOptions('form', [
'type' => 'options_buttons',
'label' => 'hidden',
'weight' => -4,
'settings' => [
'on_label' => 'Hide for the listed pages',
'off_label' => 'Show for the listed pages',
],
])
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
$fields['message'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Alert Message'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setSettings([
'default_value' => '',
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'text_textarea',
'weight' => -13,
'settings' => [
'rows' => 4,
],
])
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'text_default',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE);
$fields['scheduled_alert'] = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Schedule Alert'))
->setDescription(new TranslatableMarkup('Schedule this alert to start and stop showing at a particular date/time.'))
->setRequired(FALSE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => -8,
])
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
$fields['scheduled_date'] = BaseFieldDefinition::create('daterange')
->setLabel(new TranslatableMarkup('Date'))
->setDescription(t('This defines when this Sitewide Alert be scheduled to show.'))
->setRequired(FALSE)
->setDisplayOptions('form', [
'type' => 'daterange_default',
'weight' => -7,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
$fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Revision translation affected'))
->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
->setReadOnly(TRUE)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
return $fields;
}