0

In PHP code, in Drupal 8, how can I get the display names and the order of entity fields to be displayed in a particular view mode. I'm specifically trying to get the order of fields in node and paragraphs entities.

I've tried inspecting the node object, using the serializer service and REST interface. I can find the fields and data, but not this metadata.

1 Answer 1

1

In Drupal, the display of fields is controlled by display mode configuration entities, which define the configuration for field formatters and the such.

You cannot get this metadata by viewing the node itself (or other entity). You would have to load the display mode for it.

The config object is entity_view_display and may be accessible over RESTful endpoints. An example output (in YAML) looks like so:

content:
  body:
    type: text_default
    weight: 0
    label: hidden
    settings: {  }
    third_party_settings: {  }
    region: content
  variations:
    type: commerce_add_to_cart
    weight: 1
    label: hidden
    settings:
      default_quantity: '1'
      combine: true
      show_quantity: false
    third_party_settings: {  }
    region: content
hidden:
  created: true
  stores: true
  uid: true
2
  • Thanks. I'm now able to get content data for nodes with \Drupal::entityTypeManager()->getStorage('entity_view_display')->load("node" . '.' . "service" . '.' . "default"); but when trying the same thing with paragraphs (e.g., load("paragraph.service_paragraphs.default") I get an empty result. Inspecting the result with var_dump shows the data is there, but all the properties, including content, are protected. I think I've set the paragraphs access permissions appropriately... Sep 1, 2017 at 0:16
  • It could be permissions.. or it doesn't exist. If it's null then it'll generate based off of base field definitions and such on render. It's still a bit magical that way. Sep 1, 2017 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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