2017-05-04 101 views
1

我試圖在Drupal 8中以編程方式創建視圖。類似於Drupal 7中的crud日誌模塊。我無法在Internet上找到任何引用。如何以編程方式創建Drupal 8視圖

+0

你是真的想創建視圖(用Drupal的術語來看)還是要創建自定義數據庫查詢? – MilanG

+0

我正嘗試以編程方式創建「視圖」。非常感謝您的回覆......我昨天成功創建了它 – Rekha

回答

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; 
} 

通過以上的步驟....當我們安裝並啓用在數據庫中創建表的模塊...您必須填充它以便在視圖中查看一些數據...不要忘記在表中添加虛擬數據.....之後,轉到結構/視圖---並點擊「添加視圖」----爲您的視圖命名---然後您將能夠在「顯示」下拉框中看到表名---在這種情況下表名是「第一視圖表」...我希望這篇文章會有所幫助....