You are here

public function DisplaySettingsCopier::copyDisplaySettings in Field tools 8

Copies the display settings for a field to another bundle.

Parameters

FieldDefinitionInterface $field_definition: The field definition.

EntityDisplayBase $source_entity_display: The source display, view or form.

$destination_bundle: The destination bundle.

File

src/DisplaySettingsCopier.php, line 52

Class

DisplaySettingsCopier
Contains methods for cloning displays.

Namespace

Drupal\field_tools

Code

public function copyDisplaySettings(FieldDefinitionInterface $field_definition, EntityDisplayBase $source_entity_display, $destination_bundle) {
  $field_name = $field_definition
    ->getName();
  $component = $source_entity_display
    ->getComponent($field_name);

  // The entity type ID of the displays being copied to and from.
  $display_entity_type_id = $source_entity_display
    ->getEntityTypeId();

  // Load the corresponding display on the destination bundle.
  $destination_display = $this->entityTypeManager
    ->getStorage($display_entity_type_id)
    ->load($field_definition
    ->getTargetEntityTypeId() . '.' . $destination_bundle . '.' . $source_entity_display
    ->getMode());
  if (!empty($destination_display)) {
    if (is_null($component)) {
      $destination_display
        ->removeComponent($field_name);
    }
    else {
      $destination_display
        ->setComponent($field_name, $component);
    }

    // Save the display.
    $destination_display
      ->save();
  }

  // TODO complain if the destination doens't exist.
}