You are here

public static function InstagramAccount::baseFieldDefinitions in Instagram Feeds 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/InstagramAccount.php, line 71

Class

InstagramAccount
Defines the instagram_account entity class.

Namespace

Drupal\instagram_feeds\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = [];
  $fields['iid'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Instagram Account ID'))
    ->setSetting('max_length', 32)
    ->addConstraint('UniqueField');
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(new TranslatableMarkup('UUID'))
    ->setReadOnly(TRUE);
  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(new TranslatableMarkup('Published'))
    ->setDefaultValue(TRUE);
  $fields['account'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Instagram Username'))
    ->setDescription(t('The Instagram Account unique username.'))
    ->setSetting('max_length', 30)
    ->addConstraint('UniqueField');
  $fields['cron_import_limit'] = BaseFieldDefinition::create('integer')
    ->setLabel(new TranslatableMarkup('Cron Import Limit'))
    ->setDefaultValue(10)
    ->setSetting('min', 1)
    ->setSetting('max', 25);
  $allowed_media_types = static::getInstagramMediaTypes();
  $fields['media_bundle'] = BaseFieldDefinition::create('list_string')
    ->setLabel(new TranslatableMarkup('Media Bundle'))
    ->setSetting('max_length', 32)
    ->setSetting('allowed_values', $allowed_media_types)
    ->setDefaultValue(array_shift($allowed_media_types));
  $fields['token'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Instagram API Token'))
    ->setSetting('max_length', 255);
  $fields['token_expiration'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(new TranslatableMarkup('Instagram Token Expiration Date'))
    ->setReadOnly(TRUE);
  $fields['last_import'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(new TranslatableMarkup('Last Import Date'))
    ->setReadOnly(TRUE);
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(new TranslatableMarkup('Authored by'))
    ->setDescription(new TranslatableMarkup('The username of the author.'))
    ->setSetting('target_type', 'user')
    ->setDefaultValueCallback(self::class . '::getCurrentUserId');
  $weight = -1 - count($fields) * 5;

  /** @var \Drupal\Core\Field\BaseFieldDefinition $field */
  foreach ($fields as $id => $field) {
    $view_settings = $form_settings = [
      'weight' => $weight,
    ];
    if ($id == 'uid') {
      $view_settings['type'] = 'author';
      $form_settings += [
        'type' => 'entity_reference_autocomplete',
        'settings' => [
          'match_operator' => 'CONTAINS',
          'size' => '60',
          'placeholder' => '',
        ],
      ];
    }
    $field
      ->setDisplayOptions('view', $view_settings);
    if ($id != 'last_import') {
      $field
        ->setDisplayOptions('form', $form_settings);
      $field
        ->setDisplayConfigurable('form', TRUE);
    }
    $weight += 5;
    if ($id != 'token') {
      $field
        ->setDisplayConfigurable('view', TRUE);
    }
    if ($id != 'status') {
      $field
        ->setRequired(TRUE);
    }
  }
  return $fields;
}