Closed (fixed)
Project:
Drupal core
Version:
8.3.x-dev
Component:
entity system
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
8 Dec 2014 at 16:48 UTC
Updated:
6 Mar 2017 at 08:24 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
jibranComment #3
almaudoh commentedComment #5
naveenvalechaRemoving the needs beta evaluation tag as its needed anymore. Could we mark it as deprecated in patch and will be removed in 9.0.x contrib modules might be using it ? if yes then we need a patch for it
Comment #6
joachim commentedWould be good to see before & after example code.
Comment #7
berdirThat's easy:
\Drupal::entityQuery('node')->... || $container->get('entity.query')->get($entity_type)
vs.
\Drupal::entityTypeManager()->getStorage('node')->getQuery()->..
Yes the second is a bit longer, but the point is that when you work with entity query, you also need the storage in 95%+ of the cases to actually load the entities you queried and then you need the storage anyway. So most code will look like this then:
$storage = \Drupal::entityTypeManager()->getStorage('node'); (or properly injected)
$ids = $storage->getQuery()->...
$storage->load($ids);
Comment #8
berdirThe fun thing is that QueryFactory already calls the storage but on the entity manager. So this is kind of already deprecated, this just makes it official. And we save a bunch of method calls by calling it directly.
And a ton of injections, as written above, basically every single example that uses the query factory already has the storage injected, as you need that to do anything with the results anyway. This doesn't convert everything yet, just a part of it.
Comment #11
berdirKilled another 200 lines of code and fixed the failing test. total summary is now:
36 files changed, 181 insertions(+), 523 deletions(-)
The only usages that aren't converted now are in entity query tests, there are like a million usages there, converting probably makes sense as a separate patch.
Comment #12
berdirComment #14
berdirOops.
Comment #16
naveenvalechaFixing book module tests.
2 files changed, 2 insertions(+), 2 deletions(-)Comment #18
naveenvalechaFixed all the failures.
The patch is doing changes in services of book and forum module.Should we mark them as internal in this patch or do in a followup?
Comment #19
berdirSee the discussion in #2624770: Use more specific entity.manager services in core.services.yml in regards to constructor arguments and protected properties, those are implicitly @internal. And I think it's fairly unlikely that those services are overridden...
Comment #20
almaudoh commentedStill using entity manager here...
Comment #21
claudiu.cristeaNice work. Some small fixes.
Shouldn't we @trigger_error("...", E_USER_DEPRECATED) in both, ::get() and ::getAggregate()?
Unused statement:
use Drupal\Core\Entity\Query\QueryInterface;Let's save
$this->container->get('entity_type.manager')->getStorage('block')in a $storage var and reuse it.Let's add a line break after ->getQuery(). It's hard to read.
->getStorage()->getQuery() can go on the first line so we have the query object on the upper line.
It would be nice if we can format in this way all occurrences that we touch, with the query object on the first line followed by ->condition() on 2nd line.
Interesting, this was not used at all.
Unused too :)
Unrelated.
Unrelated.
EDIT: @almaudoh, we are not changing the entity manager in this patch.
Comment #22
claudiu.cristeaAlso, because we're deprecating here a service, we need a change note. Let's not forget also the IS update.
Comment #24
berdirReroll
#20: Changed. I actually changed the constructor there already because I was changing those lines anyway.
#21
1. Not sure, maybe. We still haven't got rid of all calls in core and unlike other places where we added it, I expect that this is used *a lot* in contrib and custom code. We could maybe do it as part of the issue to remove all remaining test-usages as well?
2. Fixed
3. Fixed
4. Fixed
5. I'm not sure about that rule (AFAIK our coding standards says to indent all calls) but changed for now.
6. & 7. Yeah, probably was removed at some point when refactoring things
8. Damn, thought I could sneak that in ;)
9. Fixed.
Also opened #2849873: Replaces usages of entity.query service in entity query tests.
Comment #25
berdirChange record: https://www.drupal.org/node/2849874
Comment #27
berdirActually, the reason I switch there is that entityQuery() uses entity_type.manager, that means keep using entityManager() would require us to mock both entity.manager and entity_type.manager, I think this is easier in that case. Reverted that.
Comment #28
claudiu.cristeaThat makes sense. I see #21 was addressed. Looks good for me.
Comment #29
catchNeeds a re-roll afaict.
Comment #30
claudiu.cristeaRerolled.
Comment #31
catchCommitted/pushed to 8.4.x and cherry-picked to 8.3.x. Thanks!
Comment #33
tim.plunkettThe 8.3.x commit message didn't come through, and the "fixed" didn't take...
Comment #34
claudiu.cristeaBut the commit exists in the codebase. Seems that d.o failed to register it.
Comment #35
catchThis happened on a couple of issues today fwiw..
Comment #36
xjmd.o also ignored it when @catch changed the branch. Weird... The commit is definitely there as others have said.
Comment #37
benjifisherI ran across this because removing net 12 lines from
FieldStorageAddForm.phpcausedgit applyto switch two hunks in #2847685: Update doc blocks for configureEntityFormDisplay() and configureEntityViewDisplay().I have a couple of questions about the change record.
I am still learning all this stuff, so I may be unclear on the distinction is between
entityTypeManagerandentityManager, but I see this as part of the patch, so I wonder if that is a typo in the change record:Maybe the change record could be expanded to explain more fully how to update existing code.
The other question is whether the change record should document the changes to the public API. In fact, I am surprised that this is allowed in a minor version update. Is it safe to assume that the constructor is never called except by the dependency injection system? In the file I am looking at, I see
Removing a parameter from a public function is a change to the API, right?
Comment #38
berdirConstructors, especially in forms and controllers are excluded from BC: https://www.drupal.org/core/d8-bc-policy
entityManager is deprecated in favor of entityTypeManager, some classes only had that available which is why I sticked to that to avoid making too many changes.
Comment #39
benjifisher@Berdir, thanks for the pointer and the explanation!
Comment #40
mradcliffeThat's annoying. I won't rely on Core code in the future and duplicate all of it so it's backwards-compatible for Drupal 8. :(
DefaultMenuLinkTreeManipulators was nice to override as I didn't actually have to write code for it - just pass in the correct parameters. Now I need to write a BC shim for it or create a new BC-breaking release in my module.
Comment #41
berdirI wrote a blogpost recently on how this can be done safely at least for services: http://www.md-systems.ch/blog/techblog/2016/12/17/how-safely-inject-addi...
Comment #42
mradcliffeThanks, Berdir. That makes sense for the situation when swapping classes.
I have a class though that only extends the default manipulator class so any service that I make needs to adhere to the constructor/API. Though similarly could write a service provider that detects what dependencies core is currently using, and then change my service appropriately. This API change is still disappointing and annoying to me despite the improvement of using EntityTypeManager (and it is the definition of an API change no matter what a policy dictates).
Edit: actually these approaches are still bad because you can't do the same logic for unit tests so unit tests will be broken for various versions of Drupal.
Comment #43
berdirYou might be able to doing it by adding a parent: name_of_core_service instead of defining the arguments yourself. But yes, doesn't work at least for unit tests.
I've been affected and annoyed by this as well. In commonly extended classes and when adding constructor arguments, we often add them as optional with a fallback, but when removing/renaming them, it's not so easy. But if we wouldn't be allowed to do this, it would be impossible for core to remove its own deprecated usages and would make improvements/new features in minor releases a lot harder and resulting in a lot of ugly, non-injected, non-testable code.