I'm really enjoying Field Collection so far, but I've run into the issue that I only want the field collection to be displayed as part of the node its embedded on. Due to my use case, I don't want the field collection displayed separately at /field-collection/field-name/[id].

The reason is that this particular field collection is used on a node with access control (using the Content Access module) to keep things private. So I don't want the field collection visible to users who don't have access to that particular node. Is there any way to keep the entity hidden at /field-collection/field-name/[id]? This would be similar to how nodes can be "unpublished" except that it would work on the field collection entity itself (and obviously the field collection would still need to be visible when displayed on an embedded node).

Does this seem like a doable feature request? Or is there some way currently to accomplish this?

Thanks,
Ben

Comments

tim.plunkett’s picture

Category: feature » support
Status: Active » Fixed

You could use hook_menu_alter() to set the access callback to FALSE for all of those callbacks, something like this:

function YOURMODULE_menu_alter(&$items) {
  foreach (field_info_fields() as $field) {
    if ($field['type'] == 'field_collection') {
      $path = field_collection_field_get_path($field);
      $items[$path . '/%field_collection_item']['access callback'] = FALSE;
      $items[$path . '/%field_collection_item/view']['access callback'] = FALSE;
    }
  }
}
BenK’s picture

Thanks! Very helpful.

Just one quick follow-up question: If I wanted to allow just those with the 'administer nodes' permission to be able to access /field-collection/field-name/[id] (and deny everyone else), how would I do that?

Thanks again,
Ben

tim.plunkett’s picture

That should just be like this:

function YOURMODULE_menu_alter(&$items) {
  foreach (field_info_fields() as $field) {
    if ($field['type'] == 'field_collection') {
      $path = field_collection_field_get_path($field);
      $items[$path . '/%field_collection_item']['access callback'] = 'user_access';
      $items[$path . '/%field_collection_item']['access arguments'] = array('administer nodes');
      $items[$path . '/%field_collection_item/view']['access callback'] = 'user_access';
      $items[$path . '/%field_collection_item/view']['access arguments'] = array('administer nodes');
    }
  }
}

See hook_menu() for more details.

BenK’s picture

Thanks so much! Will try it out now...

--Ben

Status: Fixed » Closed (fixed)

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

BenK’s picture

Status: Closed (fixed) » Active

@tim.plunkett:

Hope all is well. I've tried out your code suggestions and got some mixed results.... the code in comment #1 (deny access to all) worked fine, but when I try the code in comment #3 (grant access only to those with adminster nodes permission), I get the following notices:

Notice: Undefined index: access in _menu_translate() (line 778 of /Users/benkaplan/git/commerce_quickstart12j/includes/menu.inc).
Notice: Undefined index: access in menu_get_item() (line 464 of /Users/benkaplan/git/commerce_quickstart12j/includes/menu.inc).
Notice: Undefined index: access in menu_local_tasks() (line 1964 of /Users/benkaplan/git/commerce_quickstart12j/includes/menu.inc).
Notice: Undefined index: access in _menu_link_translate() (line 913 of /Users/benkaplan/git/commerce_quickstart12j/includes/menu.inc).
Notice: Undefined index: access in menu_set_active_trail() (line 2316 of /Users/benkaplan/git/commerce_quickstart12j/includes/menu.inc).

Additionally, access isn't actually granted to those with the administer nodes permission and the "View" tab isn't visible either for those with that permission.

Any ideas on how to fix?

Thanks again! :-)

--Ben

tim.plunkett’s picture

@BenK, those should have been "user_access" not "user access", I apologize. I've updated the code above.

13rac1’s picture

Status: Active » Fixed

Seems fixed.

Status: Fixed » Closed (fixed)

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