This might be a dupe of #1300724: I need a way to let users edit the delta of field-collection items without the use of the edit page (or others, I can't tell)

Currently there are two widgets for Field Collection - either you can edit from the parent's entity form (embedded/subform mode) or hide all together and rely on the inline links to manage field collection items.

If you have a complex field collection bundle then the latter option is probably advisable - having several instances of CKEditor on the page is one reason why you might not want to display the field collection widget on your entity edit forms.

The problem is that if you hide the widget entirely then you can't re-order the field collection items. I get the feeling that it's still up for debate where that re-ordering should occur - either on a separate page or on the entity edit form. Actually I don't really care but it would be nice to have it somewhere!

If I was to implement this, then this is what I would do:

Define a third widget for field collection - "re-order only"
Define a new display mode - not sure what it would be called, something like "title only", or maybe just use teaser?
Then, in the widget I would just return the rendered field collection item using that display mode. Drupal's internal field handling would then allow you to re-order or remove items. Not sure if we could turn off the 'add' button?

Any thoughts are welcome

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

erik.erskine’s picture

This module may help - we have addressed a similar use case: https://drupal.org/sandbox/ingaro/2019853

Angry Dan’s picture

Thanks erik_erskine! If the problem comes up again I'll be sure to take a look.

Not sure where that leaves this issue? Anything from the maintainer about the feature request?

erik.erskine’s picture

Status: Active » Needs review
FileSize
3.4 KB

I've turned that sandbox module into a patch. It adds a new field_collection_sorter widget.

guy_schneerson’s picture

Patch #3 works great for me.
It allows users to use node view to add/edit/delete field collections and the node edit to sort them.

guy_schneerson’s picture

Only issue is the titles are numbered items and that makes it difficult to sort, not sure if this is an issue for this patch.

erik.erskine’s picture

Unlike nodes, field collection items don't have any obvious property to use
as a title, so the label shown in the widget will simply be something like
'Collection 1', Collection 2 etc. This comes from the entity's label()
method and is the same thing that's shown when the 'links to field collection
items' formatter is used.

You can change this for your own field collections where you know what the
fields are and have a means of picking a suitable title. You can implement
the following hook to load the field collection entity and replace the default
label with something more useful:

  /**
   * Implements hook_field_widget_field_collection_sorter_form_alter().
   */
  function mymodule_field_widget_field_collection_sorter_form_alter(&$element, &$form_state, $context) {
    if ($context['field']['field_name'] == 'my_custom_field') {

      // special case for the 'my_custom_field' field:
      // we know that items referenced by this field will have a field called
      // 'my_collection_item_title' which we can use to identify them

      foreach ($context['items'] as $delta => $item) {
        $item_id = $context['items'][$delta]['value'];
        $item = field_collection_item_load($item_id);
        $item_wrapper = entity_metadata_wrapper('field_collection_item', $item);
        $title = $item_wrapper->my_collection_item_title->value();

        $element[$delta]['label'] = array(
          '#markup' => check_plain(),
        );
      }
    }
  }

This problem with the lack of a node title crops up elsewhere see https://drupal.org/node/1335204 & https://drupal.org/node/1674298. We could do something clever to try to guess something meaningful, but I'm not sure it's within the scope of this patch though.

guy_schneerson’s picture

Will give that a go, and thanks for the great patch.

james.williams’s picture

Just in case the field collection item has actually been deleted without updating the host entity, the attached patch avoids a fatal error, which is a bit over-kill for such a situation (which is possible when working with a variety of other modules, such as workbench).

Chris Matthews’s picture

The patch in #8 is 4 years old, but still applies cleanly to 7.x-1.x-dev and is a great improvement to this module. Can someone else review/test before RTBC?

  • ram4nd committed f9edc4f on 7.x-1.x authored by james.williams
    Issue #1995358 by erik.erskine, james.williams: Re-order only widget/...
ram4nd’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.