Updated: Comment #N
Problem/Motivation
There is a lot of code that calls entity_get_view_modes() that duplicates effort by having to add a 'default' option when presenting view modes in an #options element. This code should also be moved to EntityManager class to remove old procedural code.
Proposed resolution
Move these procedural functions to methods in EntityManager class and add a couple of new methods to provide the display mode available options.
Remaining tasks
User interface changes
None
API changes
- Adds 4 new methods to EntityManagerInterface:
- getFormModes()
- getAllFormModes()
- getViewModes()
- getAllViewModes()
- getFormModeOptions()
- getViewModeOptions()
- Deprecates entity_get_view_mode_options() and entity_get_form_mode_options()
Original report by @Dave Reid
There is a lot of code that calls entity_get_view_modes() that duplicates effort by having to add a 'default' option when presenting view modes in an #options element.
Comment | File | Size | Author |
---|---|---|---|
#74 | interdiff.txt | 1.75 KB | jlbellido |
#74 | entity-display-mode-2154711-74.patch | 1.64 KB | jlbellido |
#68 | entity-display-mode-2154711-68.patch | 1.1 KB | jlbellido |
#63 | entity-display-mode-2154711-63.patch | 33.84 KB | tim.plunkett |
Comments
Comment #1
dawehner+1, this would be nice for views!
Comment #2
Dave ReidInitial version.
Comment #4
tim.plunkettThis is what I was describing on IRC today, putting methods on the storage controller.
I'm using the inline @var docblock form where things need to be injected. This will allow IDEs like PHPStorm to work until it is injected properly.
Comment #6
tim.plunkettI was just looking at that static earlier today. Let's see when it actually needs to be called (probably not during install!)
Comment #8
olli CreditAttribution: olli commentedFixed one fatal.
Comment #10
olli CreditAttribution: olli commentedComment #12
plopescRe-rolling after #2164827: Rename the entityInfo() and entityType() methods on EntityInterface and EntityStorageControllerInterface.
Also removing the checking of the displayMode status property to pass tests because the old implementations didn't do it. Should we change the tests and expected results and keep the checking to show only the modes which provides custom options?
Regards.
Comment #14
plopescForgot to remove an old parameter...
Comment #16
Dave ReidMy opinon is yes, we should fix the tests because users likely shouldn't be able to select view modes that aren't even customized. I still think it should be an additional parameter on the getDisplayModeOptions() if disabled display modes should be included in the result or not. Let the caller decide what it wants, but default to not including disabled display modes. Without this logic in that method, I have to duplicate a lot of work in order to filter out disabled ones when it could easily just be done in that method.
Comment #17
plopescSilly me... :(
Forgot to add the new file to the patch.
Sorry for the noise.
Comment #18
plopescSorry for the previous crossposting...
New patch fixing tests and adding the second parameter in
EntityDisplayModeStorageController::getDisplayModeOptions()
.Regards.
Comment #21
plopescOh, I must go to sleep... so many work today
I hope this works at least.
Comment #22
Berdir21: entity_display_mode-2154711-21.patch queued for re-testing.
Comment #24
BerdirNote: There's a drupal_static_reset() call for entity_get_view_modes() in entity_info_clear_cache(), once that is removed we can kill that function.
Hiding those fairly often used functions on a storage controller seems unfortunate.
Given that view and form modes and their underlying entity types are core concept of the entity system, Drupal\Core\Entity depending on it and they really should be in there anyway (#2031717: Make entity module not required), can't we add those methods to the entity manager? I'm worried about what @webchick will say about the changed calls here ;)
Comment #25
plopescRe-rolling, moving getDisplayModesByEntityType() from the storage controller to EntityManager::getViewModes($entity_type_id) and EntityManager::getFormModes($entity_type_id). Same with
getDisplayModesOptions()
.Regards.
Comment #27
plopescSilly me...
Comment #29
BerdirThanks, I think that looks much much nicer (the interdiff in #25). let's see what others have to say ;)
Comment #30
plopesc27: entity_display_mode-2154711-27.patch queued for re-testing.
Comment #31
plopescRe-rolling after #2165155: Change $entity_type to $entity_type_id and $entity_info to $entity_type/$entity_types
Comment #33
plopesc31: entity_display_mode-2154711-31.patch queued for re-testing.
Comment #35
plopesc31: entity_display_mode-2154711-31.patch queued for re-testing.
Comment #36
plopescStraight re-roll
Comment #37
plopescRe-rolling after #2095303: Rename 'field_entity' to 'field_config' and 'field_instance' to 'field_instance_config'
Comment #39
plopescComment #40
plopescCross-poting with #2045927: Replace all drupal_alter() deprecated function calls.. Re-rolling again
Comment #41
plopescRe-rolling after #2070369: Remove all calls to deprecated function language()
Comment #42
BerdirShould we initialize this to an empty array and set it back to = array() ?
EntityManager has the language manager injected, so use $this->languageManager.
Same, $this->cache->get()
Same for the module handler :)
Methods that return different structures based on the argument are somewhat discouraged. Should we maybe have getAllViewModes() and getViewModes($entity_type_id) ?
$this->entityManager :)
This one doesn't have the entity manager injected yet but we should probably do that.
This can use $this->entityManager() (method, not property)
The manual cache clear in the test at the end shouldn't be necessary. We can we move this to methods on the entity classes instead?
This also seems to have $this->entityManager.
Same :)
Same, also others in this class.
Comment #43
plopescThanks for your review @Berdir
New patch addressing following points:
\Drupal::entityManager()
when possible.CustomBlockBlock
EntityManagerInterface::getAllViewModes()
andEntityManagerInterface::getAllFormModes()
EntityDisplayModeBase::preSave()
andEntityDisplayModeBase::preDelete()
Regards
Comment #44
BerdirThey should probably call clearCachedDefinitions() on the entity manager now. Not sure if we want to add specific methods for that, to avoid having to re-parse the entity types for it.
The second needs the parent:: call and @inheritdoc
Comment #45
plopescOh sorry,
I forgot your reference to kill
entity_info_cache_clear()
in #24.I didn't add the call to
parent::preDelete()
because currently is empty. but you're right, that might change in the future.Regards
Comment #46
tim.plunkettLooks good, thanks @plopesc for bringing this home
Comment #47
alexpottentity_display_mode-2154711-45.patch no longer applies.
Comment #48
BerdirTrivial re-roll, so back to RTBC assuming that bot agrees.
Comment #50
tim.plunkett#2134857: PHPUnit test the entity base classes added some use statements, no changes.
Comment #52
Berdir50: entity_display_mode-2154711-50.patch queued for re-testing.
Comment #53
BerdirBack to RTBC. random fails--.
Comment #55
plopescStraight re-roll, #2216701: Random test failure in ToolbarAdminMenuTest removed some use statements.
Comment #56
plopescLet's go testbot!
Comment #57
plopescChange record draft added: View modes and form modes listing functions moved to EntityManager
Comment #59
tim.plunkettJust a case of another issue adding a method to the bottom of core/modules/entity/lib/Drupal/entity/EntityDisplayModeBase.php.
Comment #60
BerdirYep, adding methods at the end of a class is dangerous business :)
Comment #61
BerdirComment #62
alexpottentity-display-mode-2154711-59.patch no longer applies.
Comment #63
tim.plunkettSimple conflicts (use statements, changes at end of file), no real changes.
Comment #64
alexpottNice!
Committed 02f761b and pushed to 8.x. Thanks!
Fixed this during commit.
Comment #66
ianthomas_ukWhen do we plan to remove these, before Drupal 8 or before Drupal 9? This should be stated in the docblocks.
@deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
@deprecated in Drupal 8.0, will be removed in Drupal 9.0.
Comment #67
tim.plunkettBefore 8.0. I would have removed them here, but that would be too easy.
Comment #68
jlbellidoAdded to entity_get_form_modes and entity_get_view_modes functions.
Thanks @plopesc for support.
Comment #70
jlbellidoNot related fail, restesting.
Comment #71
jlbellido68: entity-display-mode-2154711-68.patch queued for re-testing.
Comment #73
ianthomas_ukIf these are no longer used we can just remove them. We try not to remove functions as part of big patches incase something goes wrong, but a small followup immediately afterwards is fine.
Comment #74
jlbellidoRemoved "entity_get_form_modes" and "entity_get_view_modes" from core/includes/entity.inc
Comment #75
ianthomas_ukComment #76
alexpottCommitted 1df1d3b and pushed to 8.x. Thanks!