Skip to content

Commit

Permalink
Add media entity to typed data preserving data-media-* attributes. (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Tkachev authored and pmelab committed Aug 12, 2019
1 parent 2e3d6de commit 5ea5717
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
33 changes: 13 additions & 20 deletions src/DocumentConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,31 +401,24 @@ public function buildSectionData(\DOMNode $node, array &$sections_list, Document
if ($item_field_definition->getPropertyDefinition($attributeName)) {
$item_field_data->set($attributeName, $node->getAttribute($attribute->nodeName));
}
if ($attributeName == 'data-media-type') {
$entityType = $node->getAttribute($attribute->nodeName);
if ($attributeName == 'data-media-uuid') {
// Support for media entities.
$entities = $this->entityTypeManager
->getStorage('media')
->loadByProperties([
'uuid' => $node->getAttribute('data-media-uuid'),
]);
$media = reset($entities);
if (!$media) {
// FALSE can break GraphQL. NULL is safer.
$media = NULL;
}
$item_field_data->set('entity', $media);
}
}

$new_parent = $item_field_data;
}
elseif (strpos($value['item_type'], 'entity:') === 0) {
$entity_type_id = substr($value['item_type'], strlen('entity:'));
// Support media entities.
try {
$entities = $this->entityTypeManager
->getStorage($entity_type_id)
->loadByProperties([
'uuid' => $node->getAttribute('data-media-uuid'),
]);
$item_field_data = reset($entities);
if (!$item_field_data) {
$item_field_data = NULL;
}
}
catch (\Exception $e) {
$item_field_data = NULL;
}
}
else {
// If the field is just a simple (scalar) field, then we just dump
// the entire html of the node in it for now.
Expand Down
1 change: 1 addition & 0 deletions tests/src/Kernel/DocumentConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class DocumentConverterTest extends KernelTestBase {
protected function setUp() {
parent::setUp();
$this->installConfig(['ckeditor5_sections']);
$this->installEntitySchema('media');
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/src/Kernel/assets/data/page.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"content": {
"__type": "media",
"data-media-type": "image",
"data-media-uuid": "123"
"data-media-uuid": "123",
"entity": null
},
"caption": "Caption"
},
Expand All @@ -26,7 +27,8 @@
"image": {
"__type": "teaser-image",
"data-media-type": "media:image",
"data-media-uuid": "123"
"data-media-uuid": "123",
"entity": null
},
"link": {
"__type": "button",
Expand Down
3 changes: 2 additions & 1 deletion tests/src/Kernel/assets/data/teaser.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"image": {
"__type": "teaser-image",
"data-media-type": "image",
"data-media-uuid": "123"
"data-media-uuid": "123",
"entity": null
},
"link": {
"__type": "button",
Expand Down

0 comments on commit 5ea5717

Please sign in to comment.