You are here

public static function Crop::baseFieldDefinitions in Crop API 8

Same name and namespace in other branches
  1. 8.2 src/Entity/Crop.php \Drupal\crop\Entity\Crop::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 ContentEntityBase::baseFieldDefinitions

See also

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

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

File

src/Entity/Crop.php, line 233

Class

Crop
Defines the crop entity class.

Namespace

Drupal\crop\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = [];
  $fields['cid'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Crop ID'))
    ->setDescription(t('The crop ID.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('UUID'))
    ->setDescription(t('The crop UUID.'))
    ->setReadOnly(TRUE);
  $fields['vid'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Revision ID'))
    ->setDescription(t('The crop revision ID.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['type'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Type'))
    ->setDescription(t('The crop type.'))
    ->setSetting('target_type', 'crop_type')
    ->setReadOnly(TRUE);
  $fields['langcode'] = BaseFieldDefinition::create('language')
    ->setLabel(t('Language code'))
    ->setDescription(t('The node language code.'))
    ->setRevisionable(TRUE);
  $fields['entity_id'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Entity ID'))
    ->setDescription(t('ID of entity crop belongs to.'))
    ->setSetting('unsigned', TRUE)
    ->setRevisionable(TRUE)
    ->setReadOnly(TRUE);
  $fields['entity_type'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Entity type'))
    ->setDescription(t('The type of entity crop belongs to.'))
    ->setRevisionable(TRUE)
    ->setReadOnly(TRUE);

  // Denormalized information, which is calculated in storage plugin for a
  // given entity type. Saved here for performance reasons in image effects.
  // ---
  // TODO - we are not enforcing uniqueness on this as we want to support more
  // crops per same image/image_style combination. However, image effect
  // operates with image URI only, which means we have no mechanism to
  // distinguish between multiple crops in there. If we really want to
  // support multiple crops we'll need to override core at least,
  // in \Drupal\Core\Image\ImageFactory and \Drupal\Core\Image\Image.
  // Let's leave this for now and simply load based on URI only.
  // We can use some semi-smart approach in case there are multiple crops
  // with same URI for now (first created, last created, ...).
  $fields['uri'] = BaseFieldDefinition::create('uri')
    ->setLabel(t('URI'))
    ->setDescription(t('The URI of the image crop belongs to.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setSetting('max_length', 255);
  $fields['height'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Height'))
    ->setDescription(t('The crop height.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['width'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Width'))
    ->setDescription(t('The crop width.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['x'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('X coordinate'))
    ->setDescription(t("The crop's X coordinate."))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['y'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Y coordinate'))
    ->setDescription(t("The crop's Y coordinate."))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['revision_timestamp'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Revision timestamp'))
    ->setDescription(t('The time that the current revision was created.'))
    ->setRevisionable(TRUE);
  $fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Revision author ID'))
    ->setDescription(t('The user ID of the author of the current revision.'))
    ->setSetting('target_type', 'user')
    ->setRevisionable(TRUE);
  $fields['revision_log'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Revision Log'))
    ->setDescription(t('The log entry explaining the changes in this revision.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE);
  return $fields;
}