diff --git a/core/lib/Drupal/Core/Entity/ContentEntityForm.php b/core/lib/Drupal/Core/Entity/ContentEntityForm.php index 92bbbae42f98ac98acef95ae7d4de75ce8441033..958e26b042fde48b72e3baa9b6cd326e7ab0ceff 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityForm.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityForm.php @@ -15,13 +15,6 @@ */ class ContentEntityForm extends EntityForm implements ContentEntityFormInterface { - /** - * The entity manager. - * - * @var \Drupal\Core\Entity\EntityManagerInterface - */ - protected $entityManager; - /** * The entity being used by this form. * @@ -43,19 +36,28 @@ class ContentEntityForm extends EntityForm implements ContentEntityFormInterface */ protected $time; + /** + * The entity repository service. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; + /** * Constructs a ContentEntityForm object. * - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository service. * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info * The entity type bundle service. * @param \Drupal\Component\Datetime\TimeInterface $time * The time service. */ - public function __construct(EntityManagerInterface $entity_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { - $this->entityManager = $entity_manager; - + public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { + if ($entity_repository instanceof EntityManagerInterface) { + @trigger_error('Passing the entity.manager service to ContentEntityForm::__construct() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Pass the entity.repository service instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); + } + $this->entityRepository = $entity_repository; $this->entityTypeBundleInfo = $entity_type_bundle_info ?: \Drupal::service('entity_type.bundle.info'); $this->time = $time ?: \Drupal::service('datetime.time'); } @@ -65,7 +67,7 @@ public function __construct(EntityManagerInterface $entity_manager, EntityTypeBu */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager'), + $container->get('entity.repository'), $container->get('entity_type.bundle.info'), $container->get('datetime.time') ); @@ -307,7 +309,7 @@ protected function initFormLangcodes(FormStateInterface $form_state) { // Imply a 'view' operation to ensure users edit entities in the same // language they are displayed. This allows to keep contextual editing // working also for multilingual entities. - $form_state->set('langcode', $this->entityManager->getTranslationFromContext($this->entity)->language()->getId()); + $form_state->set('langcode', $this->entityRepository->getTranslationFromContext($this->entity)->language()->getId()); } } diff --git a/core/modules/book/src/Form/BookOutlineForm.php b/core/modules/book/src/Form/BookOutlineForm.php index 1ce771e0de25b5beb16c897288877ebaaf9442cb..a305681d5349622ebf0ddaa14fb7c72a05e1d8ae 100644 --- a/core/modules/book/src/Form/BookOutlineForm.php +++ b/core/modules/book/src/Form/BookOutlineForm.php @@ -5,7 +5,7 @@ use Drupal\book\BookManagerInterface; use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Entity\ContentEntityForm; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; @@ -35,8 +35,8 @@ class BookOutlineForm extends ContentEntityForm { /** * Constructs a BookOutlineForm object. * - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. * @param \Drupal\book\BookManagerInterface $book_manager * The BookManager service. * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info @@ -44,8 +44,8 @@ class BookOutlineForm extends ContentEntityForm { * @param \Drupal\Component\Datetime\TimeInterface $time * The time service. */ - public function __construct(EntityManagerInterface $entity_manager, BookManagerInterface $book_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { - parent::__construct($entity_manager, $entity_type_bundle_info, $time); + public function __construct(EntityRepositoryInterface $entity_repository, BookManagerInterface $book_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { + parent::__construct($entity_repository, $entity_type_bundle_info, $time); $this->bookManager = $book_manager; } @@ -54,7 +54,7 @@ public function __construct(EntityManagerInterface $entity_manager, BookManagerI */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager'), + $container->get('entity.repository'), $container->get('book.manager'), $container->get('entity_type.bundle.info'), $container->get('datetime.time') diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 00647ad0422b029d8613e6d24f8bf98d39e0ae7a..69c508ae038f51e5ed54ae2e4361b5080cbac231 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -9,7 +9,8 @@ use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityConstraintViolationListInterface; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityFieldManagerInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\RendererInterface; @@ -37,24 +38,32 @@ class CommentForm extends ContentEntityForm { */ protected $renderer; + /** + * The entity field manager. + * + * @var \Drupal\Core\Entity\EntityFieldManagerInterface; + */ + protected $entityFieldManager; + /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager'), + $container->get('entity.repository'), $container->get('current_user'), $container->get('renderer'), $container->get('entity_type.bundle.info'), - $container->get('datetime.time') + $container->get('datetime.time'), + $container->get('entity_field.manager') ); } /** * Constructs a new CommentForm. * - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager service. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. * @param \Drupal\Core\Render\RendererInterface $renderer @@ -64,10 +73,11 @@ public static function create(ContainerInterface $container) { * @param \Drupal\Component\Datetime\TimeInterface $time * The time service. */ - public function __construct(EntityManagerInterface $entity_manager, AccountInterface $current_user, RendererInterface $renderer, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { - parent::__construct($entity_manager, $entity_type_bundle_info, $time); + public function __construct(EntityRepositoryInterface $entity_repository, AccountInterface $current_user, RendererInterface $renderer, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, EntityFieldManagerInterface $entity_field_manager = NULL) { + parent::__construct($entity_repository, $entity_type_bundle_info, $time); $this->currentUser = $current_user; $this->renderer = $renderer; + $this->entityFieldManager = $entity_field_manager ?: \Drupal::service('entity_field.manager'); } /** @@ -76,9 +86,9 @@ public function __construct(EntityManagerInterface $entity_manager, AccountInter public function form(array $form, FormStateInterface $form_state) { /** @var \Drupal\comment\CommentInterface $comment */ $comment = $this->entity; - $entity = $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId()); + $entity = $this->entityTypeManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId()); $field_name = $comment->getFieldName(); - $field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; + $field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; $config = $this->config('user.settings'); // In several places within this function, we vary $form on: @@ -383,8 +393,8 @@ public function save(array $form, FormStateInterface $form_state) { } $query = []; // Find the current display page for this comment. - $field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$field_name]; - $page = $this->entityManager->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page')); + $field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$field_name]; + $page = $this->entityTypeManager->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page')); if ($page > 0) { $query['page'] = $page; } diff --git a/core/modules/contact/src/MessageForm.php b/core/modules/contact/src/MessageForm.php index 3c4030c2ab1b884f3f7d279f216261363f42d967..f273e52cf07a6ce97b60a5df232415d535c902d0 100644 --- a/core/modules/contact/src/MessageForm.php +++ b/core/modules/contact/src/MessageForm.php @@ -5,7 +5,7 @@ use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\Entity\ContentEntityForm; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Flood\FloodInterface; use Drupal\Core\Form\FormStateInterface; @@ -57,8 +57,8 @@ class MessageForm extends ContentEntityForm { /** * Constructs a MessageForm object. * - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. * @param \Drupal\Core\Flood\FloodInterface $flood * The flood control mechanism. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager @@ -72,8 +72,8 @@ class MessageForm extends ContentEntityForm { * @param \Drupal\Component\Datetime\TimeInterface $time * The time service. */ - public function __construct(EntityManagerInterface $entity_manager, FloodInterface $flood, LanguageManagerInterface $language_manager, MailHandlerInterface $mail_handler, DateFormatterInterface $date_formatter, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { - parent::__construct($entity_manager, $entity_type_bundle_info, $time); + public function __construct(EntityRepositoryInterface $entity_repository, FloodInterface $flood, LanguageManagerInterface $language_manager, MailHandlerInterface $mail_handler, DateFormatterInterface $date_formatter, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { + parent::__construct($entity_repository, $entity_type_bundle_info, $time); $this->flood = $flood; $this->languageManager = $language_manager; $this->mailHandler = $mail_handler; @@ -85,7 +85,7 @@ public function __construct(EntityManagerInterface $entity_manager, FloodInterfa */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager'), + $container->get('entity.repository'), $container->get('flood'), $container->get('language_manager'), $container->get('contact.mail_handler'), @@ -109,7 +109,7 @@ public function form(array $form, FormStateInterface $form_state) { '#theme_wrappers' => ['container__preview'], '#attributes' => ['class' => ['preview']], ]; - $form['preview']['message'] = $this->entityManager->getViewBuilder('contact_message')->view($message, 'full'); + $form['preview']['message'] = $this->entityTypeManager->getViewBuilder('contact_message')->view($message, 'full'); } $form['name'] = [ diff --git a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php index 60e086b13329d1c9257bdd114169f8c2007d0625..1e0ecd9aea9ce9bb124e65c46b3ad88c64b9dd21 100644 --- a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php +++ b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php @@ -4,7 +4,7 @@ use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Entity\ContentEntityForm; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageManagerInterface; @@ -43,8 +43,8 @@ class MenuLinkContentForm extends ContentEntityForm { /** * Constructs a MenuLinkContentForm object. * - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector * The menu parent form selector service. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager @@ -56,8 +56,8 @@ class MenuLinkContentForm extends ContentEntityForm { * @param \Drupal\Component\Datetime\TimeInterface $time * The time service. */ - public function __construct(EntityManagerInterface $entity_manager, MenuParentFormSelectorInterface $menu_parent_selector, LanguageManagerInterface $language_manager, PathValidatorInterface $path_validator, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { - parent::__construct($entity_manager, $entity_type_bundle_info, $time); + public function __construct(EntityRepositoryInterface $entity_repository, MenuParentFormSelectorInterface $menu_parent_selector, LanguageManagerInterface $language_manager, PathValidatorInterface $path_validator, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { + parent::__construct($entity_repository, $entity_type_bundle_info, $time); $this->menuParentSelector = $menu_parent_selector; $this->pathValidator = $path_validator; } @@ -67,7 +67,7 @@ public function __construct(EntityManagerInterface $entity_manager, MenuParentFo */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager'), + $container->get('entity.repository'), $container->get('menu.parent_form_selector'), $container->get('language_manager'), $container->get('path.validator'), diff --git a/core/modules/node/src/NodeForm.php b/core/modules/node/src/NodeForm.php index f7756349227a69a31abcf93e8f4ad9d9f3136ec4..e1201a1a9deb70c58dc8610d099e25b6a6170a7e 100644 --- a/core/modules/node/src/NodeForm.php +++ b/core/modules/node/src/NodeForm.php @@ -4,7 +4,7 @@ use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Entity\ContentEntityForm; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; @@ -35,8 +35,8 @@ class NodeForm extends ContentEntityForm { /** * Constructs a NodeForm object. * - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory * The factory for the temp store object. * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info @@ -46,8 +46,8 @@ class NodeForm extends ContentEntityForm { * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. */ - public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user) { - parent::__construct($entity_manager, $entity_type_bundle_info, $time); + public function __construct(EntityRepositoryInterface $entity_repository, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user) { + parent::__construct($entity_repository, $entity_type_bundle_info, $time); $this->tempStoreFactory = $temp_store_factory; $this->currentUser = $current_user; } @@ -57,7 +57,7 @@ public function __construct(EntityManagerInterface $entity_manager, PrivateTempS */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager'), + $container->get('entity.repository'), $container->get('tempstore.private'), $container->get('entity_type.bundle.info'), $container->get('datetime.time'), diff --git a/core/modules/user/src/AccountForm.php b/core/modules/user/src/AccountForm.php index d6959681f48ac46672b404d0a201f44067d91d07..9301f80e355d299b0302b8df0cd78a4616de14c0 100644 --- a/core/modules/user/src/AccountForm.php +++ b/core/modules/user/src/AccountForm.php @@ -6,7 +6,7 @@ use Drupal\Component\Utility\Crypt; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityConstraintViolationListInterface; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; @@ -31,8 +31,8 @@ abstract class AccountForm extends ContentEntityForm { /** * Constructs a new EntityForm object. * - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info @@ -40,8 +40,8 @@ abstract class AccountForm extends ContentEntityForm { * @param \Drupal\Component\Datetime\TimeInterface $time * The time service. */ - public function __construct(EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { - parent::__construct($entity_manager, $entity_type_bundle_info, $time); + public function __construct(EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { + parent::__construct($entity_repository, $entity_type_bundle_info, $time); $this->languageManager = $language_manager; } @@ -50,7 +50,7 @@ public function __construct(EntityManagerInterface $entity_manager, LanguageMana */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager'), + $container->get('entity.repository'), $container->get('language_manager'), $container->get('entity_type.bundle.info'), $container->get('datetime.time') diff --git a/core/tests/Drupal/Tests/Core/Entity/ContentEntityFormTest.php b/core/tests/Drupal/Tests/Core/Entity/ContentEntityFormTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d4f884575be314ada026ef809c296f6cde99bbd1 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Entity/ContentEntityFormTest.php @@ -0,0 +1,28 @@ +prophesize(EntityManagerInterface::class)->reveal(); + $entity_type_bundle_info = $this->prophesize(EntityTypeBundleInfoInterface::class)->reveal(); + $time = $this->prophesize(TimeInterface::class)->reveal(); + new ContentEntityForm($entity_manager, $entity_type_bundle_info, $time); + } + +}