Skip to content

Commit

Permalink
change properties handling, add entity property to media entities (#35)
Browse files Browse the repository at this point in the history
* change properties handling, add entity property to media entities

* fix tests

* fix multiline fields processing (#40)
  • Loading branch information
monya007 authored and pmelab committed Jun 6, 2019
1 parent 8f01fb9 commit eb891f2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
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

0 comments on commit eb891f2

Please sign in to comment.