You are here

class DisplaySettingsCopier in Field tools 8

Contains methods for cloning displays.

Hierarchy

Expanded class hierarchy of DisplaySettingsCopier

1 file declares its use of DisplaySettingsCopier
EntityDisplaySettingsBulkCopyForm.php in src/Form/EntityDisplaySettingsBulkCopyForm.php
1 string reference to 'DisplaySettingsCopier'
field_tools.services.yml in ./field_tools.services.yml
field_tools.services.yml
1 service uses DisplaySettingsCopier
field_tools.display_settings_copier in ./field_tools.services.yml
Drupal\field_tools\DisplaySettingsCopier

File

src/DisplaySettingsCopier.php, line 13

Namespace

Drupal\field_tools
View source
class DisplaySettingsCopier {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a new FieldCloner.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
    $this->entityTypeManager = $entity_type_manager;
    $this->moduleHandler = $module_handler;
  }

  /**
   * Copies the display settings for a field to another bundle.
   *
   * @param FieldDefinitionInterface $field_definition
   *  The field definition.
   * @param EntityDisplayBase $source_entity_display
   *  The source display, view or form.
   * @param $destination_bundle
   *  The destination bundle.
   */
  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.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DisplaySettingsCopier::$entityTypeManager protected property The entity type manager.
DisplaySettingsCopier::$moduleHandler protected property The module handler.
DisplaySettingsCopier::copyDisplaySettings public function Copies the display settings for a field to another bundle.
DisplaySettingsCopier::__construct public function Constructs a new FieldCloner.