Drupal 8 创建一个自定义动作/Action

在 mymodule/src/Plugin/Action 定义一个OfflineAction.php的文件

namespace Drupal\sysu_content_server\Plugin\Action;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Action\ActionBase;

/**
 * Distribution revierer action
 *
 * @Action(
 *   id = "offline_action",
 *   label = @Translation("Offline content"),
 *   type = "node"
 * )
 */
class OfflineAction extends ActionBase {

  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition
  ) {
    $this->configuration = $configuration;
    $this->pluginId = $plugin_id;
    $this->pluginDefinition = $plugin_definition;
  }

  /**
   * Checks object access.
   *
   * @param mixed                                 $object
   *   The object to execute the action on.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   (optional) The user for which to check access, or NULL to check access
   *   for the current user. Defaults to NULL.
   * @param bool                                  $return_as_object
   *   (optional) Defaults to FALSE.
   *
   * @return bool|\Drupal\Core\Access\AccessResultInterface
   *   The access result. Returns a boolean if $return_as_object is FALSE (this
   *   is the default) and otherwise an AccessResultInterface object.
   *   When a boolean is returned, the result of AccessInterface::isAllowed() is
   *   returned, i.e. TRUE means access is explicitly allowed, FALSE means
   *   access is either explicitly forbidden or "no opinion".
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    // TODO: Implement access() method.
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function executeMultiple(array $entities) {
    dpm('hello');
  }

  /**
   * Executes the plugin.
   */
  public function execute(ContentEntityInterface $entity = NULL) {
    // TODO: Implement execute() method.
  }
}

此Action定义好以后并不会刷新到,如果你的模块还没有安装。你可以定义安装文件.
mymodule/config/install/system.action.offline_action.yml

status: true
dependencies:
  module:
    - node
id: distribution_action
label: 'Office line content'
type: node
plugin: distribution_action

如果你的模块已经启用。你可以使用PHP执行

$entityManage = $action = \Drupal::entityTypeManager()->getStorage('action');
$entityManage->create([
  'label' => t('Offline content'),
  'id' => 'offline_action',
  'type' => 'node',
  'plugin' => 'offline_action',
])->save();

Drupal 8 Url常用

$current_path = \Drupal::service('path.current')->getPath();
$request = \Drupal::request();
$uri = $request->getUriForPath($current_path);

// 当前完成的路径,不包括query: http://localhost:88/drupal8/web/devel/php
dpm($uri);

// 只是后缀: /drupal8/web/devel/php?a=1&b=1
dpm($request->getRequestUri());


// 当前完整的路径,包括query: http://localhost:88/drupal8/web/devel/php?a=1&b=1
dpm($request->getUri());

Drupal 8 获取指定entity type的action

function getActions($type) {
    $actionEntityTypeManager = \Drupal::entityTypeManager()->getStorage('action');
    $actions = array_filter($actionEntityTypeManager->loadMultiple(), function ($action) use($type) {
      return $action->getType() == $type;
    });

    $action_options = [];
    foreach($actions as $id => $action) {
      $action_options[$action->id()] = $action->label();
    }
    return $action_options;
  }

Nodejs 事件注入

事件类型hook. 可以在指定的地方去注入或执行

var EventEmitter = require('events').EventEmitter;
var util = require('util');

var User = function(username){
    this.username = username;
}
util.inherits(User, EventEmitter);
var user = new User('Xiukun');
user.on('nuevent', function(props){
    console.log(props);
});
user.emit('nuevent', 'dancing');

Drupal 8 提供Oauth2登陆服务端

Drupal 8提供类似微信、Google这样的登陆服务端,将账号在服务器登陆然后获取用户信息.

1. 安装配置simple_oauth, 并且创建一个客户端账号,配置回调的url
2. 前端当点击登陆以后直接跳转到这个URL. 请替换其中的myappid和state(随机字符串可以是时间戳). http://www.mysite.com/oauth/authorize?client_id=myappid&response_type=code&scope=api&state=123
3. 这时在上面配置回掉的url中会获取到一个?code的参数。再根据code去获取Access Token

POST http://www.mysite.com/oauth/token
Args:
array(
   'redirect_uri' => http://www.myclient.com/callback
   'code' => $_GET['code'],
   'grant_type' => 'authorization_code',
   'client_id' => 'df13baf2-7cd8-4aee-bc2b-326910b6a8c0',  ### need change
   'client_secret' => 'abcd',  ### need change.
);

4. 最后通过Access token获取用户信息, 以前API为我自己自定义的API。返回当前登陆用户名。

return ['您的登陆账户为:' . \Drupal::currentUser()->getUsername()];