You are here

class EntityDuplicateSubscriber in Entity API 8

Hierarchy

  • class \Drupal\entity\EventSubscriber\EntityDuplicateSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of EntityDuplicateSubscriber

1 string reference to 'EntityDuplicateSubscriber'
entity.services.yml in ./entity.services.yml
entity.services.yml
1 service uses EntityDuplicateSubscriber
entity.entity_duplicate_subscriber in ./entity.services.yml
Drupal\entity\EventSubscriber\EntityDuplicateSubscriber

File

src/EventSubscriber/EntityDuplicateSubscriber.php, line 10

Namespace

Drupal\entity\EventSubscriber
View source
class EntityDuplicateSubscriber implements EventSubscriberInterface {

  /**
   * The bundle entity duplicator.
   *
   * @var \Drupal\entity\BundleEntityDuplicatorInterface
   */
  protected $bundleEntityDuplicator;

  /**
   * Constructs a new EntityDuplicateSubscriber object.
   *
   * @param \Drupal\entity\BundleEntityDuplicatorInterface $bundle_entity_duplicator
   *   The bundle entity duplicator.
   */
  public function __construct(BundleEntityDuplicatorInterface $bundle_entity_duplicator) {
    $this->bundleEntityDuplicator = $bundle_entity_duplicator;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      EntityEvents::ENTITY_DUPLICATE => [
        'onEntityDuplicate',
      ],
    ];
    return $events;
  }

  /**
   * Duplicates bundle fields and displays.
   *
   * @param \Drupal\entity\Event\EntityDuplicateEvent $event
   *   The entity duplicate event.
   */
  public function onEntityDuplicate(EntityDuplicateEvent $event) {
    $entity = $event
      ->getEntity();
    if ($entity
      ->getEntityType()
      ->getBundleOf()) {

      /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $source_entity */
      $source_entity = $event
        ->getSourceEntity();
      $this->bundleEntityDuplicator
        ->duplicateFields($source_entity, $entity
        ->id());
      $this->bundleEntityDuplicator
        ->duplicateDisplays($source_entity, $entity
        ->id());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityDuplicateSubscriber::$bundleEntityDuplicator protected property The bundle entity duplicator.
EntityDuplicateSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EntityDuplicateSubscriber::onEntityDuplicate public function Duplicates bundle fields and displays.
EntityDuplicateSubscriber::__construct public function Constructs a new EntityDuplicateSubscriber object.