You are here

function command_buttons_render_buttons in Command Buttons 7

Render a list of command buttons into an item list.

@todo: this really should be a theme function and / or display formatter. bootstrap is very particular about allowed markup, but being able to override it is still nice.

1 call to command_buttons_render_buttons()
command_buttons_entity_view in ./command_buttons.module
Implements hook_entity_view().

File

./command_buttons.module, line 643

Code

function command_buttons_render_buttons($commands, $classes, $source = array()) {
  if (empty($source)) {

    // create source for fieldable panels pane
    foreach ($commands as $command) {
      $source[] = array(
        'value' => $command->name,
        'provider_type' => '',
      );
    }
  }

  // Array to hold our duplicate sections if there are any.
  $duplicates = array();

  // Iterate over the command links to determine if we have duplicate sections.
  foreach ($source as $command_link) {
    if (isset($duplicates[$command_link['value']])) {
      $duplicates[$command_link['value']] = TRUE;
    }
    else {
      $duplicates[$command_link['value']] = FALSE;
    }
  }
  foreach ($commands as $command) {

    // Get the individual commands attached to the button entity.
    $links = field_get_items('command_button', $command, 'field_command_link');

    // Add the correct css class to each link, do token replacement, and render the link.
    if (!empty($links)) {
      foreach ($links as $link) {
        $path = filter_xss(token_replace($link['url']));
        if (!url_is_external($path)) {
          $item = menu_get_item($path);
          if (!$item || empty($item['access'])) {
            continue;
          }
        }
        $link['attributes']['class'] = !empty($classes['item_class']) ? $classes['item_class'] : array();
        $link['attributes']['class'] += array(
          ' command-' . str_replace('-', '_', $command->name),
        );
        foreach ($source as $command_link) {
          $options = array(
            'attributes' => $link['attributes'],
          );

          // Make sure we have a command link and that the value matches that of
          // the command we are currently on.
          if (!empty($command_link) && $command_link['value'] === $command->name) {
            if ($command_link['provider_type'] == 'oa_section') {
              $options['query'] = array(
                'oa_section_ref' => $command_link['id'],
              );
            }
            else {
              if ($command_link['provider_type'] == 'oa_space') {
                $options['query'] = array(
                  'og_group_ref' => $command_link['id'],
                );
              }
            }
            if (drupal_valid_path($path)) {

              // If we don't have duplicate sections just display the default title.
              if (!$duplicates[$command_link['value']]) {
                $title = token_replace($link['title']);
              }
              else {
                $title = db_select('node', 'n')
                  ->fields('n', array(
                  'title',
                ))
                  ->condition('n.nid', $command_link['id'])
                  ->execute()
                  ->fetchField();
                $title = token_replace($link['title'] . ' ' . t('in') . ' ' . $title);
              }
              $icon = field_get_items('command_button', $command, 'field_link_icon');
              if (!empty($icon[0]['value'])) {
                $title = '<i class="' . $icon[0]['value'] . '"></i> ' . $title;
                $options['html'] = TRUE;
              }

              // handle query argument in the command button path
              $options = array_merge_recursive($options, drupal_parse_url($path));
              $path = $options['path'];

              // can't leave 'path' in url $options or the outbound url alter
              // in oa_domains gets messed up
              unset($options['path']);
              $items[] = '<li>' . l($title, $path, $options) . '</li>';
            }
          }
        }
      }
    }
  }

  // Override default field_commands handling with our new item list.
  if (!empty($items)) {
    $vars['items'] = $items;
    $vars['icon'] = !empty($classes['icon_class']) ? check_plain($classes['icon_class']) : '';
    $vars['show_caret'] = isset($classes['show_caret']) ? $classes['show_caret'] : TRUE;
    $vars['use_dropdowns'] = $classes['use_dropdowns'];
    $vars['wrapper_class'] = implode(' ', $classes['wrapper_class']);
    $vars['label'] = check_plain($classes['dropdown_label']);
    $vars['btn_title'] = isset($classes['btn_title']) ? check_plain($classes['btn_title']) : '';
    $vars['direction'] = isset($classes['direction']) ? $classes['direction'] : '';
    return theme('command_buttons', $vars);
  }
  return '';
}