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;
  }

Drupal 7 rewrite settings.php

/**
 * Form callback.
 *
 * @param $form
 * @param $form_state
 */
function myform_submit(&$form, &$form_state) {

  global $databases;

  // Update settings.php.
  $inputs = $form_state['input'];

  require_once DRUPAL_ROOT . '/includes/install.inc';

  $settings['databases'] = array(
    'value'    => array('default' => array(
      'default' => $databases['default']['default'],
      'odbc' => array (
        'database' => $inputs['olm_mart_db_name'],
        'username' => $inputs['olm_mart_db_user'],
        'password' => $inputs['olm_mart_db_pass']['pass1'],
        'host' => $inputs['olm_mart_db_host'],
        'port' => $inputs['olm_mart_db_port'],
        'driver' => 'odbc',
        'prefix' => '',
      ),
    )),
    'required' => TRUE,
  );

  //  $settings['drupal_hash_salt'] = array(
  //    'value'    => drupal_random_key(),
  //    'required' => TRUE,
  //  );
  drupal_rewrite_settings($settings);
}

Drupal.t 不工作

今天同事遇到一个问题 Drupal.t 不翻译. Drupal.locale里面也没东西

console.dir(Drupal.locale)

Drupal.locale生成原理

在刷新缓存的时候会执行 _locale_invalidate_js()(modules/locale/locale.module) 去生成JS的标识:refresh: 保存在variable中: javascript_parsed
最后执行:_locale_rebuild_js()(includes/locale.inc)函数去生成相应的JS也就是 Drupal.locale.

最后解决办法

最后我手动在devel中运行_locale_rebuild_js(),发现在sites/defailt/files/languages中并没有生成。 好吧,目录没权限,改了权限以后一切正常

Drupal Update field instance

https://api.drupal.org/api/drupal/modules!field!field.crud.inc/function/field_update_instance/7.x

$field_info = field_info_instance('olm_city', 'field_olm_hero_photo', 'city');
$field_info['required'] = TRUE;
field_update_instance($field_info);