Problem/Motivation

View modes are part of the field instance export. That means that it is not possible to export separate view modes, without affecting the original feature. Separating the view modes per field instance would enable the possibility of providing different view modes on different sites, without affecting the 'base' feature.

Example 1
Let's say I have a platform with multiple sites. For one of the sites I wish to have an extra view mode. Ideally, this could be exported into it's own Feature, leaving all other sites unaffected.

Example 2
Again, a platform with multiple sites. On one site I wish to adjust the view mode for the teaser. If the view modes where exported seperately, I could disable the 'Base'-teaser-view-mode and create a new one, just for that one particular site.

Proposed resolution

It would be nice if field instances could be exported per view mode. So instead of this:
[entity]-[bundle]-[field]
We would end up with this:
[entity]-[bundle]-[field]-[view-mode]

For existing features [entity]-[bundle]-[field] should keep working, but when exporting it should include the [view-mode].

(Note this is a very similar approach to #1064472: separate fields from field instances)

Remaining tasks

Right now I would like to know if this is feasible. I did some research, but I have not been able to discover if this was somehow already possible, perhaps I have missed something.

#1064472: separate fields from field instances
#1211008: Split field_bundle_settings out per bundle

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hefox’s picture

How about using features overrides?

dagomar’s picture

Thanks for your reply Hefox!

Yes, features overrides is indeed what I am using right now to solve this issue. However, that adds a dependency to the platform that I would rather omit, it feels like a hack basically. What I propose would be a cleaner solution, it would give the opportunity to pack certain features in a much more logical way. A content type may have an add-on feature that consists of a custom view, and a custom view mode used in that view. In my proposal that would cleanly export, with features override that wouldn't. An untested assumption I make here is that I cannot have 2 overrides for the same content type, so I assume it is not possible to have a second add-on feature for the content introducing yet another view mode. I will test to make sure that my assumption is right.

I understand that the issue is perhaps an edge case, but I think it would be of great benefit for us developers. It would make it so much easier to extend without breaking anything.

Oh I forgot, I realized this *may* be more an issue for core first, if so, perhaps this issue should be moved there.

ndf’s picture

Status: Active » Needs review
FileSize
10.22 KB

This patch separates 'field instances display' settings from 'field instances'.

Effectively this lets you export each field-instance for each view-mode separately. This solves Example 1 and 2.

before
field_instance: node-news-body

after
field_instance: node-news-body
field_instance_display: node-news-body-full
field_instance_display: node-news-body-teaser

Note that this separation is superficial, Drupal Core does not have a distinction between field-instances-displays and field-instances. Field-instances-displays are saved within field-instances.

If you apply this patch you have to recreate all features that have field-instances.

Status: Needs review » Needs work
ndf’s picture

Needs tests. Test for field_instance (that is altered) is failing now.

ndf’s picture

Status: Needs work » Needs review
FileSize
15.61 KB
4.93 KB

Added tests.

ndf’s picture

Snippet that adds field_instance_displays when adding a display-suite layout to a feature.

/**
 * Implements hook_features_pipe_COMPONENT_alter().
 *
 * Add field_instance_displays when adding a display-suite layout to a feature.
 * Component: 'ds_layout_settings'
 */
function YOUR_MODULE_features_pipe_ds_layout_settings_alter(&$pipe, $data, $export) {

  // Automatically add ds_field_settings and field_instance_displays when adding
  // a ds_layout_setting to Features.
  foreach ($data as $ds_layout) {
    list($entity_type, $bundle, $view_mode) = explode('|', $ds_layout);


    // Add ds_field_settings for this layout/viewmode.
    $pipe['ds_field_settings'][] = $entity_type . '|' . $bundle . '|' . $view_mode;


    // Add field_instance_displays for this layout/viewmode.
    // This depends on feature-patch https://www.drupal.org/node/2029577#comment-10090088
    // "Separate field instances from view modes".
    if (function_exists('field_instance_display_features_export')) {

      // Extract fields from layout.
      $ds_layout = ds_get_layout($entity_type, $bundle, $view_mode);

      if ($ds_layout && isset($ds_layout['settings']['fields'])) {
        $fields = $ds_layout['settings']['fields'];
      }

      // Add field_instance_displays for this layout/viewmode.
      if (isset($fields) && !empty($fields)) {
        foreach ($fields as $field => $settings) {
          // Check if normal field.
          if (array_key_exists($field, field_info_field_map())) {
            $pipe['field_instance_display'][] = $entity_type . '-' . $bundle . '-' . $field . '-' . $view_mode;
          }
        }
      }
    }
  }
}

ndf’s picture

Think I will create a new module that combines patch + above snippet.

It should replace includes/features.field.inc with the one in the patch.
Quite sure its documented in some ticket how to do that, cannot find it now.

frodri’s picture

Patch on comment #6 breaks on Features version 2.11+. An updated patch is attached below.

frodri’s picture

Rerolling patch for Features version 2.14.