0

I am currently working with the Field Collection module, but I have stumbled into a small problem.

I am trying to alter the links variable (currently there is Edit & Delete links) on a field_collection, as I want to add a link to it. I believe I have tried everything, such as hook_entity_view_alter(), or a preprocess function, but nothing seems to work.

Is there a way to solve this, without hacking core?

2
  • Why doesn't it work? Try posting your code as part of your question. Commented May 7, 2012 at 6:27
  • I have simply tried to find content->links but it's no where to be found. Doubt the code will help that much, its just a bunch of dpm, in a bunch of hooks. Commented May 7, 2012 at 8:11

1 Answer 1

2

In a custom module you can use hook_field_attach_view_alter(). In this example I'm using the distribution profile eRecruiter so there is a bundle called resume with several field collections and in an entity type profile2. Every time there is a field attached to a field collection this hook is fired recursively but the links are not contained there.

/**
* Implements hook_field_attach_view_alter()
* 
* @param mixed $output
* @param mixed $context
*/
function helper_field_attach_view_alter(&$output, $context) {
  if ($output['#entity_type'] == 'profile2' && $output['#bundle'] == 'resume') {
    $f = 'do stuff here';
  }
}

So you understand how the links work here is some of the code used in field_collection.module.

The links are created in a render array. Don't worry about 'links__field_collection_view' too much because it doesn't do anything. When the #theme function is called more familiarly as theme('link__field_collection_view', $variables) it looks first for TEMPLATE_link__field_collection_view(&$variables) or link--field-collection-view.tpl.php however if it doesn't find it, it will use as it does in this case theme_links();

  $links = array(
    '#theme' => 'links__field_collection_view',
  );
  $links['#attributes']['class'][] = 'field-collection-view-links';
  foreach (array('edit', 'delete') as $op) {
    if ($settings[$op] && field_collection_item_access($op == 'edit' ? 'update' : $op, $field_collection)) {
      $links['#links'][$op] = array(
        'title' => $settings[$op],
        'href' => $field_collection->path() . '/' . $op,
        'query' => drupal_get_destination(),
      );
    }
  }

hook_admin_paths() is used to make the links rendered in the dialog window.

function field_collection_admin_paths() {
  if (variable_get('node_admin_theme')) {
    return array(
      'field-collection/*/*/edit' => TRUE,
      'field-collection/*/*/delete' => TRUE,
      'field-collection/*/add/*/*' => TRUE,
    );
  }
}

hook_menu() is set up for the link callback in this case the edit link

  $items[$path . '/%field_collection_item/edit'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array('field_collection_item_form', $count),
    'access callback' => 'field_collection_item_access',
    'access arguments' => array('update', $count),
    'title' => 'Edit',
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
    'file' => 'field_collection.pages.inc',
  );
2
  • I found you answer very explaining, but i'm still don't understand how to solve this? Should i be able to add links with <module>_field_attach_view_alter ? I guess i can't call the field_collection_field_formatter_links. Commented May 7, 2012 at 8:16
  • What is the entity you are attaching the field collections to? And what is the bundle? There is an entity called 'node' and bundles called 'page'. If there is a field collection attached to the node type article, then if ($output['#entity_type'] == 'node' && $output['#bundle'] == 'page') would test for the content to give you access to the links. Perhaps, hook_page_alter would give you access too but the links might be deeply hidden. What link to you want to add?
    – Adam S
    Commented May 7, 2012 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.