I have a project where sitemanager have access to placing views in panels, but not access to views. Setting this up, I found that there was no option to expose the view mode for rendered entities to panels. Searching a bit showed that others have the same need, so I made a patch. Hopefully it can be of use to somebody else.

The logic of which list of view modes to show (what entity is listed) in the panel_pane is a bit flawed, as it depends on the base_table. Any suggestions of how to better do this would be greatly appreciated.

Patch coming up in first comment

Files: 
CommentFileSizeAuthor
#1 view_mode_as_exposed_content_pane_setting-2032293-1.patch5.11 KBdysrama
#9 interdiff.txt1.41 KBjhedstrom
#9 view-mode-pane-setting-2032293-09.patch6.18 KBjhedstrom
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es). View
#10 view-mode-pane-setting-2032293-10.patch6.75 KBdysrama
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es). View
#11 view-mode-pane-setting-2032293-11.patch7.82 KBdysrama
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch view-mode-pane-setting-2032293-11.patch. Unable to apply patch. See the log in the details link for more information. View
#14 view-mode-pane-setting-2032293-14.patch8.91 KBmrjmd
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es). View
#17 ctools-view-mode-pane-settings-2032293-17.patch9.2 KBndf
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es). View
#20 interdiff-2032293-14-17.txt1.93 KBndf
#22 ctools-view-mode-pane-settings-2032293-22.patch8.35 KBgmercer
#28 3 - after saving the panel.png364.17 KBkopeboy
#28 2 - after panelizing the entity.png565.29 KBkopeboy
#28 1 - normal view mode.png633.86 KBkopeboy
#24 ctools-view-mode-pane-settings-2032293-24.patch9.3 KBjoelstein
#26 ctools-view-mode-pane-settings-2032293-26.patch9.35 KBjoelstein

Comments

dysrama’s picture

rerooting’s picture

You may be able to learn more from Panopoly Magic, which extends views content pane settings in this and many other ways!

bmx269’s picture

I have tried this patch, the UI elements are all there, but the View Mode does not get changed. Are there any updates for this?

bmx269’s picture

Is there any more news on this?

dysrama’s picture

I'm sorry, for some reason I hadn't seen your post from back in september.
My guess is that the dev code changed in some way to make the patch not work as intended.
Unfortunately I'm out of the country at the moment with no way of checking what version of ctools I'm running the patch on.
I'll check it out once I get back and see if I can't get it to work on the newest dev version.
If it works at my end, an export of the view you're trying to make this work on might be helpful for debug.

dysrama’s picture

The patch is running on my site with ctools version 1.3. Are you applying it to a dev version? Version 1.3 is from april, so a lot could have changed since then.

bmx269’s picture

The Views and Panels interface shows the option for View Mode, but the change to the View Mode does not change the output on the Panels pane. I am using Ctools dev from yesterday with the patch now.

jhedstrom’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
5.58 KB
5.7 KB
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es). View

This patch changes a few things from #1.

  • The entity row plugin isn't provided by views, but rather by the entity api module, thus the patch didn't work with the node row plugin provided by views.
  • This adds the additional options provided by the node row plugin to display links and comments. This shouldn't impact views using the entity row plugin, as it will just ignore those options.
  • Minor whitespace error fixes.
jhedstrom’s picture

FileSize
1.41 KB
6.18 KB
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es). View

This patch adds better defaults by pulling them from the view as pager options are done.

dysrama’s picture

FileSize
6.75 KB
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es). View

I've added a fallback option for listing node viewmodes in the pane configuration.
This is useful when your base table is ie. search api server and doesn't have view modes.

dysrama’s picture

Title: View mode as exposed content pane setting » View mode/style plugin as exposed content pane setting
FileSize
7.82 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch view-mode-pane-setting-2032293-11.patch. Unable to apply patch. See the log in the details link for more information. View

I've attached yet another patch, this one includes exposing of style plugins as well.

Status: Needs review » Needs work

The last submitted patch, 11: view-mode-pane-setting-2032293-11.patch, failed testing.

mrjmd’s picture

Status: Needs work » Needs review
FileSize
8.91 KB
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es). View

Re-rolle attached.

bmx269’s picture

#14 works perfectly. Thanks.

ndf’s picture

#14 works well. Code looks clean too.

The logic of which list of view modes to show (what entity is listed) in the panel_pane is a bit flawed, as it depends on the base_table. Any suggestions of how to better do this would be greatly appreciated.

