Drupal 7 开发内部培训资料, 分头诗人

drupal8初探,d8和d7关于模块通用的方式

猪跑啦独家原创专稿,欢迎您转载本文,转载请注明来源。
标签: drupal8

drupal8的bate版本出来了,据说d8手册已经出来了,在百度上面搜了下没有找到 ,T-T,唉,只能自己没事瞎琢磨了

这些只是自己瞎猜测的没有进行实践

发现目录结构变化了很多,网站的theme和module该到了根目录themes和modules里面。 同时兼容d7的方式sites/all/的方式

在sites下的readme 是这么说的,大意差不多

This directory structure contains the settings and configuration files specific
to your site or sites and is an integral part of multisite configurations.

It is now recommended to place your custom and downloaded extensions in the
/modules, /themes, and /profiles directories located in the Drupal root. The
sites/all/ subdirectory structure, which was recommended in previous versions
of Drupal, is still supported.

See core/INSTALL.txt for information about single-site installation or
multisite configuration.

关于模块

然后看了下d8的模块目录,看了下dblog模块,发现这个就是watchdog的记录插入数据库方式

name.module文件最醒目,和d7差不多

<?php

/**
 * @file
 * System monitoring and logging for administrators.
 *
 * The Database Logging module monitors your site and keeps a list of recorded
 * events containing usage and performance data, errors, warnings, and similar
 * operational information.
 *
 * @see watchdog()
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function dblog_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.dblog':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Database Logging module logs system events in the Drupal database. For more information, see the online handbook entry for the <a href="!dblog">Database Logging module</a>.', array('!dblog' => 'http://drupal.org/documentation/modules/dblog')) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Monitoring your site') . '</dt>';
      $output .= '<dd>' . t('The Database Logging module allows you to view an event log on the <a href="!dblog">Recent log messages</a> page. The log is a chronological list of recorded events containing usage data, performance data, errors, warnings and operational information. Administrators should check the log on a regular basis to ensure their site is working properly.', array('!dblog' => \Drupal::url('dblog.overview'))) . '</dd>';
      $output .= '<dt>' . t('Debugging site problems') . '</dt>';
      $output .= '<dd>' . t('In case of errors or problems with the site, the <a href="!dblog">Recent log messages</a> page can be useful for debugging, since it shows the sequence of events. The log messages include usage information, warnings, and errors.', array('!dblog' => \Drupal::url('dblog.overview'))) . '</dd>';
      $output .= '</dl>';
      return $output;

    case 'dblog.overview':
      return '<p>' . t('The Database Logging module monitors your website, capturing system events in a log (shown here) to be reviewed by an authorized individual at a later time. This log is a list of recorded events containing usage data, performance data, errors, warnings and operational information. It is vital to check the Recent log messages report on a regular basis, as it is often the only way to tell what is going on.') . '</p>';
  }
}

/**
 * Implements hook_menu_links_discovered_alter().
 */
function dblog_menu_links_discovered_alter(&$links) {
  if (\Drupal::moduleHandler()->moduleExists('search')) {
    $links['dblog.search'] = array(
      'title' => 'Top search phrases',
      'route_name' => 'dblog.search',
      'description' => 'View most popular search phrases.',
      'parent' => 'system.admin_reports',
    );
  }

  return $links;
}

/**
 * Implements hook_cron().
 *
 * Controls the size of the log table, paring it to 'dblog_row_limit' messages.
 */
function dblog_cron() {
  // Cleanup the watchdog table.
  $row_limit = \Drupal::config('dblog.settings')->get('row_limit');

  // For row limit n, get the wid of the nth row in descending wid order.
  // Counting the most recent n rows avoids issues with wid number sequences,
  // e.g. auto_increment value > 1 or rows deleted directly from the table.
  //数据的select变化不是很大
  if ($row_limit > 0) {
    $min_row = db_select('watchdog', 'w')
      ->fields('w', array('wid'))
      ->orderBy('wid', 'DESC')
      ->range($row_limit - 1, 1)
      ->execute()->fetchField();

    // Delete all table entries older than the nth row, if nth row was found.
    //delete 不是很大
    if ($min_row) {
      db_delete('watchdog')
        ->condition('wid', $min_row, '<')
        ->execute();
    }
  }
}

