Problem/Motivation

Entity View Mode stores everything in a single variable. This makes exporting very difficult.

Proposed resolution

Move everything into a table that matches the structure of Display Suite. This way, DS can rely on Entity View Mode.

Remaining tasks

Add the schema and updates for the new db backend.
Make the UI work with the new db backend.
Make the hook api work with any changes that where needed to make this change.
Make sure the tests still pass.

Features exporting is being handled in another issue, however, this should be committed after features exporting is done. #1425620: Features Module Support

User interface changes

None.

API changes

None. Unless, a module is attempting to read from the variable table directly.

Data model changes

Moving data storage from a single serialized variable to a custom table.

Original Post
It would be great if this module could become a standalone building block of Display Suite, rather than the two competing on functionality.

Would it suffice as a first step to move this module to using the {ds_view_modes} table to store view modes rather than a single variable?

Having them in a table would help with exportability too: #1425620: Features Module Support.

Comments

gagarine’s picture

I think using the same table can be quiet a problem with installation/desinstallion/update. But using a new table with the same structure is a first step. DS can after add this module as a dependency.

dave reid’s picture

As great as it would be, there are huge red flags with depending on another module's schema without actually declaring yourself as a dependency, so I'm not ready to take that risk yet.

joachim’s picture

In that case, how about we move to a table {entity_view_mode} and when we're ready with that we can talk to DS about them changing to depend on us?

lpalgarvio’s picture

i agree with a {entity_view_mode} table

dave reid’s picture

Title: move towards compatibility with Display Suite? » Store view modes in an entity_view_mode table (compatible with Display Suite?)
Category: task » feature

Re-titling for better visibility.

dave reid’s picture

If we used a table we possibly couldn't use ctools exportables as we possibly cannot rely on things that need database schemas in hook_entity_info(). We'd have to manually maintain that code for the UI and export/import handling, which is not ideal at all. Unless it can be proven that it would be ok to use ctools exportables and schema in hook_entity_info() for Drupal 7, I'm very tenative on this solution.

damienmckenna’s picture

Is there a specific reason why using variables is not acceptable? Most sites would only need a few variables.

damienmckenna’s picture

Put another way, could DS be updated to use the same variables?

joachim’s picture

> as we possibly cannot rely on things that need database schemas in hook_entity_info()

That's not the case -- node_entity_info() relies on the database to define node types.

joachim’s picture

> Is there a specific reason why using variables is not acceptable? Most sites would only need a few variables.

Currently, all the user-defined view modes are stored in a single variable. This is really bad for exportability: consider if you want one feature for your site's news system, another for your site events, and so on.

That could be addressed by splitting up the variable into one variable per entity type. But then you'd still have the problem that the node view mode 'News scroll' is intended for article nodes, and should be in the 'news system' feature, while the 'event calendar pop-up' view mode is intended for to event nodes, and should be in the 'event calendar' feature. So you'd need one variable per view mode, which is getting silly!

There is also a small performance consideration. The only time that Drupal core gets told about our custom view modes is during a hook_entity_info() cache rebuild (and our own admin UI). The rest of the time, it doesn't talk to us at all. Therefore, if we use the variable system, we're keeping our list of view modes in memory twice: once for our variable, and once inside the entity info array.

Using a database table may seem heavier at first, but it would be a table that Drupal totally ignores unless it's rebuilding hook_entity_info() cache.

joachim’s picture

Status: Active » Needs review
StatusFileSize
new8.88 KB

Here's a patch that converts the storage to being in a table, and upgrades the existing variable.

The next step would be to enable this table for CTools exportables.

However, there are a number of problems:

- view mode machine names are not unique, because different entity types can use the same name. CTools requires all exportables to have a unique machine name. So here we might have to fake it, and add an object key field to our database table made up of, say "$entity_type-$view_mode_name". (Assuming CTools doesn't mind the hyphen!)
- CTools exportables returns objects, but the view mode data we pass around in the module is an array.
- Furthermore, CTools expects a default hook to return objects, and to have them in a flat array. So we can't use hook_entity_view_mode_info(), which is keyed by entity name then view mode. So we'd end up with *two* hooks to specify view modes in code: hook_entity_view_mode_info(), and the CTools one.

Status: Needs review » Needs work

The last submitted patch, 1702530.entity_view_mode.table-storage.patch, failed testing.

damienmckenna’s picture

Two comments:

  • Shouldn't the view modes be stored per entity bundle?
  • Shouldn't the separator be two underlines?
joachim’s picture

> Shouldn't the view modes be stored per entity bundle?

View modes aren't per entity bundle. In hook_entity_info() they are per entity type. The ticky boxes in this module's UI for entity bundles appear to be a fiction that passes something on to Field API to do with the 'custom settings' option. I didn't investigate much further than that!

> Shouldn't the separator be two underlines?

Quite possibly. I'm not sure which is safest to use!

dobe’s picture

Issue summary: View changes
StatusFileSize
new12.18 KB

Patch failed because we need to delete the old record when changing machine_name.

dobe’s picture

Status: Needs work » Needs review
frob’s picture

Status: Needs review » Needs work

I wasn't able to apply the patch, git reported fatal: unrecognized input

However, just looking at the patch, it doesn't add a .install file to update existing view modes to use the db, it doesn't add a schema to add the new table. So I don't think it does what it is supposed to do.

dobe’s picture

Sry about that. Try this one. As for the unrecognized input likely an issue with line endings. Should be fixed.

dobe’s picture

Status: Needs work » Needs review
socialnicheguru’s picture

Status: Needs review » Needs work

I can't use the patch:

patch -p1 < 1702530-17.entity_view_mode.table-storage.patch
patch unexpectedly ends in middle of line
patch: **** Only garbage was found in the patch input.

dobe’s picture

LOL I have such a hard time with windows providing good patches.... Here is an updated. It applied cleanly on my linux box. I should have known with the difference in filesize.

dobe’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 21: 1702530-21_entity_view_mode_table-storage.patch, failed testing.

frob’s picture

I can confirm that the patch applies cleanly.

dobe’s picture

Status: Needs work » Needs review
StatusFileSize
new10.51 KB

The last failing test is due to a test that checks if the functionality still works when a manual variable_set() is used to update entity_view_mode. I replaced it with a direct db update test. Not sure if we want this or not but let see if it works!

Status: Needs review » Needs work

The last submitted patch, 25: 1702530-25_entity_view_mode_table-storage.patch, failed testing.

dobe’s picture

StatusFileSize
new10.51 KB
dobe’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 27: 1702530-27_entity_view_mode_table-storage.patch, failed testing.

dobe’s picture

Status: Needs work » Needs review
StatusFileSize
new10.51 KB

Hmm. This is my last attempt at blindly swinging before I need to setup a testing environment on this box.

Status: Needs review » Needs work

The last submitted patch, 30: 1702530-29_entity_view_mode_table-storage.patch, failed testing.

dobe’s picture

Status: Needs work » Needs review
StatusFileSize
new10.72 KB

Maybe simpletest doesn't like drupal_write_record? Give this a quick try.

Status: Needs review » Needs work

The last submitted patch, 32: 1702530-31_entity_view_mode_table-storage.patch, failed testing.

dobe’s picture

I am not sure what is failing here. I will have to check into it when I get in a testing environment.

frob’s picture

It looks like the variable_set call and the db_update call are not the same. You can see there is 'taxonomy_term' being set with an empty array in the variable set. I am not sure what that is doing, but it might be why these are not working.

frob’s picture

Assigned: Unassigned » frob

This is what I get for commenting, before I read the testbot fail message. The labels don't match.

Value array ( 'label' => 'Hook-defined view mode #3', 'custom settings' => false, ) is identical to value array ( 'label' => 'DB-altered view mode', 'custom settings' => true, ).

The patch doesn't label things consistently.

frob’s picture

Before I start changing these test, I want to make sure this is working. So I will begin some manual testing to confirm the test is wrong.

I was just about to make the mistake of assuming that the test was wrong, so just change the test.

frob’s picture

I think I found it.

There is a problem somewhere with the custom settings. It likely has to do with "custom_settings" vs "custom settings"

What I have noticed is the custom settings being shown don't reflect what they are for custom db view modes.

frob’s picture

So I worked on this a bit, it still needs more work.

The module is no longer updating the field settings when the view mode is updated and visa versa. So more work to be done.

I have attached an interdiff so you can see what I changed. We have enough patches that we should start to do interdiffs.

frob’s picture

Assigned: frob » Unassigned
Issue summary: View changes
Related issues: +#1425620: Features Module Support

Updated the summary.

dobe’s picture

Hmm. I don't believe #39 is correct. I believe entity_get_info() returns "custom settings" not "custom_settings" however we have to store the data in the database with the underline. I think your patch may actually cause issues on the admin page. As you are setting "custom_settings" to a value that doesn't exist.

joachim’s picture

> It likely has to do with "custom_settings" vs "custom settings"

I filed an issue and a patch for that issue: #2508313: hook_entity_view_mode_info() 'custom_settings' property is not being respected.

frob’s picture

@dobe #39 was meant to address the admin pages being broken with the patch from #32.

The patch mentioned in #42 should fix the test issues, However, the admin will still be broken with the previous patch. This is because the module used to store everything with no underscore (this is how core field api needs it). When saving the module would change custom_settings into "custom settings"

My patch isn't right. that is why the issue is still marked as needs work. We need to figure out what we are going to do to keep everything in sync. I am thinking we need to leave the admin ui clean with custom_settings as it is in the DB and add some mapping from custom_settings into "custom settings" in the field_entity_view_mode_* functions. Keep the core specific conversions in the core portion and leave everything else as it is. Maybe we should just add a couple to/from mapping functions.

function _entity_view_mode_reformat_to_core_format($entity_view_mode) {
  // do conversion
  return $entity_view_mode;
}

function _entity_view_mode_reformat_from_core_format($entity_view_mode) {
  // do conversion
  return $entity_view_mode;
}
dobe’s picture

Status: Needs work » Needs review
StatusFileSize
new11.75 KB

This patch should pass tests. I removed the test to check the hooks after variable_set since we are not using it anymore and I don't really believe we need to test if the functionality works by modifying the database directly (not even sure simpletest supports this type of test).

This patch may also address https://www.drupal.org/node/2508313 by setting "custom settings" when we load from the db. I feel doing it here may be better as it makes sure that if someone uses those functions they shouldn't' have to do the conversion in there code. We also call one of the load functions in entity_view_mode_entity_info_alter().

This also solves the issue on the admin pages without modifying the admin file.

frob’s picture

The test that was set for testing after a variable set was testing view modes that where created in the ui. Otherwise, all that is being tested is the hook_implemented view modes. We need to still test the view modes that are created with the ui. I don't see how this could address #2508313: hook_entity_view_mode_info() 'custom_settings' property is not being respected.

Have you done any manual tests to make sure this patch works?

dobe’s picture

The test that was set for testing after a variable set was testing view modes that where created in the ui. Otherwise, all that is being tested is the hook_implemented view modes. We need to still test the view modes that are created with the ui.

The test that was removed has NOTHING to do with view modes created through the UI. Those tests are passing, they all happen in: function testEntityViewModes() which I did not have to modify (the first 58 tests, test the UI). I modified testInfoHooks() which most of the tests are still there. I only removed the one test that was used to test that the info hooks still worked after variable_set().

Have you done any manual tests to make sure this patch works?

I have done manual tests. Everything seems functional. But that is also where the community comes in.

I don't see how this could address #2508313: hook_entity_view_mode_info() 'custom_settings' property is not being respected.

The current patch does what #2508313 does in entity_view_mode_load() and entity_view_mode_load_all() now instead of entity_view_mode_entity_info_alter().

frob’s picture

Assigned: Unassigned » frob

testing with new patch from #44

frob’s picture

Assigned: frob » Unassigned
Issue summary: View changes
Status: Needs review » Needs work

The admin UI is broken.

From admin/config/system/entity-view-modes the user cannot set the view mode custom settings.

Also, the admin table column for showing if the Display mode has custom settings doesn't reflect the settings that are in the DB.

These are the things that I fixed in #39. I will not be submitting another patch any time soon, but I can still review your patches if you fix these bugs.

I updated the issue summary with things that are done and need to be done.

dobe’s picture

Status: Needs work » Needs review
StatusFileSize
new12.04 KB

Hmm. For some reason the unset in the save function showed back up which the first patch in this que removed... Not sure how that happened.

Status: Needs review » Needs work

The last submitted patch, 49: entity_view_mode-change_save_to_db-1702530-49.patch, failed testing.

denix’s picture

Version: 7.x-1.x-dev » 7.x-1.0-rc1
Component: Miscellaneous » Code
Status: Needs work » Needs review
StatusFileSize
new12.03 KB

This fix the admin UI

Status: Needs review » Needs work

The last submitted patch, 54: entity_view_mode-change_save_to_db-1702530-54.patch, failed testing.

denix’s picture

Version: 7.x-1.0-rc1 » 7.x-1.x-dev
StatusFileSize
new12.02 KB
denix’s picture

Status: Needs work » Needs review

The last submitted patch, 39: entity_view_mode-change_save_to_db-1702530-39.patch, failed testing.

frob’s picture

I queued a new test of #56. For some reason the last patch tested was an old one I wrote in #39.

epophoto’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new49.38 KB
new40.58 KB

I installed #56 locally and tested it adding a custom view mode, and looking at the presence of it in the database. I verified the view modes functionality within the content type structure interface, and deleted it.

denix’s picture

Is this going to be included in a next release?