Regarding #10, search api is a good example for the fallback. But it won't work for users, terms, etc...
It is possible to get this information from the SearchApi query, although it doesn't feel good to add those logic to ctools. But ok, here it is.

in views_panes.inc

// This is assuming too much. A better way to do this?
// Ideally just use the options_form method from the plugin.
$base_table = $view->display_handler->view->base_table;
// Check if is a valid entity type.
$entity_types = entity_get_info();

if (isset($entity_types[$base_table])) {
  $view_modes = array_keys($entity_types[$base_table]['view modes']);
  $options = array_combine($view_modes, $view_modes);
}
elseif (drupal_substr($view->base_table, 0, 10) == 'search_api') {
  // We must prepare the query to get more info from search_api views.
  $view->set_display('default');
  $view->build();
  // Get entityType from SearchApi query.
  $base_table = $view->query->getIndex()->getEntityType();
  $view_modes = array_keys($entity_types[$base_table]['view modes']);
  $options = array_combine($view_modes, $view_modes);
}
else {
  // Default to node
  $entity_info = entity_get_info('node');
  $view_modes = array_keys($entity_info['view modes']);
  $options = array_combine($view_modes, $view_modes);
}
ndf’s picture

FileSize
9.2 KB
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es). View

Attached patch adds SearchAPI support to #14.
Also removed some duplicated code in #14. The logic to load available view-modes is now:

// This is assuming too much. A better way to do this?
// Ideally just use the options_form method from the plugin.
$base_table = $view->display_handler->view->base_table;
// Check if is a valid entity type.
$entity_types = entity_get_info();

// Create the options list with available view-modes.
$options = array();

// Real entities where Views is querying the entity table.
if (isset($entity_types[$base_table])) {
  foreach ($entity_types[$base_table]['view modes'] as $mode => $settings) {
    $options[$mode] = $settings['label'];
  }
}
// SearchAPI index where Views is querying the search_api_index table.
elseif (drupal_substr($view->base_table, 0, 16) == 'search_api_index') {
  // We must prepare the query to get more info from search_api views.
  $view->set_display('default');
  $view->build();
  // Get entityType from SearchApi query.
  $base_table = $view->query->getIndex()->getEntityType();
  foreach ($entity_types[$base_table]['view modes'] as $mode => $settings) {
    $options[$mode] = $settings['label'];
  }
}
// Fallback mode.
else {
  // Default to node
  $base_table = 'node';
  foreach ($entity_types[$base_table]['view modes'] as $mode => $settings) {
    $options[$mode] = $settings['label'];
  }
}

joelpittet’s picture

@nielsdefeyter that seems to be a bit of scope creep on the original issue. Additionally can you add an interdiff so we can see what you changed from #14? @see https://www.drupal.org/documentation/git/interdiff

ndf’s picture

FileSize
1.93 KB

