简体   繁体   English

如何以编程方式创建 Drupal 8 视图

[英]How to create a Drupal 8 view programmatically

I am trying to create a view programmatically in Drupal 8. Something similar to the crud log module in Drupal 7. I couldn't find any references on the internet either.我正在尝试在 Drupal 8 中以编程方式创建一个视图。类似于 Drupal 7 中的 crud 日志模块。我在互联网上也找不到任何参考资料。

I was successful in creating a view programatically----I did the following-- 1. Create a folder--C:\xampp\htdocs\Drupal Instance\modules 2. Create a YML info file --first_view.info and add the following code to it---- name: First View type: module description: My First Drupal 8 View package: Custom core: 8.x 3. Create an install file---first_view.install And add the following code to it我成功地以编程方式创建了一个视图----我做了以下-- 1. 创建一个文件夹--C:\xampp\htdocs\Drupal Instance\modules 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. Create a file---first_view.views.inc And add the following code to it-- 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;
}

By following the above steps....when we install and enable the module a table gets created in the database...You will have to populate it in order to see some data in the view...don't forget to add dummy data in the table.....After that go to Structure/views--- And click on "Add View"----Give a name to your View---and then you will be able to see the table name in "Show" Drop Down box---In this case the table name is "First View Table"...I hope this article would be helpful....按照上述步骤....当我们安装并启用模块时,会在数据库中创建一个表...您必须填充它才能在视图中看到一些数据...不要忘记添加表中的虚拟数据.....之后转到结构/视图---然后单击“添加视图”----为视图命名---然后您将能够看到该表“显示”下拉框中的名称---在这种情况下,表名称是“第一视图表”...我希望这篇文章会有所帮助....

Although it is not created from scratch programatically, here is a simple method:虽然它不是以编程方式从头开始创建的,但这里有一个简单的方法:

  • create a view using the back office使用后台创建视图
  • export that single view using the configuration manager module使用配置管理器模块导出单个视图
  • copy that yml file and place it in a basic custom module /config/install folder复制该 yml 文件并将其放在基本的自定义模块 /config/install 文件夹中
  • remove the first line containing the uuid删除包含 uuid 的第一行
  • (optional )modify the file manually if needed and if understood how it work (可选)如果需要并且了解其工作原理,请手动修改文件
  • activating your custom module will import the view激活您的自定义模块将导入视图

You can create a new View through the ConfigEntityStorage.您可以通过 ConfigEntityStorage 创建一个新的视图。

Create the View in the UI.在 UI 中创建视图。 Export the View's YAML config file to a path in your module, removing the UUID.将视图的 YAML 配置文件导出到模块中的路径,删除 UUID。

// view config is in `my_module/views.view.my_view.yml`
// (uuid removed!)

$dir = drupal_get_path('module', 'my_module');
$fileStorage = new FileStorage($dir);
$config = $fileStorage->read('views.view.my_view');

/** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $storage */
$storage = \Drupal::entityTypeManager()
  ->getStorage('view');

/** @var \Drupal\views\Entity\View $view */
$view = $storage->create($config);

$view->save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM