0

I am writing a module to export drupal's content to another platform, the module provides a UI to manage content types and their fields to be shared on another platform via API.

So far i have managed to get node field values using this sample code

$nid = 4951;
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$data = [];
foreach ($node->getFields() as $name => $field) {
  $data[$name]  = $field->getString();
}

the $field->getString(); however do not provides reference entity values and only provide ids.

The out of the above code is like:

{"nid":"4951","uuid":"0fae7e9b-d860-43f0-9559-a8bd6bab22b7","vid":"1272611","langcode":"en","type":"article","revision_timestamp":"1612303794","revision_uid":"1166","revision_log":"","status":"1","uid":"866","title":"xyz Chapter April Update","created":"1555961981","changed":"1612303794","promote":"0","sticky":"0","default_langcode":"1","revision_default":"1","revision_translation_affected":"1","moderation_state":"published","scheduled_transition_date":"","scheduled_transition_state":"","metatag":"","path":"\/wisconsin-chapter-april-update, 17276, en","menu_link":"","body":"some this here, rich_text","field_article_author":"Wisconsin Chapter","field_article_chapter":"541","field_article_endnotes":"","field_article_questions_cta":"1","field_article_recommendation":"","field_article_recommended_link":"","field_article_related":"","field_article_related_link":"","field_article_resource_download":"","field_article_subhead":"Here's the latest news from the abc.","field_article_thumbnail":"","field_article_type":"news","field_blog_section":"","field_meta_tags":"","field_tax_article_type":"24, 28, 45","field_tax_audience":"","field_tax_breadcrumbs":"","field_tax_content_format":"50","field_tax_disease":"55, 56, 57, 58","field_tax_ga_type":"","field_tax_research_type":"","field_tax_topic":"90, 91, 92, 93, 94, 108, 109, 111"}

Scroll last and see for example field_tax_topic

Is there a direct or indirect way of automatically detecting reference entity field and extracting its values, also it should be dynamic solution and that must work with new field types as well.

Any pointer or direction to do it in the right way will help!

2
  • 2
    Don't reinvent the wheel. Use JsonAPI and/or views. – Hudri Apr 14 at 10:02
  • as @Hudri said, using JsonAPI , you can easily manage extracting referenced fields using include, then you can fetch data from endpoint – Alireza Tabatabaeian Apr 14 at 12:04
0

If you really really want to do this which you probably wont with JSON API Views and serialisation

https://www.drupal.org/docs/8/api/serialization-api/serialization-api-overview

eg:

dump(Drupal::service('serializer')->serializer->serialize($entity, 'json'));

But if you really want to ...

$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$data = [];
foreach ($node->getFields() as $name => $field) {
   // Get Type.
   $field_type = $node->get($name)->getFieldDefinition()->getType();
   //see https://drupal.stackexchange.com/questions/180373/how-to-get-the-list-of-field-types

   switch($field_type) {
      case 'text_with_summary':
      case 'text':
      case 'text_long: 
        $data[$name]  = $field->getString();
        break;
      case 'entity_reference':
      case 'entity_reference':
        $value = [];
        $items = $field->referencedEntities();
        if (!empty($items)) {
            foreach ($items as $item) {
                // here is the crappy part 
                $val_1 = $item->get('field_something')->value;
                $value[$item->id()] =  $val_1,
            }
        }
        $data[$name] = $value;
      break;
      // continue for other data types
      // and set a default !
   }
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.