Skip to content

Commit

Permalink
Revert "change properties handling, add entity property to media enti…
Browse files Browse the repository at this point in the history
…ties (#35)"

This reverts commit eb891f2.
  • Loading branch information
Dan Lemon committed Jun 14, 2019
1 parent 538b09c commit e843bb0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 45 deletions.
46 changes: 9 additions & 37 deletions src/DocumentConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ protected function processTemplateNode(DocumentSection $section, \DOMElement $el
foreach ($el->childNodes as $child) {
$el->removeChild($child);
}
foreach ($fragment->documentElement->lastChild->lastChild->childNodes as $child) {
$el->appendChild($el->ownerDocument->importNode($child, TRUE));

foreach ($fragment->firstChild->childNodes as $child) {
$el->appendChild($child);
}
}
}
Expand Down Expand Up @@ -262,51 +263,42 @@ 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';
$fieldName = $node->getAttribute('itemprop');

// Also support entities. Add a field 'entity'.
// Also support entities.
if (
$node->hasAttribute('data-media-uuid') &&
$node->hasAttribute('data-media-type') &&
($entity_type_id = @explode(':', trim($node->getAttribute('data-media-type')))[0])
) {
$attributes['entity'] = 'entity:' . $entity_type_id;
$itemtype ='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_merge(array_map(function(\DOMNode $attribute) {
'attributes' => array_map(function(\DOMNode $attribute) {
return $attribute->nodeValue;
}, iterator_to_array($node->attributes)), $attributes),
}, iterator_to_array($node->attributes)),
];

if ($node->hasAttribute('ck-contains')) {
$result[$parentType]['fields'][$fieldName]['cardinality'] = 'multiple';
}
Expand Down Expand Up @@ -383,32 +375,12 @@ 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('//ck-media[@data-media-uuid]') as $node) {
foreach ($xpath->query('//div[@data-media-uuid]') as $node) {
$entities[$node->getAttribute('data-media-uuid')] = 'media';
}
return $entities;
Expand Down
1 change: 0 additions & 1 deletion tests/src/Kernel/DocumentConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class DocumentConverterTest extends KernelTestBase {
public static $modules = [
'ckeditor5_sections',
'editor',
'media',
'filter',
'serialization'
];
Expand Down
7 changes: 1 addition & 6 deletions tests/src/Kernel/assets/definitions/teaser.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"data-media-uuid": "",
"itemprop": "image",
"itemtype": "teaser-image",
"class": "teaser__image",
"entity": "entity:media"
"class": "teaser__image"
}
},
"headline": {
Expand Down Expand Up @@ -62,10 +61,6 @@
"data-media-uuid": {
"label": "data-media-uuid",
"type": "string"
},
"entity": {
"label": "media Entity",
"type": "entity:media"
}
},
"attributes": {
Expand Down

0 comments on commit e843bb0

Please sign in to comment.