我试图在Drupal 8中以编程方式创建视图。类似于Drupal 7中的crud日志模块。我无法在Internet上找到任何引用。如何以编程方式创建Drupal 8视图
1
A
回答
0
我成功地编程创建视图----我做了following-- 1.创建一个文件夹 - C:\ XAMPP \ htdocs中\ Drupal的实例\模块 2.创建一个YML信息文件--first_view.info和下面的代码添加到它---- 名称:首先查看 类型:模块 描述:我的第一个Drupal的8查看 包:自定义 核心:8.x的 3.创建一个安装文件--- first_view.install 并向其中添加下面的代码
<?php
/**
* @file
* Install, schema, and uninstall functions for the First View module.
*/
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\taxonomy\Entity\Term;
/**
* Implements hook_install().
*/
function first_view_install() {
}
/**
* Implements hook_uninstall().
*/
function first_view_uninstall() {
}
/**
* Implements hook_schema().
*/
function first_view_schema() {
$schema['first_view_table'] = [
// Example (partial) specification for table "node".
'description' => 'The base table for first_view.',
'fields' => [
'id' => [
'description' => 'The primary identifier for a node.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'name' => [
'description' => 'The name of Employee.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
],
'age' => [
'description' => 'The age of employee.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'is_active' => [
'description' => 'The activity of employee.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'timestamp' => [
'description' => 'The timestamp of employee.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'project_code' => [
'description' => 'The project code of employee.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => 0,
],
],
'unique keys' => [
'id' => ['id'],
],
// For documentation purposes only; foreign keys are not created in the
// database.
'primary key' => ['id'],
];
return $schema;
}
4.创建一个文件--- first_view.views.inc 并添加以下代码到它 -
<?php
/**
* Implements hook_views_data().
*/
function first_view_views_data() {
$data = [];
$data['first_view_table'] = [];
$data['first_view_table']['table'] = [];
$data['first_view_table']['table']['group'] = t('First View table');
$data['first_view_table']['table']['provider'] = 'first_view_module';
$data['first_view_table']['table']['base'] = [
'field' => 'id',
'title' => t('First View table'),
'help' => t('First View table contains example content and can be related to nodes.'),
'weight' => -10,
];
$data['first_view']['table']['join'] = [
'node_field_data' => [
'left_field' => 'id',
'field' => 'id',
'extra' => [
0 => [
'field' => 'published',
'value' => TRUE,
],
1 => [
'left_field' => 'age',
'value' => 1,
'numeric' => TRUE,
],
2 => [
'field' => 'published',
'left_field' => 'is_active',
'operator' => '!=',
],
],
],
];
$data['first_view_table']['table']['join']['node_field_data'] = [
'left_table' => 'foo',
'left_field' => 'id',
'field' => 'id',
'extra' => [
['left_field' => 'project_code', 'field' => 'project_code'],
['field' => 'age', 'value' => 0, 'numeric' => TRUE, 'operator' => '>'],
],
];
$data['first_view_table']['id'] = [
'title' => t('Example content'),
'help' => t('Relate example content to the node content'),
'relationship' => [
'base' => 'node_field_data',
'base field' => 'id',
'id' => 'standard',
'label' => t('Example node'),
],
];
$data['first_view_table']['name'] = [
'title' => t('Name'),
'help' => t('Just a Name field.'),
'field' => [
'id' => 'standard',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'string',
],
'argument' => [
'id' => 'string',
],
];
$data['first_view_table']['project_code'] = [
'title' => t('Project Code'),
'help' => t('Just a Project code field.'),
'field' => [
'id' => 'standard',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'string',
],
'argument' => [
'id' => 'string',
],
];
$data['first_view_table']['age'] = [
'title' => t('Age'),
'help' => t('Just a numeric field.'),
'field' => [
'id' => 'numeric',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'numeric',
],
'argument' => [
'id' => 'numeric',
],
];
$data['first_view_table']['is_active'] = [
'title' => t('Is Active'),
'help' => t('Just an on/off field.'),
'field' => [
'id' => 'boolean',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'boolean',
'label' => t('Published'),
'type' => 'yes-no',
'use_equal' => TRUE,
],
];
$data['first_view_table']['timestamp'] = [
'title' => t('Timestamp'),
'help' => t('Just a timestamp field.'),
'field' => [
'id' => 'date',
],
'sort' => [
'id' => 'date',
],
'filter' => [
'id' => 'date',
],
];
$data['views']['area'] = [
'title' => t('Text area'),
'help' => t('Provide markup text for the area.'),
'area' => [
'id' => 'text',
],
];
return $data;
}
通过以上的步骤....当我们安装并启用在数据库中创建表的模块...您必须填充它以便在视图中查看一些数据...不要忘记在表中添加虚拟数据.....之后,转到结构/视图---并点击“添加视图”----为您的视图命名---然后您将能够在“显示”下拉框中看到表名---在这种情况下表名是“第一视图表”...我希望这篇文章会有所帮助....
相关问题
- 1. 以编程方式创建视图
- 2. 以编程方式创建视图
- 3. Ubercart:以编程方式通过产品创建订单Drupal 8
- 4. 以编程方式在Drupal 8中构建一个视图总和行
- 5. 如何以编程方式创建UIScrollview
- 6. 如何以编程方式创建Tabbar
- 7. 如何以编程方式创建NSPopover
- 8. 如何以编程方式创建UIScrollView?
- 9. 如何以编程方式创建TextView?
- 10. 如何以编程方式创建表格视图?
- 11. 如何以编程方式创建视图对象?
- 12. 如何以编程方式创建Web视图停止按钮
- 13. 如何使用Drupal Workbench Moderation以编程方式创建“草稿”?
- 14. 以编程方式创建视频?
- 15. 以编程方式创建流程图
- 16. 如何以编程方式创建NSSearchField?
- 17. 如何以编程方式创建NSFetchedPropertyDescription?
- 18. 如何以编程方式在Drupal 8中清除缓存
- 19. 如何以编程方式创建UIViewController?
- 20. 如何以编程方式创建UIButton
- 21. 如何以编程方式创建NSTextField?
- 22. 如何以编程方式创建ProgressBar?
- 23. 如何以编程方式创建CustomCell?
- 24. 如何以编程方式创建GPO?
- 25. 以编程方式创建视图后添加样式
- 26. 试图以编程方式创建rightBarButtonItem
- 27. 以编程方式创建绘图
- 28. 以编程方式创建UML类图
- 29. 以编程方式创建图像?
- 30. 以编程方式创建Drupal 7字段组
你是真的想创建视图(用Drupal的术语来看)还是要创建自定义数据库查询? – MilanG
我正尝试以编程方式创建“视图”。非常感谢您的回复......我昨天成功创建了它 – Rekha