Drupal 7制作可滚动的Banner

需要的模块:config_pages(用于配置Banner图片,需要一个多图字段), flexslider, linkimage

function mymodule_block_info() {
  // This example comes from node.module.
  $blocks['banner'] = array(
    'info' => t('Banner'),
    'cache' => DRUPAL_NO_CACHE
  );

  return $blocks;
}

function mymodule_block_view($delta = '') {
  if($delta == 'banner') {

    $config = config_pages_config('banner');
    if ($config) {
      // load flexslider.
      $optionset = flexslider_optionset_load('default');
      flexslider_add('block-invest-banner', $optionset);
    }

    // render item.
    $items = array();
    foreach($config->raw()->field_banner_slider['und'] as $image_item) {
      $items[] = theme('linkimage_formatter', array('item' => $image_item, 'image_style' => 'newbanner'));
    }
    $item_list = theme('item_list', array('items' => $items, 'attributes' => array('class' => array('slides'))));
    return array(
      'content' => '
' . $item_list . '
', ); } }

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;