Drupal Views:如何设置列和行的标题?

时间:2014-09-05 09:48:01

标签: php drupal views rows

Drupal Views:如何设置列和行的标题?结果会是这样的吗?

coltitle1 coltitle2 coltitle3

rowtitle4结果结果

rowtitle3结果结果

rowtitle3结果结果

1 个答案:

答案 0 :(得分:0)

AFAIK,核心视图无法实现这一点。你需要一个不同的Views Style插件来处理这种情况。 Views Matrix模块提供了这样的插件,但仍在开发中。

以下代码也实现了这种解决方案,但尚未经过广泛测试:

/**
 * Implements hook_views_plugins().
 */
function MODULE_views_plugins() {
  $plugins = array(
    'style' => array(
      'drupalcamp_sessions_schedule' => array(
        'title' => t('Schedule'),
        'help' => t('Display content as a schedule table.'),
        'handler' => 'views_plugin_style_schedule',
        'uses options' => TRUE,
        'uses row plugin' => TRUE,
        'uses fields' => TRUE,
        'type' => 'normal',
        'theme' => 'table',
        'register theme' => FALSE,
      ),
    ),
  );
  return $plugins;
}

class views_plugin_style_schedule extends views_plugin_style_mapping {

  /**
   * {@inheritdoc}
   */
  protected function define_mapping() {
    return array(
      'row' => array(
        '#title' => t('Rows headers'),
        '#required' => TRUE,
      ),
      'column' => array(
        '#title' => t('Columns header)'),
        '#required' => TRUE,
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $column_field = isset($this->view->field[$this->options['mapping']['column']]) ? $this->options['mapping']['column'] : reset(array_keys($this->view->field));
    $row_field = isset($this->view->field[$this->options['mapping']['row']]) ? $this->options['mapping']['row'] : reset(array_keys($this->view->field));
    $variables = array(
      'view' => $this->view,
      'options' => $this->options,
      'header' => array(
       '_TIME_' => $this->view->field[$row_field]->options['label'] ? $this->view->field[$row_field]->options['label'] : t('Time')
      ),
      'rows' => array(),
      'mapping' => $this->define_mapping(),
    );
    // Collect table headers and render results into table cells.
    foreach ($this->view->result as $index => $item) {
      $column_key = $this->get_field_value($index, $column_field);
      if (!empty($column_key)) {
        if(!is_scalar($column_key)) {
          $column_key = md5(serialize($column_key));
        }
        if (!isset($variables['header'][$column_key])) {
          $variables['header'][$column_key] = $this->get_field($index, $column_field);
          if ($this->view->field[$column_field]->options['label']) {
            $variables['header'][$column_key] = $this->view->field[$column_field]->options['label'] . ': ' . $variables['header'][$column_key];
          }
        }
      }
      $row_key = $this->get_field_value($index, $row_field);
      if ($row_key != NULL && !is_scalar($row_key)) {
        $row_key = md5(serialize($row_key));
      }
      if (!isset($variables['rows'][$row_key])) {
        $variables['rows'][$row_key]['_TIME_'] = array(
          'data' => $this->get_field($index, $row_field),
          'header' => TRUE,
        );
      }
      $this->view->row_index = $index;
      // FIXME: Support multiple item in the same cell.
      $variables['rows'][$row_key][!empty($column_key) ? $column_key : '__DEFAULT__'] = array(
        'data' => $this->row_plugin->render($item),
        'colspan' => 1,
      );
    }
    // Process table rows to...
    foreach ($variables['rows'] as $index => $row) {
      // ...order cells in each line and fill-in any empty cells.
      $variables['rows'][$index] = array();
      $default = isset($row['__DEFAULT__']) ? $row['__DEFAULT__'] : array(
        'data' => '',
        'colspan' => 1,
      );
      foreach (array_keys($variables['header']) as $column) {
        $variables['rows'][$index][] = isset($row[$column]) ? $row[$column] : $default;
      }
      $last_cell_index = NULL;
      // ...merge adjacent cells containing an equal value.
      foreach ($variables['rows'][$index] as $cell_index => $cell) {
        if (!empty($last_cell_index) && ($variables['rows'][$index][$last_cell_index]['data'] == $cell['data'])) {
          $variables['rows'][$index][$last_cell_index]['colspan'] += 1;
          unset($variables['rows'][$index][$cell_index]);
        }
        else {
          $last_cell_index = $cell_index;
        }
      }
    }
    return theme($this->theme_functions(), $variables);
  }
}