Drupal Views 增加默认过滤条件

/**
* Implements hook_form_views_exposed_form_alter().
* Set the default exposed filter value.
* @param $form
* @param $form_state
*/
function mymodule_form_views_exposed_form_alter(&$form, &$form_state) {
  if (!isset($form_state['input']['myfield']['value'])) {
    $form_state['input']['myfield']['value'] = 'test';
  }
}

Drupal 生成机器名

在Drupal很多地方都有一个生成机器名的地方,比如创建内容类型名. Views名. 于是挖出这段代码觉得非常简单(非JS)

https://api.drupal.org/api/drupal/includes%21form.inc/function/form_process_machine_name/7.x

$element['#machine_name'] += array(
    'source' => array('name'),
    'target' => '#' . $element['#id'],
    'label' => t('Machine name'),
    'replace_pattern' => '[^a-z0-9_]+',
    'replace' => '_',
    'standalone' => FALSE,
    'field_prefix' => $element['#field_prefix'],
    'field_suffix' => $element['#field_suffix'],
  );

基本就是这样

preg_replace('#[^a-z0-9_]+#', '_', $str);  // 把非小写字母和数字的替换成下划线