Skip to content

Commit

Permalink
add entity reference support
Browse files Browse the repository at this point in the history
  • Loading branch information
rloos289 committed Dec 14, 2023
1 parent ba15c46 commit 96702a7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,28 @@ public function getReferencedEntity(array $entities) {
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$langcode = $entity->language()->getId();

$wagtail_id = $wagtail_utilities->getWagtailId($entity_id, $entity_type, $langcode) ?: 'not found';
$reference_data = [];
// @todo entity reference field feature in progress on wagtail.
if ($entity_type === 'node') {
$reference_data['drupal_id'] = (int) $entity_id;
$reference_data['entity_type'] = $entity_type;
// If its a streamfield wagtail expects the following two values to
// identify the node. They Can also be used to identify a node for a
// regular entity reference field.
$reference_data['type'] = $bundle;
$reference_data['value'] = (int) $wagtail_id;
}
elseif ($entity_type === 'media') {
$reference_data = $wagtail_utilities->getCredentials()['api_url_base'] . $wagtail_utilities->getWagBundle($entity) . '/' . $wagtail_id;

switch ($entity_type) {
case 'paragraph':
// Paragraphs become streamfields in wagtail. Which expect a type and
// a value. Other data is just metadata.
$reference_data['drupal_id'] = (int) $entity_id;
$reference_data['entity_type'] = $entity_type;
$reference_data['type'] = $bundle;
$reference_data['value'] = (int) $wagtail_id;
break;

case 'node':
// Node references are made via their Wagtail ID and bundle.
$reference_data = '/api/cms/sf.' . $wagtail_utilities->getWagBundle($entity) . '/' . $wagtail_id;
break;

case 'media':
// Media references are very similar to node references.
$reference_data = $wagtail_utilities->getCredentials()['api_url_base'] . $wagtail_utilities->getWagBundle($entity) . '/' . $wagtail_id;
break;
}
$entities_data[] = $reference_data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public function setCustomData($entity) {
return [
'description' => $entity->get('field_description')->value,
'intro' => (string) $entity->get('field_intro_text')->value,
// Entity Reference not currently supported by Wagtail.
'related_content_topics' => $this->getReferencedEntity($entity->get('field_topics')->referencedEntities()),
'topics' => $this->getReferencedEntity($entity->get('field_topics')->referencedEntities()),
'steps' => $this->getReferencedData($entity->get('field_process_steps')->referencedEntities()),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function setCustomData($entity) {
'time' => $entity->get('field_text_time')->value,
'step_description' => $entity->get('field_process_step_description')->value,
'cost' => $this->getReferencedData($entity->get('field_cost')->referencedEntities()),
// Stream field referencing not currently supported by Wagtail.
'related_content_transactions' => $this->getReferencedEntity($entity->get('field_transaction')->referencedEntities()),
];
}
Expand Down

0 comments on commit 96702a7

Please sign in to comment.