2017-05-04 3 views
1

Ich versuche, eine Ansicht programmgesteuert in Drupal 8 zu erstellen. Etwas ähnlich dem Crud-Log-Modul in Drupal 7. Ich konnte auch keine Referenzen im Internet finden.So erstellen Sie eine Drupal 8-Ansicht programmgesteuert

+0

Möchten Sie wirklich eine Ansicht erstellen (in Drupal-Terminologie angezeigt) oder eine benutzerdefinierte Datenbankabfrage erstellen? – MilanG

+0

Ich habe versucht, eine "Ansicht" programmatisch zu erstellen. Vielen Dank für Ihre Antwort ... Ich war erfolgreich, es gestern zu schaffen – Rekha

Antwort

0

war ich erfolgreich eine Ansicht programmatisch bei der Schaffung ---- Ich habe die following-- 1. Erstellen Sie einen Ordner - C: \ xampp \ htdocs \ Drupal Instanz \ modules 2. Erstellen Sie eine Info-Datei YML --first_view.info und fügen sie den folgenden Code, um es ---- Name: First View Typ: Modul Beschreibung: My First Drupal 8 Ansicht Paket: Benutzerdefinierte Kern: 8.x 3. eine Create-Datei installieren --- first_view.install Und fügen Sie den folgenden Code hinzu

<?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. Erstellen Sie eine Datei --- first_view.views.inc und fügen Sie den folgenden Code

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

Durch die oben genannten Schritte es-- .... wenn wir installieren und aktivieren das Modul eine Tabelle wird in der Datenbank erstellt ... Sie müssen es ausfüllen, um einige Daten in der Ansicht zu sehen ... Vergessen Sie nicht, Dummy-Daten in der Tabelle hinzuzufügen ..... Danach gehen Sie zu Struktur/Ansichten --- Und klicken Sie auf "Add View" ---- Geben Sie Ihrer Ansicht einen Namen --- und dann werden Sie in der Lage sein, den Tabellennamen in "Show" Dropdown-Box zu sehen --- In diesem Fall der Tabellenname ist "First View Table" ... Ich hoffe dieser Artikel wäre hilfreich ....

Verwandte Themen