in Web & Tech

Output a Field Collection’s Item ID and build a Jump Navigation

It just took me a while to figure this out, as I needed the Field Collection’s ID to build anchors for a (views built) jump navigation. Here’s how I’m wrapping each Field Collection item with a section tag that has a CSS ID with each Field Collection item’s unique identifier.

<section id="section-<?php print $elements['#entity']->item_id; ?>">
  <?php print render($content); ?>
</section>

For future reference here’s also the (simple) View that builds the jump navigation from Field Collection titles. It takes the node ID as an contextual filter from the URL. So you just get a jump navigation for the Field Collection items from the host node that’s currently being displayed. Note that this View expects a field named field_section_title to use for the menu item.

$view = new view();
$view->name = 'jumpnav';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'field_collection_item';
$view->human_name = 'Sprungnavigation';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Sprungnavigation';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['use_more_text'] = 'mehr';
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['exposed_form']['options']['submit_button'] = 'Anwenden';
$handler->display->display_options['exposed_form']['options']['reset_button_label'] = 'Zur├╝cksetzen';
$handler->display->display_options['exposed_form']['options']['exposed_sorts_label'] = 'Sortieren nach';
$handler->display->display_options['exposed_form']['options']['sort_asc_label'] = 'Aufsteigend';
$handler->display->display_options['exposed_form']['options']['sort_desc_label'] = 'Absteigend';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['style_plugin'] = 'list';
$handler->display->display_options['style_options']['default_row_class'] = FALSE;
$handler->display->display_options['style_options']['row_class_special'] = FALSE;
$handler->display->display_options['style_options']['wrapper_class'] = '';
$handler->display->display_options['row_plugin'] = 'fields';
/* Beziehung: Field collection item: Entity with the Abschnitt (field_section) */
$handler->display->display_options['relationships']['field_section_node']['id'] = 'field_section_node';
$handler->display->display_options['relationships']['field_section_node']['table'] = 'field_collection_item';
$handler->display->display_options['relationships']['field_section_node']['field'] = 'field_section_node';
/* Feld: Field collection item: Field collection item ID */
$handler->display->display_options['fields']['item_id']['id'] = 'item_id';
$handler->display->display_options['fields']['item_id']['table'] = 'field_collection_item';
$handler->display->display_options['fields']['item_id']['field'] = 'item_id';
$handler->display->display_options['fields']['item_id']['exclude'] = TRUE;
/* Feld: Field collection item: Titel */
$handler->display->display_options['fields']['field_section_title']['id'] = 'field_section_title';
$handler->display->display_options['fields']['field_section_title']['table'] = 'field_data_field_section_title';
$handler->display->display_options['fields']['field_section_title']['field'] = 'field_section_title';
$handler->display->display_options['fields']['field_section_title']['label'] = 'Title Raw';
$handler->display->display_options['fields']['field_section_title']['exclude'] = TRUE;
$handler->display->display_options['fields']['field_section_title']['alter']['strip_tags'] = TRUE;
$handler->display->display_options['fields']['field_section_title']['element_type'] = '0';
$handler->display->display_options['fields']['field_section_title']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_section_title']['element_wrapper_type'] = '0';
$handler->display->display_options['fields']['field_section_title']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['field_section_title']['field_api_classes'] = TRUE;
/* Feld: Field collection item: Titel */
$handler->display->display_options['fields']['field_section_title_1']['id'] = 'field_section_title_1';
$handler->display->display_options['fields']['field_section_title_1']['table'] = 'field_data_field_section_title';
$handler->display->display_options['fields']['field_section_title_1']['field'] = 'field_section_title';
$handler->display->display_options['fields']['field_section_title_1']['label'] = '';
$handler->display->display_options['fields']['field_section_title_1']['alter']['alter_text'] = TRUE;
$handler->display->display_options['fields']['field_section_title_1']['alter']['text'] = '<a href="#section-[item_id]" title="Zu [field_section_title] springen">[field_section_title]</a>';
$handler->display->display_options['fields']['field_section_title_1']['element_type'] = '0';
$handler->display->display_options['fields']['field_section_title_1']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_section_title_1']['element_wrapper_type'] = '0';
/* Kontextfilter: Inhalt: Beitrags-ID */
$handler->display->display_options['arguments']['nid']['id'] = 'nid';
$handler->display->display_options['arguments']['nid']['table'] = 'node';
$handler->display->display_options['arguments']['nid']['field'] = 'nid';
$handler->display->display_options['arguments']['nid']['relationship'] = 'field_section_node';
$handler->display->display_options['arguments']['nid']['default_action'] = 'default';
$handler->display->display_options['arguments']['nid']['exception']['title'] = 'Alle';
$handler->display->display_options['arguments']['nid']['default_argument_type'] = 'node';
$handler->display->display_options['arguments']['nid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['nid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['nid']['summary_options']['items_per_page'] = '25';

/* Display: Subnavigation */
$handler = $view->new_display('block', 'Subnavigation', 'block');
$translatables['jumpnav'] = array(
  t('Master'),
  t('Sprungnavigation'),
  t('mehr'),
  t('Anwenden'),
  t('Zur├╝cksetzen'),
  t('Sortieren nach'),
  t('Aufsteigend'),
  t('Absteigend'),
  t('field_section'),
  t('Field collection item ID'),
  t('.'),
  t(','),
  t('Title Raw'),
  t('<a href="#section-[item_id]" title="Zu [field_section_title] springen">[field_section_title]</a>'),
  t('Alle'),
  t('Subnavigation'),
);

Write a Comment

Comment