function EntityContentBase::fields

Overrides Entity::fields

File

core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php, line 423

Class

EntityContentBase
Provides destination class for all content entities lacking a specific class.

Namespace

Drupal\migrate\Plugin\migrate\destination

Code

public function fields() : array {
  $entity_type = $this->storage
    ->getEntityType();
  // Retrieving fields from a non-fieldable content entity will return a
  // LogicException. Return an empty list of fields instead.
  if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
    return [];
  }
  // Try to determine the bundle to use when getting fields.
  $bundle_info = $this->entityTypeBundleInfo
    ->getBundleInfo($entity_type->id());
  if (!empty($this->configuration['default_bundle'])) {
    // If the migration destination configuration specifies a default_bundle,
    // then use that.
    $bundle = $this->configuration['default_bundle'];
    if (!isset($bundle_info[$bundle])) {
      throw new MigrateException(sprintf("The default_bundle value '%s' is not a valid bundle for the destination entity type '%s'.", $bundle, $entity_type->id()));
    }
  }
  elseif (count($bundle_info) === 1) {
    // If the destination entity type has only one bundle, use that.
    $bundle = array_key_first($bundle_info);
  }
  if (isset($bundle)) {
    // If we have a bundle, get all the fields.
    $field_definitions = $this->entityFieldManager
      ->getFieldDefinitions($entity_type->id(), $bundle);
  }
  else {
    // Without a bundle, we can only get the base fields.
    $field_definitions = $this->entityFieldManager
      ->getBaseFieldDefinitions($entity_type->id());
  }
  $fields = [];
  foreach ($field_definitions as $field_name => $definition) {
    $fields[$field_name] = (string) $definition->getLabel();
  }
  return $fields;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.