You are here

public static function Poll::baseFieldDefinitions in Poll 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/Poll.php, line 198

Class

Poll
Defines the poll entity class.

Namespace

Drupal\poll\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields['id'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Poll ID'))
    ->setDescription(t('The ID of the poll.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Author'))
    ->setDescription(t('The poll author.'))
    ->setSetting('target_type', 'user')
    ->setTranslatable(TRUE)
    ->setDefaultValueCallback('Drupal\\poll\\Entity\\Poll::getCurrentUserId')
    ->setDisplayOptions('form', array(
    'type' => 'entity_reference_autocomplete',
    'weight' => -10,
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ),
  ))
    ->setDisplayConfigurable('form', TRUE);
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('UUID'))
    ->setDescription(t('The poll UUID.'))
    ->setReadOnly(TRUE);
  $fields['question'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Question'))
    ->setDescription(t('The poll question.'))
    ->setRequired(TRUE)
    ->setTranslatable(TRUE)
    ->setSetting('max_length', 255)
    ->setDisplayOptions('form', array(
    'type' => 'string_textfield',
    'weight' => -100,
  ))
    ->setDisplayConfigurable('form', TRUE);
  $fields['langcode'] = BaseFieldDefinition::create('language')
    ->setLabel(t('Language code'))
    ->setDescription(t('The poll language code.'));
  $fields['choice'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Choice'))
    ->setSetting('target_type', 'poll_choice')
    ->setDescription(t('Enter the poll choices.'))
    ->setRequired(TRUE)
    ->setTranslatable(FALSE)
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
    ->setSetting('max_length', 255)
    ->setDisplayOptions('form', [
    'type' => 'poll_choice_default',
    'settings' => [],
    'weight' => -20,
  ])
    ->setDisplayConfigurable('form', TRUE);

  // Poll attributes
  $duration = array(
    // 1-6 days.
    86400,
    2 * 86400,
    3 * 86400,
    4 * 86400,
    5 * 86400,
    6 * 86400,
    // 1-3 weeks (7 days).
    604800,
    2 * 604800,
    3 * 604800,
    // 1-3,6,9 months (30 days).
    2592000,
    2 * 2592000,
    3 * 2592000,
    6 * 2592000,
    9 * 2592000,
    // 1 year (365 days).
    31536000,
  );
  $period = array(
    0 => t('Unlimited'),
  ) + array_map(array(
    \Drupal::service('date.formatter'),
    'formatInterval',
  ), array_combine($duration, $duration));
  $fields['runtime'] = BaseFieldDefinition::create('list_integer')
    ->setLabel(t('Poll Duration'))
    ->setDescription(t('After this period, the poll will be closed automatically.'))
    ->setSetting('unsigned', TRUE)
    ->setRequired(TRUE)
    ->setSetting('allowed_values', $period)
    ->setDefaultValue(0)
    ->setDisplayOptions('form', array(
    'type' => 'options_select',
    'weight' => 0,
  ))
    ->setDisplayConfigurable('form', TRUE);
  $fields['anonymous_vote_allow'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Allow anonymous votes'))
    ->setDescription(t('A flag indicating whether anonymous users are allowed to vote.'))
    ->setDefaultValue(0)
    ->setDisplayOptions('form', array(
    'type' => 'boolean_checkbox',
    'settings' => array(
      'display_label' => TRUE,
    ),
    'weight' => 1,
  ))
    ->setDisplayConfigurable('form', TRUE);
  $fields['cancel_vote_allow'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Allow cancel votes'))
    ->setDescription(t('A flag indicating whether users may cancel their vote.'))
    ->setDefaultValue(1)
    ->setDisplayOptions('form', array(
    'type' => 'boolean_checkbox',
    'settings' => array(
      'display_label' => TRUE,
    ),
    'weight' => 2,
  ))
    ->setDisplayConfigurable('form', TRUE);
  $fields['result_vote_allow'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Allow view results'))
    ->setDescription(t('A flag indicating whether users may see the results before voting.'))
    ->setDefaultValue(0)
    ->setDisplayOptions('form', array(
    'type' => 'boolean_checkbox',
    'settings' => array(
      'display_label' => TRUE,
    ),
    'weight' => 3,
  ))
    ->setDisplayConfigurable('form', TRUE);
  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Active'))
    ->setDescription(t('A flag indicating whether the poll is active.'))
    ->setDefaultValue(1)
    ->setDisplayOptions('form', array(
    'type' => 'boolean_checkbox',
    'settings' => array(
      'display_label' => TRUE,
    ),
    'weight' => -5,
  ))
    ->setDisplayConfigurable('form', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('When the poll was created, as a Unix timestamp.'));
  return $fields;
}