You are here

public function BundleEntityDuplicator::duplicateDisplays in Entity API 8

Duplicates the bundle entity's view/form displays.

Parameters

\Drupal\Core\Config\Entity\ConfigEntityInterface $bundle_entity: The bundle entity.

string $target_bundle_id: The target bundle ID.

Throws

\InvalidArgumentException Thrown if the given entity is not a bundle entity.

Overrides BundleEntityDuplicatorInterface::duplicateDisplays

1 call to BundleEntityDuplicator::duplicateDisplays()
BundleEntityDuplicator::duplicate in src/BundleEntityDuplicator.php
Duplicates the bundle entity, its fields and displays.

File

src/BundleEntityDuplicator.php, line 78

Class

BundleEntityDuplicator

Namespace

Drupal\entity

Code

public function duplicateDisplays(ConfigEntityInterface $bundle_entity, $target_bundle_id) {
  $entity_type = $bundle_entity
    ->getEntityType();
  $bundle_of = $entity_type
    ->getBundleOf();
  if (!$bundle_of) {
    throw new \InvalidArgumentException(sprintf('The "%s" entity type is not a bundle entity type.', $entity_type
      ->id()));
  }
  if (empty($target_bundle_id)) {
    throw new \InvalidArgumentException('The $target_bundle_id must not be empty.');
  }
  $id_prefix = $bundle_of . '.' . $bundle_entity
    ->id() . '.';
  $form_displays = $this
    ->loadEntities('entity_form_display', $id_prefix);
  $view_displays = $this
    ->loadEntities('entity_view_display', $id_prefix);
  foreach ($form_displays as $form_display) {

    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
    $duplicate_form_display = $form_display
      ->createDuplicate();
    $duplicate_form_display
      ->set('id', $bundle_of . '.' . $target_bundle_id . '.' . $form_display
      ->getMode());
    $duplicate_form_display
      ->set('bundle', $target_bundle_id);
    $duplicate_form_display
      ->save();
  }
  foreach ($view_displays as $view_display) {

    /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
    $duplicate_view_display = $view_display
      ->createDuplicate();
    $duplicate_view_display
      ->set('id', $bundle_of . '.' . $target_bundle_id . '.' . $view_display
      ->getMode());
    $duplicate_view_display
      ->set('bundle', $target_bundle_id);
    $duplicate_view_display
      ->save();
  }
}