This is the interdiff between #14 and #17.
@joelpittet: the addition is searchapi handling. I am not sure it belongs in ctools (probably not), but I put it here because it is a clear usecase for this ticket.
The other change is code cleanup (removed redundant code from patch #14).

joelpittet’s picture

Status: Needs review » Needs work

Thank you @nielsdefeyter for the interdiff, it's way easier to review your changes with that!

I agree, likely shouldn't be putting search_api specific code into ctools, but if we can help make this patch friendly for search_api to alter, we should! Any ideas? Alter hook?

Bunch o' questions:

  1. +++ b/views_content/plugins/content_types/views_panes.inc
    @@ -472,28 +472,31 @@
    -      $options = array_combine($view_modes, $view_modes);
    

    Does array_combine not do what it needs to?

  2. +++ b/views_content/plugins/content_types/views_panes.inc
    @@ -472,28 +472,31 @@
           foreach ($entity_types[$base_table]['view modes'] as $mode => $settings) {
             $options[$mode] = $settings['label'];
           }
    ...
    +      foreach ($entity_types[$base_table]['view modes'] as $mode => $settings) {
    +        $options[$mode] = $settings['label'];
    +      }
    

    Can you refactor so that this loop definition is duplicated?

  3. +++ b/views_content/plugins/content_types/views_panes.inc
    @@ -472,28 +472,31 @@
    +    elseif (drupal_substr($view->base_table, 0, 16) == 'search_api_index') {
    

    What is the likely hood of a base_table bing a unicode character? better to put this as regular php substr to avoid the overhead, or (strpos('search_api_index', $view->base_table) === 0) is also good.

  4. +++ b/views_content/plugins/content_types/views_panes.inc
    @@ -472,28 +472,31 @@
    -      $entity_info = entity_get_info('node');
    -      foreach ($entity_info['view modes'] as $mode => $settings) {
    +      $base_table = 'node';
    +      foreach ($entity_types[$base_table]['view modes'] as $mode => $settings) {
    

    If this base table is to be hardcoded string, why not just put the string in the key instead of creating a variable for it?

gmercer’s picture

Patch #17 does not apply if you also need the patch from [1910608#29]

The patch attached will resolve this, however the test will fail.

joelstein’s picture

Patch #17 works for me!

joelstein’s picture

Here's a re-roll of #17, not considering the comments in #21... just re-rolling so it applies cleanly to DEV.

rabbitlair’s picture

Patch on #24 works for me!

joelstein’s picture

In case you've applied the patch at #1910608-33: Ajax + Allow settings: Allowed settings lost on ajax (exposed forms/pager), here's a patch that will apply cleanly.

Note, the patch in #24 still applies cleanly against DEV (without the other patch).

kopeboy’s picture

I am just a site builder... but this would be SUPER useful!

Anyway, I tried the dev ctools with the patch in #24, but the site was blank afterwards (no errors). Maybe some conflict with other modules which depend on Ctools, will try with a clean install.

UPDATE: on a clean site with Drupal 7.43.1 I got no problems installing latest dev + patch.

BUT the patch #24 still doesn't work (for me?!)
The options work in the View and show up in Panels, but don't get saved when changed from the Panels interface.
Whatever setting I put in Panels, I always get defaults defined in the View.
This is reflected in the Panels' pane, and it's true for any (even pre-existing) allowed setting.

Example: edit your panel page, click on the view content pane settings, change some allowed setting (determined in the View) like "Num items" from 10 to 2, click "Finish", Update and save the Panel, open the pane settings once more: "Num items" is 10.

Using "Rendered entity" or "Content" in the View > Format > Show is not relevant.

UPDATE 2
I found that the problem is in Panelizer. After panelizing the content type, some weird things happen.

Look at the screenshots:

  1. normal teaser view mode, working correctly (I can change the view mode of the View's contents from the Panel)
  2. panelized entity teaser view mode: seems to work, but the single fields are added as panes under the View pane (is this normal?!)
  3. editing the pane settings won't work, saving the panel will make the pane disappear from it.

normal view mode

after panelizing the entity

after saving the panel.png

kopeboy’s picture

kopeboy’s picture

Ok so, the patch works even if it's not compatible with Panelizer.

Would it be possible to also choose some view modes to exclude from the list in the pane settings form?

The use case is clear: if you want this patch you want some user (not admin) to select the view mode for the entities he is listing, BUT you will never want him to choose from view modes that are not meant to be displayed or for him.
Ideally we could have a checkbox in the View display > Allow settings that shows up when we check the set_view_mode option, allowing us to check which view modes we want to make available. So we can hide for example Colorbox, RSS, Token, Search etc. etc. from the selection in the Pane settings.

They are doing this in Panopoly (Magic submodule) or in Fieldable Panel Panes.
#2170695: Make view modes removed from pane configuration a setting
#2498623: Only show view modes with custom settings on FPP edit form

In Panopoly code look for "PANOPOLY_MAGIC_HIDDEN_VIEW_MODE_OPTIONS"

UPDATE

Ok, I did it like this:
in views_panes.inc I added on top

/**
 * These are the view modes I want to hide from pane configuration forms.
 */
define('HIDDEN_VIEW_MODE_OPTIONS', "full\nteaser\nrss\ncolorbox\ntoken\npreview\nworkflow_tab\ndiff_standard\nrevision");

and then inside if ($allow['set_view_mode']) {
I added
$hidden_view_modes = array_filter(array_map('trim', explode("\n", variable_get('hidden_view_mode_options', HIDDEN_VIEW_MODE_OPTIONS))));

and modified all the

      foreach ($entity_types[$base_table]['view modes'] as $mode => $settings) {
        $options[$mode] = $settings['label'];
      }

to this

      foreach ($entity_types[$base_table]['view modes'] as $mode => $settings) {
        if (!in_array($mode, $hidden_view_modes)) {
          $options[$mode] = $settings['label'];
        }
      }

What I miss now (since I am not a developer, yet?) is adding a form inside Views content pane display "Allowed settings" to make the list of excluded view modes configurable..
Can anybody help on this?