我試圖在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