【问题标题】:Custom Field cannot be sorted or filtered in Views for Drupal 7无法在 Drupal 7 的视图中对自定义字段进行排序或过滤
【发布时间】:2017-02-05 18:03:11
【问题描述】:

我使用模块为视图创建了一个自定义字段。为了在此处更好地进行可视化,我对其进行了简化:自定义字段仅生成一个介于 1 和 10 之间的随机数。

我想对这个随机数进行“排序”。但是,在视图中使用此设置时,我收到以下错误:

SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列 'my_custom_field'

我很难在我的代码中找到错误。

感谢您在我的模块代码中提供的任何帮助!!

这是我的文件:

my_custom_module.info

name = My Custom Module
description = Implement random number in views.
core = 7.x
files[] = includes/views_handler_my_custom_field.inc

my_custom_module.module

<?php
/**
 * Implements hook_views_api().
 */
function my_custom_module_views_api() {
  return array(
    'api' => 3,
  );
}

my_custom_module.views.inc

<?php
/**
 * Implements hook_views_data().
 */
function my_custom_module_views_data() {
  $data['my_custom_module']['table']['group'] = t('My custom module');
  $data['my_custom_module']['table']['join'] = array(
    // Exist in all views.
    '#global' => array(),
  );

  $data['my_custom_module']['my_custom_field'] = array(
    'title' => t('My custom field'),
    'help' => t('My custom field displays a random number.'),
    'field' => array(
      'handler' => 'views_handler_my_custom_field',
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
  );

  return $data;
}

views_handler_my_custom_field.inc

<?php
/**
 * @file
 * Custom views handler definition.
 */

/**
 * Custom handler class.
 *
 * @ingroup views_field_handlers
 */
class views_handler_my_custom_field extends views_handler_field {
  /**
   * {@inheritdoc}
   *
   * Perform any database or cache data retrieval here. In this example there is
   * none.
   */
  function query() {

  }

  /**
   * {@inheritdoc}
   *
   * Modify any end user views settings here. Debug $options to view the field
   * settings you can change.
   */
  function option_definition() {
    $options = parent::option_definition();
    return $options;
  }

  /**
   * {@inheritdoc}
   *
   * Make changes to the field settings form seen by the end user when adding
   * your field.
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
  }

  /**
   * Render the random field.
   */
  public function render($values) {
    $random = rand(1, 10);
    return $random;
  }
}

【问题讨论】:

    标签: drupal drupal-7 drupal-views


    【解决方案1】:

    简短回答:如果没有适当的数据库字段,您无法对视图进行排序。

    稍微长一点的回答:hook_views_data() 的主要目的是向 Views 描述一个数据库表。您确实使用'#global' =&gt; array() 显示了一个在数据库中并不真正存在的字段,但由于该特定字段不是SQL 查询的一部分,您根本无法对其进行排序。甚至你在views_handler_my_custom_field-&gt;render()方法中得到的随机数的值都是在视图生成并执行SQL查询后生成的,此时所有的排序都已经发生了。

    【讨论】:

    • 感谢您抽出宝贵时间回答!我现在更好地理解了这个逻辑。不幸的是,我必须改变我的“大图”的方法,因为这种方法行不通。我创建的实际自定义字段根据登录用户是动态的(与上下文过滤器一样)。问题是我需要多个上下文过滤器,它们之间使用 OR 函数并公开供用户使用。是时候找到 B 计划了……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多