EntityDuplicateController.php in Entity API 8
File
src/Controller/EntityDuplicateController.php
View source
<?php
namespace Drupal\entity\Controller;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityDuplicateController implements ContainerInjectionInterface {
use StringTranslationTrait;
protected $entityRepository;
protected $entityTypeManager;
protected $formBuilder;
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder, TranslationInterface $string_translation) {
$this->entityRepository = $entity_repository;
$this->entityTypeManager = $entity_type_manager;
$this->formBuilder = $form_builder;
$this->stringTranslation = $string_translation;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('entity_type.manager'), $container
->get('form_builder'), $container
->get('string_translation'));
}
public function form(RouteMatchInterface $route_match) {
$entity_type_id = $route_match
->getRouteObject()
->getDefault('entity_type_id');
$source_entity = $route_match
->getParameter($entity_type_id);
$entity = $source_entity
->createDuplicate();
$form_object = $this->entityTypeManager
->getFormObject($entity_type_id, 'duplicate');
$form_object
->setEntity($entity);
$form_object
->setSourceEntity($source_entity);
$form_state = new FormState();
return $this->formBuilder
->buildForm($form_object, $form_state);
}
public function title(RouteMatchInterface $route_match) {
$entity_type_id = $route_match
->getRouteObject()
->getDefault('entity_type_id');
$source_entity = $route_match
->getParameter($entity_type_id);
$source_entity = $this->entityRepository
->getTranslationFromContext($source_entity);
return $this
->t('Duplicate %label', [
'%label' => $source_entity
->label(),
]);
}
}