As for #3056911: Parse RTE to get referenced entities, it would be nice that link fields referencing entities would have the entity imported automatically.

Using the UUIDLinkEnhancer from JSON:API Extras it should be feasable.

Also needs to take #2975806: Allow an entity to be updated locally after being synchronized once into account.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Grimreaper created an issue. See original summary.

Grimreaper’s picture

Extracted from a project. At the end of updateRelationships methon in JsonapiHelper.php:

+    foreach ($data['attributes'] as $field_name => $field_data) {
+      if (is_array($field_data) && !array_key_exists(0,$field_data) && isset($field_data['links']['related'])) {
+        $field_values = [];
+        if (isset($field_data['data']['id'])) {
+          $isUnlink = \Drupal::database()->select('entity_share_unlink', 'esu')
+            ->fields('esu', ['entity_id'])
+            ->condition('esu.uuid',$field_data['data']['id']);
+
+          $data = $isUnlink->execute();
+          $unlinkResults = $data->fetchAllAssoc('entity_id');
+          $unlink_nids = array_keys($unlinkResults);
+        }
+        else {
+          $unlink_nids= [];
+        }
+        if(!empty($unlink_nids)) {
+          $field_values = $field_data;
+          $type = explode('--', $field_values['data']['type']);
+          unset($field_values['data']);
+          unset($field_values['links']);
+          $nid = array_shift($unlink_nids);
+          $field_values['uri'] = 'entity:' . $type[0] . '/' . $nid;
+        }
+        else {
+          if (isset($field_data['links']) && isset($field_data['links']['related'])) {
+            $referenced_entities_response = $this->getHttpClient()->get($field_data['links']['related'])
+              ->getBody()
+              ->getContents();
+            $referenced_entities_json = Json::decode($referenced_entities_response);
+
+            if (!isset($referenced_entities_json['errors'])) {
+              $referenced_entities_ids = $this->importEntityListData($referenced_entities_json['data']);
+
+              $field_values = $field_data;
+              $type = explode('--',$field_values['data']['type']);
+              unset($field_values['data']);
+              unset($field_values['links']);
+
+              $field_values['uri'] = 'entity:' .$type[0] . '/' .$referenced_entities_ids[0];
+            }
+          }
+        }
+        $entity->set($field_name, $field_values);
+      }
+      elseif (is_array($field_data)) {
+        $field_datas = $field_data;
+        $field_values = $entity->get($field_name)->getValue();
+        foreach ($field_datas as $index => $field_data) {
+          if (isset($field_data['data']['id'])) {
+            $isUnlink = \Drupal::database()->select('entity_share_unlink', 'esu')
+              ->fields('esu', ['entity_id'])
+              ->condition('esu.uuid',$field_data['data']['id']);
+            $data = $isUnlink->execute();
+            $unlinkResults = $data->fetchAllAssoc('entity_id');
+            $unlink_nids = array_keys($unlinkResults);
+          }
+          else {
+            $unlink_nids= [];
+          }
+          if(!empty($unlink_nids)) {
+            $type = explode('--', $field_data['data']['type']);
+            $nid = array_shift($unlink_nids);
+            $field_values[$index]['uri'] = 'entity:' . $type[0] . '/' . $nid;
+            $field_values[$index]['title'] = $field_data['title'];
+            $field_values[$index]['options'] = $field_data['options'];
+          }
+          else {
+            if (isset($field_data['links']) && isset($field_data['links']['related'])) {
+              $referenced_entities_response = $this->getHttpClient()->get($field_data['links']['related'])
+                ->getBody()
+                ->getContents();
+              $referenced_entities_json = Json::decode($referenced_entities_response);
+
+              if (!isset($referenced_entities_json['errors'])) {
+                $referenced_entities_ids = $this->importEntityListData($referenced_entities_json['data']);
+                $type = explode('--',$field_data['data']['type']);
+                $field_values[$index]['uri'] = 'entity:' .$type[0] . '/' .$referenced_entities_ids[0];
+                $field_values[$index]['title'] = $field_data['title'];
+                $field_values[$index]['options'] = $field_data['options'];
+              }
+            }
+          }
+        }
+        $entity->set($field_name, $field_values);
+      }
+    }
id.aleks’s picture

Issue tags: +LutskGCW20

Tagging for Drupal Global Contribution Weekend

Grimreaper’s picture

Hello @id.aleks,

Did you have time to work on this issue during the contribution weekend?

id.aleks’s picture

Issue tags: -LutskGCW20

Hello @Grimreaper. Unfortunately no. So I'm removing the LutskGCW20 tag for this issue.

Grimreaper’s picture

Version: 8.x-2.x-dev » 8.x-3.x-dev
Parent issue: » #3082611: Entity share 3.x
Grimreaper’s picture

To do this feature:

  1. Implements an import plugin
  2. If think it can be implemented at the prepare_importable_entity_data stage
  3. It will parse a link enhanced using the uuid_link field enhancer from JSON:API Extras.
  4. From the entity type, bundle and uuid, from the enhanced data, the JSON:API URL of the referenced entity can be requested and imported
  5. Maybe also inherit from the entity reference plugin to set a max recursion depth

dgroene made their first commit to this issue’s fork.

dgroene’s picture

I created a plugin and merge request- https://git.drupalcode.org/project/entity_share/-/merge_requests/10 . Apologies if this is not the right process.

Grimreaper’s picture

Assigned: Unassigned » Grimreaper
Status: Active » Needs work

Hello,

@dgroene Very good MR! Thanks!

Before fixing the review point I will write an automated tests to ensure it is working.

Grimreaper’s picture

Done, now we should see tests only failing. And with new plugins it will be ok. I have also updated documentation.

  • Grimreaper authored a37a7e7 on 8.x-3.x
    Issue #3064276 by Grimreaper: Tests: Parse link fields to get referenced...
  • dgroene authored f4e9a6c on 8.x-3.x
    Issue #3064276 by dgroene, Grimreaper: Parse link fields to get...

Grimreaper’s picture

Assigned: Grimreaper » Unassigned
Status: Needs work » Fixed

And merged!

Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.