Guest User

Untitled

a guest
Dec 6th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\sd8;
  4.  
  5. use Drupal\Core\Entity\ContentEntityInterface;
  6. use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
  7.  
  8. /**
  9. * Convert an entity to an array and back.
  10. */
  11. class EntityArrayHelper {
  12.  
  13. /**
  14. * Convert an array to entity or entities.
  15. *
  16. * @param $entityTypeId
  17. * The entity type id.
  18. * @param array $data
  19. * Similar to what can be passed ContentEntityStorageInterface::create()
  20. * but entity references can be inlined and they will be autocreated. The
  21. * reference entity type can be specified by an entity_type key.
  22. * Run static::entity2array() for examples.
  23. *
  24. * @return \Drupal\Core\Entity\ContentEntityInterface
  25. */
  26. public static function array2entity($entityTypeId, array $data) {
  27. /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  28. $entity = \Drupal::entityTypeManager()->getStorage($entityTypeId)->create($data);
  29. foreach ($entity->getFieldDefinitions() as $fieldName => $fieldDefinition) {
  30. $fieldItemList = $entity->get($fieldName);
  31. if (isset($data[$fieldName]) && $fieldItemList instanceof EntityReferenceFieldItemListInterface) {
  32. if (!isset($data[$fieldName][0])) {
  33. $data[$fieldName] = [$data[$fieldName]];
  34. }
  35. foreach ($data[$fieldName] as $referencedData) {
  36. if (!isset($referencedData['target_id'])) {
  37. $referencedEntityTypeId = $referencedData['entity_type'] ?? $fieldDefinition->getSetting('target_type');
  38. $fieldItemList->appendItem(static::array2entity($referencedEntityTypeId, $referencedData));
  39. }
  40. }
  41. }
  42. }
  43. return $entity;
  44. }
  45.  
  46. /**
  47. * Convert an entity to an array, inlining referred entities.
  48. *
  49. * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  50. * Entity to be converted.
  51. * @param array $inline
  52. * Entity type IDs to be inlined.
  53. *
  54. * @return array
  55. * Similar to $entity->toArray() but the values for entity references are
  56. * not target_id but the entire referenced $entity->toArray() for types in
  57. * $expand. As the default value of $inline shows, this is very useful for
  58. * paragraphs.
  59. */
  60. public static function entity2array(ContentEntityInterface $entity, array $inline = ['paragraph']) {
  61. $array = $entity->toArray();
  62. foreach ($entity->getFields() as $fieldName => $fieldItemList) {
  63. if ($fieldItemList instanceof EntityReferenceFieldItemListInterface) {
  64. foreach ($fieldItemList->referencedEntities() as $delta => $referencedEntity) {
  65. if ($referencedEntity instanceof ContentEntityInterface && array_search($referencedEntity->getEntityTypeId(), $inline) !== FALSE) {
  66. $array[$fieldName][$delta] = static::inlineEntity($referencedEntity, $inline);
  67. }
  68. }
  69. }
  70. }
  71. return $array;
  72. }
  73.  
  74. /**
  75. * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  76. *
  77. * @param array $inline
  78. *
  79. * @return array
  80. */
  81. protected static function inlineEntity(ContentEntityInterface $entity, array $inline) {
  82. $array = static::entity2array($entity, $inline);
  83. $array['entity_type'] = $entity->getEntityTypeId();
  84. $keys = $entity->getEntityType()->getKeys();
  85. unset($array[$keys['id']], $array[$keys['revision']], $array[$keys['uuid']]);
  86. return $array;
  87. }
  88.  
  89. }
Add Comment
Please, Sign In to add comment