很久以前用的drupal7,也用drupal7搭建过很多网站,那时候下载一些基本的module ,然后后台配置下,然后建些views 什么的,最多再重写下views对应的模版,改下css html,基本就能搭建完了。 后面换了工作基本就没有搞drupal了,现在drupal都已经9了,最近一个契机,让我重拾下,需求很简单,就是希望用户注册后,去调用一个命令生成一些key之类的东西再存起来以便将来使用。
为此我首先建立了一个module ,假如这个module名字是test
可以放在web/modules/下面自己建一个custom 目录 ,然后再建一个test目录, 也可以放在老地方sites/all/default/modules ,建一个test目录。
里面安放3文件:
test.info.yml
name: Test Module
type: module
description: 'Test Module'
package: Custom
version: 1.0
core_version_requirement: ^8 || ^9
test.module
<?php
use Drupal\user\UserInterface;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Drupal\Core\Database\Database;
function test_user_insert(UserInterface $entity)
{
$name = $entity->getAccountName();
$process = new Process(["/usr/local/bin/xxx.sh", $name]);
$process->run();
$query = $connection->insert('test_table')
->fields([
'uid' => $entity->id(),
'username' => $name,
'key' => $public
])
->execute();
}
test.install
<?php
/**
* Implements hook_schema().
*/
function test_schema()
{
$schema['test_table'] = [
'description' => 'test_table keys for users',
'fields' => [
'uid' => [
'description' => 'The {users}.uid that has the keys',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'username' => [
'type' => 'varchar_ascii',
'not null' => TRUE,
'default' => '',
'length' => 255,
'description' => 'user name.'
],
'key' => [
'type' => 'varchar_ascii',
'not null' => TRUE,
'default' => '',
'length' => 255,
'description' => ' key.'
],
'ip' => [
'type' => 'varchar_ascii',
'not null' => TRUE,
'default' => '',
'length' => 100,
]
],
'primary key' => ['uid']
];
return $schema;
}
遇到 的问题
1,hook_user_insert(实际上就是hook_ENTITY_TYPE_insert)
相关文档中entity.api.php中写到:
* @section save Save operations
* To update an existing entity, you will need to load it, change properties,
* and then save; as described above, when creating a new entity, you will also
* need to save it. Here is the order of hooks and other events that happen
* during an entity save:
* - preSave() is called on the entity object, and field objects.
* - hook_ENTITY_TYPE_presave()
* - hook_entity_presave()
* - Entity is saved to storage.
* - For updates on content entities, if there is a translation added that
* was not previously present:
* - hook_ENTITY_TYPE_translation_insert()
* - hook_entity_translation_insert()
* - For updates on content entities, if there was a translation removed:
* - hook_ENTITY_TYPE_translation_delete()
* - hook_entity_translation_delete()
* - postSave() is called on the entity object.
* - hook_ENTITY_TYPE_insert() (new) or hook_ENTITY_TYPE_update() (update)
* - hook_entity_insert() (new) or hook_entity_update() (update)
*
* Some specific entity types invoke hooks during preSave() or postSave()
* operations. Examples:
* - Field configuration preSave(): hook_field_storage_config_update_forbid()
* - Node postSave(): hook_node_access_records() and
* hook_node_access_records_alter()
* - Config entities that are acting as entity bundles in postSave():
* hook_entity_bundle_create()
* - Comment: hook_comment_publish() and hook_comment_unpublish() as
* appropriate.
*
* Note that all translations available for the entity are stored during a save
* operation. When saving a new revision, a copy of every translation is stored,
* regardless of it being affected by the revision.
可以看出来里面的顺序。
2,就是 Process 调用命令的时候
需要开启proc_open 和 proc_get_status 俩函数
另外命令里面如果有涉及到写入的文件,必须保证那个文件是可写入的,不然就出错,而且一点都不会给出错误提示的!!!
3, 调用服务器上的脚本,要注意这个可以写。
4,
The command “xxxx.sh ht31” failed. Exit Code: 1(General error) Working directory: /
这个一般也是权限问题。
5,遇到
Warning: file_get_contents(): open_basedir restriction in effect. File(/xxxxx.key) is not within the allowed path(s): (/xxxxx/:/tmp/:/proc/) in /xxxxx/test.php on line 6
去nginx 里面配置
cd /usr/local/nginx/conf/
vi fastcgi.conf
fastcgi_param PHP_ADMIN_VALUE “open_basedir=$document_root/:/tmp/:/proc/:/xxxxxx”;