class DisplaySettingsCopier in Field tools 8
Contains methods for cloning displays.
Hierarchy
- class \Drupal\field_tools\DisplaySettingsCopier
Expanded class hierarchy of DisplaySettingsCopier
1 file declares its use of DisplaySettingsCopier
1 string reference to 'DisplaySettingsCopier'
1 service uses DisplaySettingsCopier
File
- src/
DisplaySettingsCopier.php, line 13
Namespace
Drupal\field_toolsView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DisplaySettingsCopier:: |
protected | property | The entity type manager. | |
DisplaySettingsCopier:: |
protected | property | The module handler. | |
DisplaySettingsCopier:: |
public | function | Copies the display settings for a field to another bundle. | |
DisplaySettingsCopier:: |
public | function | Constructs a new FieldCloner. |