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
#20 interdiff-2032293-14-17.txt1.93 KBndf
#17 ctools-view-mode-pane-settings-2032293-17.patch9.2 KBndf
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es).
[ View ]
#14 view-mode-pane-setting-2032293-14.patch8.91 KBmrjmd
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 ]
#10 view-mode-pane-setting-2032293-10.patch6.75 KBdysrama
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es).
[ View ]
#9 view-mode-pane-setting-2032293-09.patch6.18 KBjhedstrom
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es).
[ View ]
#9 interdiff.txt1.41 KBjhedstrom
#1 view_mode_as_exposed_content_pane_setting-2032293-1.patch5.11 KBdysrama

Comments

dysrama’s picture

The patch

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
StatusFileSize
new5.58 KB
new5.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

StatusFileSize
new1.41 KB
new6.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

StatusFileSize
new6.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
StatusFileSize
new7.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
StatusFileSize
new8.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

StatusFileSize
new9.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

StatusFileSize
new1.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?