Drupal 修改Node显示模式

经常可能有这么一种需求,当判断node一个文件字段,传的是图片的时候用default显示,如果是传了视频那换种显示方式比如用摘要的显示模式去显示

/**
 * Implements hook_entity_view_mode_alter().
 *
 * @param $view_mode
 * @param $context
 */
function mymodule_entity_view_mode_alter(&$view_mode, $context) {
  if (!empty($context['entity']->type) && $context['entity']->type == 'material_other' && !empty($context['entity']->sco_manifest)) {
    $view_mode = 'teaser';
  }
}

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 修改Entity(ECK)默认的CRUD URL

function mymodule_entity_info_alter(&$entity_info) {

  if (isset($entity_info[myentity_type])) {
    // Rebuild entity crud url.
    $entity_info[myentity_type]['bundles'][mybundle]['crud'] = [
      'add' => ['path' => 'admin/content/mybundle/add'],
      'edit' => [
        'path' => 'admin/content/mybundle/%eckentity/edit',
        'entity_id' => 3, // Entity ID在URL上面的位置, 类似arg(3)
      ],
      'delete' => [
        'path' => 'admin/content/mybundle/%eckentity/delete',
        'entity_id' => 3,
      ],
      'view' => [
        'path' => 'admin/content/district/%eckentity/view',
        'entity_id' => 3,
      ]
    ];

    // admin url.
    $entity_info[myentity_type]['bundles']['mybundle']['admin']['path'] = 'admin/content/mybundle';
  }
}

Drupal 覆盖Region theme

创建theme_preprocess_region

function mytheme_preprocess_region(&$variables) {
  $region = $variables['region'];
  $classes = &$variables['classes_array'];

  // Content region.
  if ($region === 'header' && current_path() == 'node/1') {
    // @todo is this actually used properly?
    $variables['theme_hook_suggestions'][] = 'region__myheader__wrapper';
  }
}

创建模板region–myheader–wrapper.tpl.php

print $content;

Drupal 加载文件

1. 使用 include

include drupal_get_path('module', 'mymodule') . '/myfile.inc';

2. 使用module_load_include

module_load_include('inc', 'mymodule', 'myfile');

3. ctools_include

ctools_include('myfile', 'mymodule', 'dir');  // Dir不存在的时候默认使用includes, 后缀是.inc