0

I have 3 text Fields that I use for URLs, attached to a Panel Page. I'd like to create a menu with these URLs.

I've sort of done this with a View, by setting the Contextual Filters to use the node ID, then adding the Fields output and setting the Format to a pre-made Menu format I've used in the past. I've used Rewrite Results to make the <a> tag.

However, the issue is that all my fields are treated as a single menu item, not 3 individual ones.

Instead of the desired result being something like:

<span class="menu-item"><a href="url">Link Name</a></span>
<span class="menu-item"><a href="url">Link Name</a></span>
<span class="menu-item"><a href="url">Link Name</a></span>

The result I get is:

<span class="menu-item"><a href="url">Link Name</a><a href="url">Link Name</a><a href="url">Link Name</a></span>

Would anyone know how I can iterate through and Format each Field within a View?

1 Answer 1

0

OK, so the only way I could do this was to adjust the Features .module file to register both a theme and a CTools plugin:

/**
 * Implements hook_theme().
 */
function mycomponent_theme() {
  return array(
    'mycomponent_links_menu' => array(
      'path' => drupal_get_path('module', 'mycomponent') . '/templates',
      'template' => 'mycomponent_links_menu',
      'variables' => array(
        'config' => array(),
      ),
    ),
  );
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function mycomponent_ctools_plugin_directory($owner, $plugin_type) {
  if ($owner === 'ctools' && $plugin_type === 'content_types') {
    return 'plugins/content_types';
  }
}

Then, I needed to create an .inc file (mycomponent_links_menu.inc) to enable me to add the menu as a component. On that file I created the plugin, , eg:

$plugin = array(
  'single' => TRUE,
  'title' => t('Link Menu'),
  'description' => t('Displays links.'),
  'category' => t('Mycomponent'),
  'edit form' => 'mycomponent_link_menu_panel_settings',
  'render callback' => 'mycomponent_link_menu_panel_render',
  'defaults' => array(),
  'all contexts' => TRUE,
);

and rendered the panel to a block, grabbing each Field from the node's array:

function mycomponent_link_menu_panel_render($subtype, $conf, $args, $contexts) {
  $node_context = reset($contexts);
  $block = new stdClass();

  $field_text1_items = field_get_items('node', $node_context->data, 'field_text1');
  $field_text2_items = field_get_items('node', $node_context->data, 'field_text2');
  $field_text3_items = field_get_items('node', $node_context->data, 'field_text3')


  $panel_content = array(
    '#theme' => 'mycomponent_links_menu',
    '#config' => array(
      'link_to_something' => l(t('Something'), $field_text1_items [0]['safe_value']),
      'link_to_anotherthing => l(t('Anotherthing'), $field_text2_items [0]['safe_value']),
      'link_to_somthingelse' => l(t('Invision'), $field_text3_items [0]['safe_value']),
    ),
  );

  $block->content = render($panel_content);
  return $block;
}

Then I created a .tpl file that actually wrapped the rendered links in HTML, eg:

  <li class="menu__item">
    <?php print $config['link_to_somthingelse']; ?>
  </li>

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.