/**
 * Gathers a list of uniquely defined database log message types.
 *
 * @return array
 *   List of uniquely defined database log message types.
 */
function _dblog_get_message_types() {
  return db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type')
    ->fetchAllKeyed(0, 0);
}

/**
 * Implements hook_form_FORM_ID_alter() for system_logging_settings().
 */
//由这个hook推测from的写法和原来的应该是一样的
function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) {
  $row_limits = array(100, 1000, 10000, 100000, 1000000);
  $form['dblog_row_limit'] = array(
    '#type' => 'select',
    '#title' => t('Database log messages to keep'),
    '#default_value' => \Drupal::config('dblog.settings')->get('row_limit'),
    '#options' => array(0 => t('All')) + array_combine($row_limits, $row_limits),
    '#description' => t('The maximum number of messages to keep in the database log. Requires a <a href="@cron">cron maintenance task</a>.', array('@cron' => \Drupal::url('system.status')))
  );

  $form['#submit'][] = 'dblog_logging_settings_submit';
}

/**
 * Form submission handler for system_logging_settings().
 *
 * @see dblog_form_system_logging_settings_alter()
 */
/* 这块别人已经写过了,略
 * 不过我对\Drupal 这个类很感兴趣,让我想起里以前做的一个yii的项目的一些写法,应该会有个aotoload自动加载类的方法 ,来加载第三方类库
 * \Drupa应该是/core/lib/drupal.php文件,然后看到了drupal文件夹,猜想下如果和其他的一些第三方类库整合是否会用到这里
 * 例如fpdf、phpexecl之类的
 * 目前纯属猜测,以后遇到了在研究
 */
function dblog_logging_settings_submit($form, FormStateInterface $form_state) {
  \Drupal::config('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save();
}

name.info换成了dblog.info.yml

name.install文件以前的没有看过,不过用过d7的  schema这个模块,生成的.install文件内容也应该是一样的,

简写下

function dblog_schema() {
  $schema['watchdog'] = array(  //watchdog表名
    'description' => 'Table that contains logs of all system events.',  //表描述
    'fields' => array(
      'wid' => array(    //字段名
        'type' => 'serial',  //字段类型
        'not null' => TRUE,  //是否允许空
        'description' => 'Primary Key: Unique watchdog event ID.', //
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {users}.uid of the user who triggered the event.',
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Type of log message, for example "user" or "page not found."',
      ),
      'message' => array(
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
        'description' => 'Text of log message to be passed into the t() function.',
      ),
      'variables' => array(
        'type' => 'blob',
        'not null' => TRUE,
        'size' => 'big',
        'description' => 'Serialized array of variables that match the message string and that is passed into the t() function.',
      ),
      'severity' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'The severity level of the event; ranges from 0 (Emergency) to 7 (Debug)',
      ),
      'link' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
        'description' => 'Link to view the result of the event.',
      ),
      'location'  => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'URL of the origin of the event.',
      ),
      'referer' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'URL of referring page.',
      ),
      'hostname' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Hostname of the user who triggered the event.',
      ),
      'timestamp' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Unix timestamp of when event occurred.',
      ),
    ),
    'primary key' => array('wid'),  //主键
    'indexes' => array(              //索引
      'type' => array('type'),
      'uid' => array('uid'),
      'severity' => array('severity'),
    ),
  );

  return $schema;
}

从以上这些地方看来d8和d7 是在模块的写法上是可以通用的,只是在开发的时候需要查找下d8的常用api 。

评论

孤魂的头像

好文。drupal 8还是比较新,没太多的资料,还真不敢那么快就使用