Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change properties handling, add entity property to media entities #35

Merged
merged 4 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions src/DocumentConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ protected function processTemplateNode(DocumentSection $section, \DOMElement $el
foreach ($el->childNodes as $child) {
$el->removeChild($child);
}

foreach ($fragment->firstChild->childNodes as $child) {
// @todo: Test coverage.
foreach ($fragment->childNodes as $child) {
$el->appendChild($child);
}
}
Expand Down Expand Up @@ -261,42 +261,51 @@ protected function buildSectionDefinitions(\DOMNode $node, &$result, $parentType
$internalAttributes[$attributeName] = $attribute->nodeValue;
continue;
}

// Add the attribute as a string field.
$result[$type]['fields'][$attributeName] = [
'label' => $attributeName,
'type' => 'string',
];
}
if (array_key_exists('data-media-type', $result[$type]['fields'])) {
$entity_type_id = @explode(':', trim($node->getAttribute('data-media-type')))[0];
$result[$type]['fields']['entity'] = [
'type' => 'entity:' . $entity_type_id,
'label' => $entity_type_id . ' Entity',
];
}
$result[$type]['attributes'] = $internalAttributes;
}
// If the element has an item prop, then we actually have to add it to its
// parent (if any).
if ($node->hasAttribute('itemprop') && !empty($parentType)) {
$attributes = [];
// The type of the field depends actually on the itemtype property. If
// there is an itemtype, it will just be used as the type, otherwise
// the type will be 'string'.
$itemtype = $node->hasAttribute('itemtype') ? 'section:' . $node->getAttribute('itemtype') : 'string';
// Also support entities.
$fieldName = $node->getAttribute('itemprop');

// Also support entities. Add a field 'entity'.
if (
$node->hasAttribute('data-media-uuid') &&
$node->hasAttribute('data-media-type') &&
($entity_type_id = @explode(':', trim($node->getAttribute('data-media-type')))[0])
) {
$itemtype ='entity:' . $entity_type_id;
$attributes['entity'] = 'entity:' . $entity_type_id;
}

if ($node->hasAttribute('ck-contains')) {
$itemtype = 'section';
}
$fieldName = $node->getAttribute('itemprop');
$result[$parentType]['fields'][$fieldName] = [
'label' => $fieldName,
'type' => $itemtype,
'attributes' => array_map(function(\DOMNode $attribute) {
'attributes' => array_merge(array_map(function(\DOMNode $attribute) {
return $attribute->nodeValue;
}, iterator_to_array($node->attributes)),
}, iterator_to_array($node->attributes)), $attributes),
];

if ($node->hasAttribute('ck-contains')) {
$result[$parentType]['fields'][$fieldName]['cardinality'] = 'multiple';
}
Expand Down Expand Up @@ -373,12 +382,32 @@ public function buildSectionData(\DOMNode $node, array &$sections_list, Document
if ($item_field_definition instanceof DocumentSectionDataDefinition) {
$item_field_data = new DocumentSection($value['item_type']);
// Add all the attributes.
$entityType = '';
foreach ($node->attributes as $attribute) {
$attributeName = $attribute->nodeName;
// Add the attribute to the current value.
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 ($item_field_definition->getPropertyDefinition('entity')) {
$entity_type_id = @explode(':', trim($entityType))[0];
try {
$entities = $this->entityTypeManager
->getStorage($entity_type_id)
->loadByProperties([
'uuid' => $node->getAttribute('data-media-uuid'),
]);
$entity = reset($entities);
}
catch (\Exception $e) {
$entity = NULL;
}
$item_field_data->set('entity', $entity);
}
$new_parent = $item_field_data;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/EntityUsage/Track/CKEditor5SectionsMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function parseEntitiesFromText($text) {
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
$entities = [];
foreach ($xpath->query('//div[@data-media-uuid]') as $node) {
foreach ($xpath->query('//ck-media[@data-media-uuid]') as $node) {
$entities[$node->getAttribute('data-media-uuid')] = 'media';
}
return $entities;
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 @@ -14,6 +14,7 @@ class DocumentConverterTest extends KernelTestBase {
public static $modules = [
'ckeditor5_sections',
'editor',
'media',
'filter',
'serialization'
];
Expand Down
7 changes: 6 additions & 1 deletion tests/src/Kernel/assets/definitions/teaser.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"data-media-uuid": "",
"itemprop": "image",
"itemtype": "teaser-image",
"class": "teaser__image"
"class": "teaser__image",
"entity": "entity:media"
}
},
"headline": {
Expand Down Expand Up @@ -61,6 +62,10 @@
"data-media-uuid": {
"label": "data-media-uuid",
"type": "string"
},
"entity": {
"label": "media Entity",
"type": "entity:media"
}
},
"attributes": {
Expand Down