EntityFieldDefinitionTrait.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Drupal\migrate;
  3. /**
  4. * The entity field definition trait.
  5. */
  6. trait EntityFieldDefinitionTrait {
  7. /**
  8. * Gets the field definition from a specific entity base field.
  9. *
  10. * The method takes the field ID as an argument and returns the field storage
  11. * definition to be used in getIds() by querying the destination entity base
  12. * field definition.
  13. *
  14. * @param string $key
  15. * The field ID key.
  16. *
  17. * @return array
  18. * An associative array with a structure that contains the field type, keyed
  19. * as 'type', together with field storage settings as they are returned by
  20. * FieldStorageDefinitionInterface::getSettings().
  21. *
  22. * @see \Drupal\Core\Field\FieldStorageDefinitionInterface::getSettings()
  23. */
  24. protected function getDefinitionFromEntity($key) {
  25. $plugin_id = $this->getPluginId();
  26. $entity_type_id = $this->getEntityTypeId($plugin_id);
  27. /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $definitions */
  28. $definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id);
  29. $field_definition = $definitions[$key];
  30. return [
  31. 'type' => $field_definition->getType(),
  32. ] + $field_definition->getSettings();
  33. }
  34. /**
  35. * Finds the entity type from configuration or plugin ID.
  36. *
  37. * @param string $plugin_id
  38. * The plugin ID.
  39. *
  40. * @return string
  41. * The entity type.
  42. */
  43. protected static function getEntityTypeId($plugin_id) {
  44. $entity_type_id = NULL;
  45. if (strpos($plugin_id, static::DERIVATIVE_SEPARATOR)) {
  46. list(, $entity_type_id) = explode(static::DERIVATIVE_SEPARATOR, $plugin_id, 2);
  47. }
  48. return $entity_type_id;
  49. }
  50. }