You are here

public static function TwitterWidget::baseFieldDefinitions in Twitter Profile Widget 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 ContentEntityBase::baseFieldDefinitions

See also

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

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

File

src/Entity/TwitterWidget.php, line 149

Class

TwitterWidget
Defines the Twitter widget entity.

Namespace

Drupal\twitter_profile_widget\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields['id'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('ID'))
    ->setDescription(t('The ID of the Twitter widget entity.'))
    ->setReadOnly(TRUE);
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('UUID'))
    ->setDescription(t('The UUID of the Twitter widget entity.'))
    ->setReadOnly(TRUE);
  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Publishing status'))
    ->setDescription(t('A boolean indicating whether the Twitter widget is published.'))
    ->setDefaultValue(TRUE);
  $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Authored by'))
    ->setDescription(t('The user ID of author of the Twitter widget entity.'))
    ->setRevisionable(TRUE)
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setDefaultValueCallback('Drupal\\node\\Entity\\Node::getCurrentUserId')
    ->setTranslatable(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'author',
    'weight' => 0,
  ])
    ->setReadOnly(TRUE)
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Widget Label'))
    ->setDescription(t('Identifies this widget within the site. It should be descriptive and unique.'))
    ->setSettings([
    'max_length' => 50,
    'text_processing' => 0,
  ])
    ->setDefaultValue('')
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -4,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -4,
  ])
    ->setRequired(TRUE)
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['headline'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Headline'))
    ->setDescription(t('Optional header that appears above the tweets.'))
    ->setSettings([
    'default_value' => 'Latest Tweets',
    'max_length' => 255,
    'text_processing' => 0,
  ])
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -1,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -1,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['type'] = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Type'))
    ->setDescription(t('Criteria used to return tweets.'))
    ->setSettings([
    'allowed_values' => [
      'status' => 'User tweets',
      'timeline' => 'User timeline',
      'favorites' => 'Favorited by user',
      'search' => 'Search term (Twitter-wide)',
    ],
  ])
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => 0,
  ])
    ->setRequired(TRUE)
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['account'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Twitter Account'))
    ->setDescription(t('The username (handle) from which to pull tweets.'))
    ->setSettings([
    'default_value' => '',
    'max_length' => 255,
    'text_processing' => 0,
  ])
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => 1,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['search'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Search query'))
    ->setDescription(t('A search term, which may include Twitter <a href=":examples" target="blank">query operators</a>. Results are sorted based on Twitter ranking algorithm.', [
    ':examples' => 'https://dev.twitter.com/rest/public/search#query-operators',
  ]))
    ->setSettings([
    'default_value' => '',
    'max_length' => 255,
    'text_processing' => 0,
  ])
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => 2,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['timeline'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Timeline List'))
    ->setDescription(t('Must already exist in Twitter to return any results. Lists are found at https://twitter.com/[username]/lists'))
    ->setSettings([
    'default_value' => '',
    'max_length' => 255,
    'text_processing' => 0,
  ])
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => 2,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['count'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Number of tweets'))
    ->setDescription(t('How many Tweets should be displayed?'))
    ->setDefaultValue(5)
    ->setSetting('min', 1)
    ->setSetting('max', 10)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'hidden',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'integer',
    'weight' => 3,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['retweets'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Display Retweets'))
    ->setDefaultValue(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'boolean',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => 4,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['replies'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Display Replies'))
    ->setDefaultValue(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'boolean',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => 5,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['view_all'] = BaseFieldDefinition::create('string')
    ->setLabel(t('"View all..." text'))
    ->setDescription(t('Optional text displayed at the bottom of the widget, linking to Twitter.'))
    ->setSettings([
    'default_value' => 'View on Twitter',
    'max_length' => 255,
    'text_processing' => 0,
  ])
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => 2,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['langcode'] = BaseFieldDefinition::create('language')
    ->setLabel(t('Language code'))
    ->setDescription(t('The language code for the Twitter widget entity.'))
    ->setDisplayOptions('form', [
    'type' => 'language_select',
    'weight' => 10,
  ])
    ->setDisplayConfigurable('form', 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.'));
  return $fields;
}