Hello everyone,

I am trying to programmatically copy a field from 1 content type to another one in D8. What works well with my code snippet: the content type fields are copied, the form display settings (manage form display) are copied.

What doesn't work: the display settings (manage display) are not. For example, a field with label "hidden" in the source content type will show label "above" after copy.

How are we supposed to programmatically get/set the display settings (label and format) of a field?

Thanks a lot for your answers.

Comments

rockthedrop’s picture

I'm trying to do the same thing, either programmatically or scripted (during migration) via drush.

Other Settings are fine. I need to programmatically switch the form display widget to "Autocomplete (Tags Style"

thank you for any assistance or update.

useernamee’s picture

I only needed to get the manage display settings and I did it like this: (I needed for each view mode)

// Get selected view modes for bundle
$view_modes = \Drupal::service('entity_display.repository')
  ->getViewModeOptionsByBundle(
    $entity_type, $bundle
  );

// Get format settings for field
foreach (array_keys($view_modes) as $view_mode) {
  $settings = \Drupal::service('entity_type.manager')
    ->getStorage('entity_view_display')
    ->load($entity_type . '.' . $bundle . '.' . $view_mode)
    ->getRenderer($field_name)
    ->getSettings();
  // Get settings I need.
}
    

Hope it helps.

vasanthanand’s picture

How to set the display of particular field to hidden  by default progrmatically

colepacak’s picture

thanks, useernamee. this is super helpful.

anybody’s picture

Also see this documentation for helpful snippets: https://www.drupal.org/docs/drupal-apis/update-api/updating-entities-and...

http://www.DROWL.de || Professionelle Drupal Lösungen aus Ostwestfalen-Lippe (OWL)
http://www.webks.de || webks: websolutions kept simple - Webbasierte Lösungen die einfach überzeugen!
http://www.drupal-theming.com || Individuelle Responsive Themes