所以我试着在drupal 8中实现一个主题,我想从搜索表单中删除搜索按钮。但当我尝试使用hook_form_alter时,我的.theme文件中没有任何内容被触及,我无法想到一个原因,据我所知,我的语法是正确的。即使它不正确,'die'和var_dump语句至少应输出一些东西,但目前不是。
我已将默认主题更改为drupal8_zymphonies_theme,并且挂钩已参与该主题,因此我认为必须有某种需要改变的设置。据说zymphonies主题是在这个结构/主题/ drupa8_zymphonies_theme,而我的endymion主题是主题/自定义/ endymion。不确定这是否重要,但我会提到它。
/**
* @file
* Contains theme override functions and preprocess functions
*/
var_dump('another hello');
die('hello there');
use Drupal\Core\Template\RenderWrapper;
use Drupal\search\Form\SearchBlockForm;
use Drupal\Core\Form\FormStateInterface;
function endymion_form_search_block_form_alter(SearchBlockForm &$form, FormStateInterface &$form_state, $form_id) {
//Remove the search box from the form to make the look a little cleaner
$logger = \Drupal::logger('endymion');
$logger->debug(''. print_R($form, true) . 'search form');
if ($form_id == 'search_block_form') {
$logger->debug(''. print_R($form, true) . 'search form');
$form['actions']['#attributes']['class'][] = 'element-invisible';
$form['search_block_form']['#size'] = 40; // define size of the textfield
$form['search_block_form']['#default_value'] = t('Search'); // Set a default value for the textfield
// Add extra attributes to the text box
$form['search_block_form']['#attributes']['onblur'] = "if (this.value == '') {this.value = 'Search';}";
$form['search_block_form']['#attributes']['onfocus'] = "if (this.value == 'Search') {this.value = '';}";
// Prevent user from searching the default text
$form['#attributes']['onsubmit'] = "if(this.search_block_form.value=='Search'){ alert('Please enter a search'); return false; }";
// Alternative (HTML5) placeholder attribute instead of using the javascript
$form['search_block_form']['#attributes']['placeholder'] = t('Search');
}
}