Node::baseFieldDefinitions

public static Node::baseFieldDefinitions(EntityTypeInterface $entity_type)

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\EntityManagerInterface::getFieldDefinitions()

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

File

core/modules/node/src/Entity/Node.php, line 368

Class

Node
Defines the node entity class.

Namespace

Drupal\node\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);

  $fields['title'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Title'))
    ->setRequired(TRUE)
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE)
    ->setSetting('max_length', 255)
    ->setDisplayOptions('view', array(
      'label' => 'hidden',
      'type' => 'string',
      'weight' => -5,
    ))
    ->setDisplayOptions('form', array(
      'type' => 'string_textfield',
      'weight' => -5,
    ))
    ->setDisplayConfigurable('form', TRUE);

  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Authored by'))
    ->setDescription(t('The username of the content author.'))
    ->setRevisionable(TRUE)
    ->setSetting('target_type', 'user')
    ->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId')
    ->setTranslatable(TRUE)
    ->setDisplayOptions('view', array(
      'label' => 'hidden',
      'type' => 'author',
      'weight' => 0,
    ))
    ->setDisplayOptions('form', array(
      'type' => 'entity_reference_autocomplete',
      'weight' => 5,
      'settings' => array(
        'match_operator' => 'CONTAINS',
        'size' => '60',
        'placeholder' => '',
      ),
    ))
    ->setDisplayConfigurable('form', TRUE);

  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Publishing status'))
    ->setDescription(t('A boolean indicating whether the node is published.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(TRUE);

  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Authored on'))
    ->setDescription(t('The time that the node was created.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDisplayOptions('view', array(
      'label' => 'hidden',
      'type' => 'timestamp',
      'weight' => 0,
    ))
    ->setDisplayOptions('form', array(
      'type' => 'datetime_timestamp',
      'weight' => 10,
    ))
    ->setDisplayConfigurable('form', TRUE);

  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the node was last edited.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE);

  $fields['promote'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Promoted to front page'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(TRUE)
    ->setDisplayOptions('form', array(
      'type' => 'boolean_checkbox',
      'settings' => array(
        'display_label' => TRUE,
      ),
      'weight' => 15,
    ))
    ->setDisplayConfigurable('form', TRUE);

  $fields['sticky'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Sticky at top of lists'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(FALSE)
    ->setDisplayOptions('form', array(
      'type' => 'boolean_checkbox',
      'settings' => array(
        'display_label' => TRUE,
      ),
      'weight' => 16,
    ))
    ->setDisplayConfigurable('form', TRUE);

  $fields['revision_timestamp'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Revision timestamp'))
    ->setDescription(t('The time that the current revision was created.'))
    ->setQueryable(FALSE)
    ->setRevisionable(TRUE);

  $fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Revision user ID'))
    ->setDescription(t('The user ID of the author of the current revision.'))
    ->setSetting('target_type', 'user')
    ->setQueryable(FALSE)
    ->setRevisionable(TRUE);

  $fields['revision_log'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Revision log message'))
    ->setDescription(t('Briefly describe the changes you have made.'))
    ->setRevisionable(TRUE)
    ->setDefaultValue('')
    ->setDisplayOptions('form', array(
      'type' => 'string_textarea',
      'weight' => 25,
      'settings' => array(
        'rows' => 4,
      ),
    ));

  $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;
}
doc_Drupal
2016-10-29 09:30:19
Comments
Leave a Comment

Please login to continue.