常用 API

API 变更记录与查询

https://www.drupal.org/list-changes/drupal

TBD

https://www.drupal.org/node/1848332

HTTP请求:Guzzle HTTP client library added to replace drupal_http_request() | Drupal.org

页面提示消息(Message)

# D7
# $type: status | warning | error 
drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE);

# D8
# @see https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Messenger%21Messenger.php/class/Messenger/9.0.x
\Drupal::messenger()->addError($message);
\Drupal::messenger()->addWarning($message);
\Drupal::messenger()->addMessage($message);
\Drupal::messenger()->addStatus($message);

日志 API

# D7
# $severity 等级说明:
#   WATCHDOG_EMERGENCY: 紧急,系统不可用
#   WATCHDOG_ALERT: 警告,必须马上采取行动
#   WATCHDOG_CRITICAL: 严重
#   WATCHDOG_ERROR: 错误
#   WATCHDOG_WARNING: 注意
#   WATCHDOG_NOTICE: (默认) 一般,但有用的信息
#   WATCHDOG_INFO: 信息
#   WATCHDOG_DEBUG: 调试信息
#
# @see https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/watchdog/7.x
watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)

# D8
# 可用方法: debug | info | notice | warning | error | criticle | alert | emergency
#
# @see https://api.drupal.org/api/drupal/vendor%21psr%21log%21Psr%21Log%21LoggerTrait.php/9.0.x
\Drupal::logger('name')->info($message, array $context = []);

用户 API

# D7
global $user;
user_is_logged_in();

$account = user_load(1);
$edit = array('name' => 'new_name');
user_save($account, $edit);

# D8
$user = \Drupal::currentUser();
\Drupal::currentUser()->isAuthenticated()

$account = Drupal\user\Entity\User::load($uid);
$account = \Drupal::entityTypeManager()->getStorage('user')->load($uid);

$account->id();
$account->getEmail();
$account->hasRole(AccountInterface::AUTHENTICATED_ROLE);

$account = user_load(1);
$account->name = 'new_name';
$account->save();

# D9
$account = \Drupal\user\Entity\User::load($uid)

节点 API

# D7
$node = node_load($nid);
$nodes = node_load_multiple($nids);

# 获取当前页面节点对象.
$node = menu_get_object();

# D8
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);

# 获取当前页面节点对象.
$node = \Drupal::routeMatch()->getParameter('node');

# 构造节点链接
$link = \Drupal\Core\Link::fromTextAndUrl($text, $node->toUrl('canonical'))->toString();
$link = \Drupal\Core\Link::createFromRoute($text, 'entity.node.canonical', ['node' => $nid], $options);


评论 API

# D7
$comment = comment_load($cid);
$comments = comment_load_multiples($cids);

# D8
$comment = \Drupal::entityTypeManager()->getStorage('comment')->load($cid);
$comments = \Drupal::entityTypeManager()->getStorage('comment')->loadMultiple($cids);

数据库 API

# D7
db_select();
db_insert();
db_update();
db_delete();
db_query();

# D8
\Drupal::database()->select();
\Drupal::database()->insert();
\Drupal::database()->update();
\Drupal::database()->delete();
\Drupal::database()->query();

$query->fetchField();

表单 API

# D7
xxx_form();
xxx_form_validate($form, &$form_state)
xxx_form_submit($form, &$form_state)
hook_form_FORM_ID_alter(&$form, &$form_state, $form_id)
hook_form_alter(&$form, &$form_state, $form_id)

# D8
# FormBase 继承方法
getFormId()
buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state)
validateForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state)
submitForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state)
# ConfigFormBase 继承方法,同 FormBase 及以下方法
getEditableConfigNames()

配置项 API

# D7
$var = variable_get($key, $default_value);
variable_set($key, $value);

# D8
# 默认值在 config/install/xxx.yml 文件中声明,不再在代码中指定.
\Drupal::config('module_name.xxx')->get('module_conf_name');
\Drupal::config('module_name.xxx')->get('module_conf_name');
$config = $this->config('module_name.config_name');
$config->set($key, $value);
$config->save()

State API

$state = \Drupal::state();
$state->get('system.cron_last');
$state->set('system.cron_last', REQUEST_TIME);

 命名空间与类

# D8
# 常用命名空间
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;

# 常用 Base 类
# FormBase 
# ConfigFormBase 
# ControllerBase ???

\Drupal\comment\CommentInterface $comment
\Drupal\Core\Form\FormStateInterface $form_state

其它

# ==========
# 模块
# ==========
# D7
module_exists($module_name);

# D8
\Drupal::moduleHandler()->moduleExists($module_name)


# ==========
# 验证
# ==========
# D7
valid_email_address($mail)

# D8
\Drupal::service('email.validator')->isValid($mail)